diff options
| author | Fuwn <[email protected]> | 2023-10-29 22:21:41 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-10-29 22:21:41 -0700 |
| commit | be28fe2d5805a1231f2ba706fa1cd3580afeef10 (patch) | |
| tree | f222de12d4f8fde138c0294595eae374ce5c2c72 /src/routes/api/badges | |
| parent | feat(badges): switch to http get (diff) | |
| download | due.moe-be28fe2d5805a1231f2ba706fa1cd3580afeef10.tar.xz due.moe-be28fe2d5805a1231f2ba706fa1cd3580afeef10.zip | |
refactor(badges): use http methods
Diffstat (limited to 'src/routes/api/badges')
| -rw-r--r-- | src/routes/api/badges/+server.ts | 52 | ||||
| -rw-r--r-- | src/routes/api/badges/add/+server.ts | 26 | ||||
| -rw-r--r-- | src/routes/api/badges/get/+server.ts | 5 | ||||
| -rw-r--r-- | src/routes/api/badges/remove/+server.ts | 22 |
4 files changed, 52 insertions, 53 deletions
diff --git a/src/routes/api/badges/+server.ts b/src/routes/api/badges/+server.ts new file mode 100644 index 00000000..c8a52bcf --- /dev/null +++ b/src/routes/api/badges/+server.ts @@ -0,0 +1,52 @@ +import { userIdentity } from '$lib/AniList/identity.js'; +import { removeUserBadge } from '$lib/userBadgesDatabase.js'; +import { getUserBadges } from '$lib/userBadgesDatabase'; +import { addUserBadge } from '$lib/userBadgesDatabase.js'; + +export const GET = async ({ url }) => { + return Response.json(getUserBadges(Number(url.searchParams.get('id') || 0))); +}; + +export const DELETE = async ({ url, cookies }) => { + const userCookie = cookies.get('user'); + + if (!userCookie) { + return new Response('Unauthenticated', { status: 401 }); + } + + const user = JSON.parse(userCookie); + const identity = await userIdentity({ + tokenType: user['token_type'], + expiresIn: user['expires_in'], + accessToken: user['access_token'], + refreshToken: user['refresh_token'] + }); + + removeUserBadge(identity.id, Number(url.searchParams.get('id'))); + + return Response.json({}); +}; + +export const PUT = async ({ cookies, url }) => { + const userCookie = cookies.get('user'); + + if (!userCookie) { + return new Response('Unauthenticated', { status: 401 }); + } + + const user = JSON.parse(userCookie); + const identity = await userIdentity({ + tokenType: user['token_type'], + expiresIn: user['expires_in'], + accessToken: user['access_token'], + refreshToken: user['refresh_token'] + }); + + addUserBadge(identity.id, { + post: url.searchParams.get('post') || undefined, + image: url.searchParams.get('image') || undefined, + description: url.searchParams.get('description') || undefined + }); + + return Response.json({}); +}; diff --git a/src/routes/api/badges/add/+server.ts b/src/routes/api/badges/add/+server.ts deleted file mode 100644 index 627176a7..00000000 --- a/src/routes/api/badges/add/+server.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { userIdentity } from '$lib/AniList/identity.js'; -import { addUserBadge } from '$lib/userBadgesDatabase.js'; - -export const POST = async ({ cookies, url }) => { - const userCookie = cookies.get('user'); - - if (!userCookie) { - return new Response('Unauthenticated', { status: 401 }); - } - - const user = JSON.parse(userCookie); - const identity = await userIdentity({ - tokenType: user['token_type'], - expiresIn: user['expires_in'], - accessToken: user['access_token'], - refreshToken: user['refresh_token'] - }); - - addUserBadge(identity.id, { - post: url.searchParams.get('post') || undefined, - image: url.searchParams.get('image') || undefined, - description: url.searchParams.get('description') || undefined - }); - - return Response.json({}); -}; diff --git a/src/routes/api/badges/get/+server.ts b/src/routes/api/badges/get/+server.ts deleted file mode 100644 index efef70e9..00000000 --- a/src/routes/api/badges/get/+server.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { getUserBadges } from '$lib/userBadgesDatabase'; - -export const GET = async ({ url }) => { - return Response.json(getUserBadges(Number(url.searchParams.get('id') || 0))); -}; diff --git a/src/routes/api/badges/remove/+server.ts b/src/routes/api/badges/remove/+server.ts deleted file mode 100644 index 8b05369a..00000000 --- a/src/routes/api/badges/remove/+server.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { userIdentity } from '$lib/AniList/identity.js'; -import { removeUserBadge } from '$lib/userBadgesDatabase.js'; - -export const POST = async ({ url, cookies }) => { - const userCookie = cookies.get('user'); - - if (!userCookie) { - return new Response('Unauthenticated', { status: 401 }); - } - - const user = JSON.parse(userCookie); - const identity = await userIdentity({ - tokenType: user['token_type'], - expiresIn: user['expires_in'], - accessToken: user['access_token'], - refreshToken: user['refresh_token'] - }); - - removeUserBadge(identity.id, Number(url.searchParams.get('id'))); - - return Response.json({}); -}; |