delete button in Anmeldung, Breite seite

This commit is contained in:
titver968
2025-04-24 08:14:12 +02:00
parent 3679e89088
commit e3c8dff646
3 changed files with 53 additions and 8 deletions

Binary file not shown.

View File

@@ -6,11 +6,27 @@
const res = await fetch('/api/admin/anmeldungen'); const res = await fetch('/api/admin/anmeldungen');
anmeldungen = await res.json(); anmeldungen = await res.json();
} }
async function loeschen(id: number) {
if (!confirm('Diese Anmeldung wirklich löschen?')) return;
try {
const res = await fetch(`/api/admin/anmeldungen?id=${id}`, { method: 'DELETE' });
if (!res.ok) {
const errorText = await res.text();
throw new Error(`Fehler beim Löschen (${res.status}): ${errorText}`);
}
await ladeAnmeldungen();
} catch (error) {
console.error(error);
alert('Fehler beim Löschen der Anmeldung.\n' + error.message);
}
}
onMount(ladeAnmeldungen); onMount(ladeAnmeldungen);
</script> </script>
<div class="p-6 max-w-4xl mx-auto"> <div class="p-6 max-w-7xl mx-auto">
<h1 class="text-2xl font-bold mb-4 text-center">Alle Anmeldungen</h1> <h1 class="text-2xl font-bold mb-4 text-center">Alle Anmeldungen</h1>
<table class="w-full border text-sm"> <table class="w-full border text-sm">
<thead> <thead>
@@ -18,6 +34,7 @@
<th class="p-2 text-left">Name</th> <th class="p-2 text-left">Name</th>
<th class="p-2 text-left">E-Mail</th> <th class="p-2 text-left">E-Mail</th>
<th class="p-2 text-left">Wunsch 13</th> <th class="p-2 text-left">Wunsch 13</th>
<th class="p-2 text-left">Datum</th>
<th class="p-2 text-left">Aktionen</th> <th class="p-2 text-left">Aktionen</th>
</tr> </tr>
</thead> </thead>
@@ -25,18 +42,30 @@
{#each anmeldungen as a} {#each anmeldungen as a}
<tr class="border-t"> <tr class="border-t">
<td class="p-2">{a.anrede} {a.vorname} {a.nachname}</td> <td class="p-2">{a.anrede} {a.vorname} {a.nachname}</td>
<td class="p-2">{a.email}</td> <td class="p-2">{a.email}</td>
<td class="p-2">
{a.wunsch1?.name}<br>
{a.wunsch2?.name}<br>
{a.wunsch3?.name}
</td> </td>
<td class="p-2">{new Date(a.timestamp).toLocaleDateString()}</td>
<td class="p-2 text-right">
<button
class="text-red-600 hover:underline"
on:click={() => loeschen(a.id)}>
Löschen
</button>
</td> </td>
</tr> </tr>
{/each} {/each}
</tbody> </tbody>
</table>
<button <button
on:click={async () => { on:click={async () => {
await fetch('/api/admin/logout', { method: 'POST' }); await fetch('/api/admin/logout', { method: 'POST' });
location.reload(); location.reload();
}} }}
class="bg-red-600 text-white px-4 py-3 rounded text-center hover:bg-red-700"> class="bg-red-600 text-white px-4 py-3 rounded text-center hover:bg-red-700">
Logout Logout
</button> </button>

View File

@@ -1,9 +1,18 @@
//import { PrismaClient } from '@prisma/client';
//import type { RequestHandler } from '@sveltejs/kit';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import type { RequestHandler } from '@sveltejs/kit'; import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
const prisma = new PrismaClient(); const prisma = new PrismaClient();
export const GET: RequestHandler = async () => { function checkAuth(cookies: any) {
return cookies.get('admin_session') === 'true';
}
export const GET: RequestHandler = async ({ cookies }) => {
if (!checkAuth(cookies)) return new Response('Nicht erlaubt', { status: 401 });
const anmeldungen = await prisma.anmeldung.findMany({ const anmeldungen = await prisma.anmeldung.findMany({
include: { include: {
wunsch1: true, wunsch1: true,
@@ -12,8 +21,15 @@ export const GET: RequestHandler = async () => {
}, },
orderBy: { timestamp: 'desc' } orderBy: { timestamp: 'desc' }
}); });
return new Response(JSON.stringify(anmeldungen), { return new Response(JSON.stringify(anmeldungen), {
headers: { 'Content-Type': 'application/json' } headers: { 'Content-Type': 'application/json' }
}); });
}; };
export const DELETE: RequestHandler = async ({ cookies, url }) => {
if (!checkAuth(cookies)) return new Response('Nicht erlaubt', { status: 401 });
const id = Number(url.searchParams.get('id'));
if (!id) return new Response('Ungültige ID', { status: 400 });
await prisma.anmeldung.delete({ where: { id } });
return json({ success: true });
};