diff options
| author | Fuwn <[email protected]> | 2024-02-11 15:45:56 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-11 15:45:56 -0800 |
| commit | a8417ece5d5d0f75a6215dbfd47f0cc2f691defd (patch) | |
| tree | 3b877afff20635f8acdcfe0fb09cb1c8581c4de4 /src | |
| parent | refactor(database): rename event badges (diff) | |
| download | due.moe-a8417ece5d5d0f75a6215dbfd47f0cc2f691defd.tar.xz due.moe-a8417ece5d5d0f75a6215dbfd47f0cc2f691defd.zip | |
feat(database): config sync scaffolding
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/Database/userConfiguration.ts | 29 | ||||
| -rw-r--r-- | src/routes/api/configuration/+server.ts | 23 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/lib/Database/userConfiguration.ts b/src/lib/Database/userConfiguration.ts new file mode 100644 index 00000000..5e2f0c96 --- /dev/null +++ b/src/lib/Database/userConfiguration.ts @@ -0,0 +1,29 @@ +import supabase from './supabase'; + +interface UserConfiguration { + user_id: number; + configuration: any; + created_at: string; + updated_at: string; +} + +export const getUserConfiguration = async (userId: number) => { + const { data, error } = await supabase + .from('user_configuration') + .select('*') + .eq('user_id', userId); + + if (error || data.length === 0 || data[0].user_id !== userId) return null; + + return data[0] as UserConfiguration; +}; + +export const setUserConfiguration = async (userId: number, configuration: UserConfiguration) => { + const { data, error } = await supabase + .from('user_configuration') + .upsert({ user_id: userId, configuration }); + + if (error || !data || (data as []).length === 0) return null; + + return data[0] as UserConfiguration; +}; diff --git a/src/routes/api/configuration/+server.ts b/src/routes/api/configuration/+server.ts new file mode 100644 index 00000000..a42d9c54 --- /dev/null +++ b/src/routes/api/configuration/+server.ts @@ -0,0 +1,23 @@ +import { getUserConfiguration, setUserConfiguration } from '$lib/Database/userConfiguration.js'; + +export const GET = async ({ url }) => + Response.json(await getUserConfiguration(Number(url.searchParams.get('id') || 0)), { + headers: { + 'Access-Control-Allow-Origin': 'https://due.moe' + } + }); + +export const PUT = async ({ url, request }) => + Response.json( + await setUserConfiguration(Number(url.searchParams.get('id') || 0), { + configuration: await request.json(), + updated_at: new Date().toISOString(), + user_id: Number(url.searchParams.get('id') || 0), + created_at: new Date().toISOString() + }), + { + headers: { + 'Access-Control-Allow-Origin': 'https://due.moe' + } + } + ); |