Add problem deletion

This commit is contained in:
orosmatthew 2023-04-28 20:18:01 -04:00
parent 80fb1cdb3e
commit c5b060415b
3 changed files with 36 additions and 0 deletions

View File

@ -3,6 +3,15 @@ import type { PageServerLoad } from './$types';
export const load = (async () => {
const query = await db.problem.findMany();
query.sort((a, b) => {
if (a.friendlyName < b.friendlyName) {
return -1;
}
if (a.friendlyName > b.friendlyName) {
return 1;
}
return 0;
});
return {
problems: query.map((row) => {
return { id: row.id, friendlyName: row.friendlyName };

View File

@ -1,4 +1,6 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import type { Actions, PageData } from './$types';
let editing = false;
@ -9,6 +11,18 @@
function stretchTextarea(textarea: HTMLTextAreaElement) {
textarea.style.height = textarea.scrollHeight + 'px';
}
async function deleteProblem() {
const sure = confirm('Are you sure?');
if (!sure) {
return;
}
const res = await fetch($page.url, { method: 'DELETE' });
const data = await res.json();
if (data.success) {
goto('/admin/problems');
}
}
</script>
<h1 style="text-align:center" class="mb-4">{data.problemData.friendlyName}</h1>
@ -17,6 +31,7 @@
<a href="/admin/problems" class="btn btn-outline-primary">Back</a>
</div>
<div class="col-6 text-end">
<button on:click={deleteProblem} type="button" class="btn btn-danger">Delete</button>
{#if !editing}
<button
on:click={() => {

View File

@ -0,0 +1,12 @@
import { error, json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { db } from '$lib/server/prisma';
export const DELETE = (async ({ params }) => {
const problemId = parseInt(params.problemId);
if (isNaN(problemId)) {
throw error(400, 'Invalid problem');
}
await db.problem.delete({ where: { id: problemId } });
return json({ success: true });
}) satisfies RequestHandler;