File size: 923 Bytes
3d330e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7069a57
 
 
 
 
 
 
 
 
3d330e9
 
 
 
 
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
import { collections } from '$lib/server/db';
import { pages } from '$lib/server/db/page';
import { error } from '@sveltejs/kit';
import type { RequestHandler } from './$types';

export const POST: RequestHandler = async (input) => {
	const id = input.params.id;

	const body: { type: 'picture' | 'text'; key: string; value: string } = await input.request.json();

	if (!(id in pages)) {
		throw error(400, 'Mauvaise id de page: ' + id);
	}

	const type = body.type === 'picture' ? 'pictures' : body.type;

	const page = pages[id as keyof typeof pages];

	if (!(type in page) || !(body.key in page[type])) {
		throw error(400, 'Mauvaise clé');
	}

	await collections.pages.updateOne(
		{ _id: id },
		{
			$set: {
				[`${type}.${body.key}`]: String(body.value).replaceAll('\r', ''),
				updatedAt: new Date()
			},
			$setOnInsert: {
				createdAt: new Date()
			}
		},
		{ upsert: true }
	);

	return new Response();
};