File size: 1,219 Bytes
7069a57
de10f77
7069a57
 
 
 
de10f77
7069a57
 
 
 
 
 
 
 
 
 
 
 
 
de10f77
7069a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de10f77
 
 
7069a57
 
 
 
 
 
 
 
 
 
 
 
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
import type { Picture } from '$lib/types/Picture';
import { client, collections } from '$lib/server/db';
import { error, redirect } from '@sveltejs/kit';
import type { PageServerLoad, Actions } from './$types';

export const load: PageServerLoad = async ({ params }) => {
	const picture = await collections.pictures.findOne({ _id: params.id });

	if (!picture) {
		throw error(404, 'Photo non trouvée');
	}

	return {
		photo: picture
	};
};

export const actions: Actions = {
	update: async function (input) {
		const name = String((await input.request.formData()).get('name'));
		await collections.pictures.updateOne(
			{ _id: input.params.id },
			{
				$set: {
					name,
					updatedAt: new Date()
				}
			}
		);

		return {};
	},
	delete: async function ({ params }) {
		let picture: Picture | null = null;

		await client.withSession(async (session) => {
			picture = (await collections.pictures.findOneAndDelete({ _id: params.id }, { session }))
				.value;
			await collections.picturesFs.deleteMany({ picture: params.id }, { session });
		});

		if (!picture) {
			throw error(404);
		}

		throw redirect(
			303,
			picture?.productId ? '/admin/produits/' + picture.productId : '/admin/photos'
		);
	}
};