coyotte508 HF staff commited on
Commit
7069a57
1 Parent(s): 3d330e9

✨ Admin photos list

Browse files
src/routes/admin/pages/[id]/+server.ts CHANGED
@@ -22,7 +22,15 @@ export const POST: RequestHandler = async (input) => {
22
 
23
  await collections.pages.updateOne(
24
  { _id: id },
25
- { $set: { [`${type}.${body.key}`]: String(body.value).replaceAll('\r', '') } },
 
 
 
 
 
 
 
 
26
  { upsert: true }
27
  );
28
 
 
22
 
23
  await collections.pages.updateOne(
24
  { _id: id },
25
+ {
26
+ $set: {
27
+ [`${type}.${body.key}`]: String(body.value).replaceAll('\r', ''),
28
+ updatedAt: new Date()
29
+ },
30
+ $setOnInsert: {
31
+ createdAt: new Date()
32
+ }
33
+ },
34
  { upsert: true }
35
  );
36
 
src/routes/admin/photos/+page.server.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import { collections } from '$lib/server/db';
2
+ import type { PageServerLoad } from './types';
3
+
4
+ export const load: PageServerLoad = async () => {
5
+ return {
6
+ photos: await collections.pictures.find({ productId: { $exists: false } }).toArray()
7
+ };
8
+ };
src/routes/admin/photos/+page.svelte ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Picture from '$lib/components/Picture.svelte';
3
+ import type { PageData } from './$types';
4
+
5
+ export let data: PageData;
6
+ </script>
7
+
8
+ <h1 class="text-sunray">Liste des photos</h1>
9
+
10
+ <a href="/admin/photos/nouveau" class="my-4 block link">Nouvelle photo</a>
11
+
12
+ <div class="flex flex-row flex-wrap gap-6 mt-6">
13
+ {#each data.photos as photo}
14
+ <div class="flex flex-col text-center">
15
+ <a href="/admin/photos/{photo._id}" class="flex flex-col items-center">
16
+ <Picture picture={photo} class="h-36 block" style="object-fit: scale-down;" />
17
+ <span>{photo.name}</span>
18
+ </a>
19
+ </div>
20
+ {/each}
21
+ </div>
src/routes/admin/photos/[id]/+page.server.ts ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Picture } from '$lib/types/Picture';
2
+ import { client, pictures, picturesFs } from '$lib/server/db';
3
+ import { error, redirect } from '@sveltejs/kit';
4
+ import type { PageServerLoad, Actions } from './$types';
5
+
6
+ export const load: PageServerLoad = async ({ params }) => {
7
+ const picture = await pictures.findOne({ _id: params.id });
8
+
9
+ if (!picture) {
10
+ throw error(404, 'Photo non trouvée');
11
+ }
12
+
13
+ return {
14
+ photo: picture
15
+ };
16
+ };
17
+
18
+ export const actions: Actions = {
19
+ update: async function (input) {
20
+ const name = String((await input.request.formData()).get('name'));
21
+ await pictures.updateOne(
22
+ { _id: input.params.id },
23
+ {
24
+ $set: {
25
+ name,
26
+ updatedAt: new Date()
27
+ }
28
+ }
29
+ );
30
+
31
+ return {};
32
+ },
33
+ delete: async function ({ params }) {
34
+ let picture: Picture | null = null;
35
+
36
+ await client.withSession(async (session) => {
37
+ picture = (await pictures.findOneAndDelete({ _id: params.id }, { session })).value;
38
+ await picturesFs.deleteMany({ picture: params.id }, { session });
39
+ });
40
+
41
+ if (!picture) {
42
+ throw error(404);
43
+ }
44
+
45
+ throw redirect(
46
+ 303,
47
+ picture?.productId ? '/admin/produits/' + picture.productId : '/admin/photos'
48
+ );
49
+ }
50
+ };
src/routes/admin/photos/[id]/+page.svelte ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { enhance } from '$app/forms';
3
+ import PictureComponent from '$lib/components/Picture.svelte';
4
+ import type { PageData } from './$types';
5
+
6
+ export let data: PageData;
7
+ </script>
8
+
9
+ <div class="flex flex-col items-center">
10
+ <form method="post" action="?/update" use:enhance>
11
+ <input type="text" name="name" class="input" value={data.photo.name} />
12
+ <input type="submit" hidden />
13
+ </form>
14
+ <PictureComponent picture={data.photo} class="mt-2 max-w-full" style="max-height: 500px" />
15
+ <form method="post" action="?/delete" use:enhance>
16
+ <input type="submit" value="Supprimer" class="mt-2" />
17
+ </form>
18
+ </div>
src/routes/admin/photos/nouveau/+page.svelte ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { enhance } from '$app/forms';
3
+ import { page } from '$app/stores';
4
+
5
+ const productId = $page.url.searchParams.get('productId');
6
+ </script>
7
+
8
+ <h1 class="text-sunray">Ajouter une photo</h1>
9
+
10
+ <form method="post" action="/admin/photos" enctype="multipart/form-data" use:enhance>
11
+ <label class="block my-4 leading-8">
12
+ Nom de la photo
13
+ <input class="input block" type="text" name="name" placeholder="Nom définitif" required />
14
+ </label>
15
+
16
+ <label class="block my-4 leading-8">
17
+ Fichier JPEG
18
+ <input type="file" name="photo" accept="image/jpeg" class="block required" />
19
+ </label>
20
+
21
+ {#if productId}
22
+ <p>Produit associé: <a href="/admin/produits/{productId}" class="link">{productId}</a></p>
23
+ {/if}
24
+
25
+ <input type="hidden" name="productId" value={productId || ''} />
26
+
27
+ <input type="submit" hidden />
28
+ </form>