1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/** @jsx jsx */
/** @jsxFrag Fragment */
import { jsx, Fragment } from "https://deno.land/x/hono/middleware.ts";
import { getUserByUsername, insertUserLink, deleteUserLink, getLinkById, updateUserLink, getUserLinks } from "https://esm.town/v/iamseeley/Queries";
import EditProfilePage from "https://esm.town/v/iamseeley/EditProfilePage";
import RootLayout from "https://esm.town/v/iamseeley/RootLayout";
import UserLinks from "https://esm.town/v/iamseeley/UserLinks"
export const addLinkHandler = async (c) => {
try {
const payload = await c.get('jwtPayload');
const body = await c.req.parseBody();
const label = body.label;
const url = body.url;
if (!label || !url) {
return c.json({ error: 'Label and URL are required' }, 400);
}
await insertUserLink(payload.userId, label, url);
const username = payload.username;
const userProfile = await getUserByUsername(username);
return c.html(
<EditProfilePage
user={userProfile}
/>
);
} catch (error) {
console.error('Error adding link:', error);
return c.json({ error: 'Error adding link', message: error.message }, 500);
}
};
export const updateLinkHandler = async (c) => {
try {
const payload = await c.get('jwtPayload');
const username = payload.username;
const linkId = parseInt(c.req.param('linkId'), 10);
const body = await c.req.parseBody();
if (!body.label || !body.url) {
return c.json({ error: 'Label and URL are required' }, 400);
}
const link = await getLinkById(linkId);
if (link.userId !== payload.userId) {
return c.json({ error: 'Unauthorized', message: 'This link does not belong to you' }, 403);
}
await updateUserLink(linkId, body.label, body.url);
const userProfile = await getUserByUsername(username);
return c.html(
<EditProfilePage
user={userProfile}
/>
);
} catch (error) {
console.error('Error updating link:', error);
return c.json({ error: 'Error updating link', message: error.message }, 500);
}
};
export const deleteLinkHandler = async (c) => {
try {
const payload = await c.get('jwtPayload');
const username = payload.username;
const linkId = parseInt(c.req.param('linkId'), 10);
await deleteUserLink(linkId, payload.userId);
const userProfile = await getUserByUsername(username);
return c.html(
<EditProfilePage
user={userProfile}
/>
);
} catch (error) {
console.error('Error deleting link:', error);
return c.json({ error: 'Error deleting link', message: error.message }, 500);
}
};