aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/badges/+server.ts
blob: fc5a4d0a2025c52b505f4ce4ceb8f9cd8d7f4bb3 (plain) (blame)
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
import { userIdentity } from '$lib/AniList/identity';
import { removeUserBadge, updateUserBadge } from '$lib/Database/badges';
import { getUserBadges } from '$lib/Database/badges';
import { addUserBadge } from '$lib/Database/badges';

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']
	});

	if (
		getUserBadges(identity.id).find((badge) => badge.id === Number(url.searchParams.get('update')))
	) {
		updateUserBadge(identity.id, Number(url.searchParams.get('update')), {
			post: url.searchParams.get('post') || undefined,
			image: url.searchParams.get('image') || undefined,
			description: url.searchParams.get('description') || undefined,
			time: url.searchParams.get('time') || undefined
		});
	} else {
		addUserBadge(identity.id, {
			post: url.searchParams.get('post') || undefined,
			image: url.searchParams.get('image') || undefined,
			description: url.searchParams.get('description') || undefined,
			time: url.searchParams.get('time') || undefined
		});
	}

	return Response.json({});
};

export const PATCH = 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']
	});

	updateUserBadge(identity.id, Number(url.searchParams.get('id')), {
		post: url.searchParams.get('post') || undefined,
		image: url.searchParams.get('image') || undefined,
		description: url.searchParams.get('description') || undefined,
		time: url.searchParams.get('time') || undefined
	});

	return Response.json({});
};