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 () => { export const load = (async () => {
const query = await db.problem.findMany(); 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 { return {
problems: query.map((row) => { problems: query.map((row) => {
return { id: row.id, friendlyName: row.friendlyName }; return { id: row.id, friendlyName: row.friendlyName };

View File

@ -1,4 +1,6 @@
<script lang="ts"> <script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import type { Actions, PageData } from './$types'; import type { Actions, PageData } from './$types';
let editing = false; let editing = false;
@ -9,6 +11,18 @@
function stretchTextarea(textarea: HTMLTextAreaElement) { function stretchTextarea(textarea: HTMLTextAreaElement) {
textarea.style.height = textarea.scrollHeight + 'px'; 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> </script>
<h1 style="text-align:center" class="mb-4">{data.problemData.friendlyName}</h1> <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> <a href="/admin/problems" class="btn btn-outline-primary">Back</a>
</div> </div>
<div class="col-6 text-end"> <div class="col-6 text-end">
<button on:click={deleteProblem} type="button" class="btn btn-danger">Delete</button>
{#if !editing} {#if !editing}
<button <button
on:click={() => { 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;