Skip to content

Update syntax to Svelte 5 #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/lib/api/category.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { prisma } from '$lib/prisma';

export const getCategories = async () => {
return await prisma.category.findMany({
return prisma.category.findMany({
orderBy: { name: 'asc' }
});
};

export const getMinimalCategories = async () => {
return await prisma.category.findMany({
select: { id: true, name: true },
return prisma.category.findMany({
select: {
id: true,
name: true
},
orderBy: { name: 'asc' }
});
};
Expand Down
11 changes: 9 additions & 2 deletions src/lib/components/Code.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<script lang="ts">
export let filename: string = '';
import type { Snippet } from 'svelte';

interface Props {
filename?: string;
children: Snippet;
}

let { filename, children }: Props = $props();
</script>

<main class="p-8">
{#if filename}
<pre>{filename}</pre>
{/if}
<div class="bg-base-300 rounded-xl p-8 text-sm">
<slot />
{@render children()}
</div>
</main>
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</div>

<div class="drawer-side z-30 border-r-(--tab-border-color)">
<label for="sidebar-drawer" class="drawer-overlay" />
<label for="sidebar-drawer" class="drawer-overlay"></label>
<aside class="bg-base-100 min-h-full w-36">
<div class="navbar sticky top-0 place-content-center">
<a href="/">
Expand Down
13 changes: 6 additions & 7 deletions src/routes/api/categories/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<script lang="ts">
import type { PageProps } from './$types';
import { goto } from '$app/navigation';
import Code from '$lib/components/Code.svelte';

import type { PageData } from './$types';

export let data: PageData;
$: ({ categories } = data);
let { data }: PageProps = $props();
</script>

<Code filename="/api/categories.json">
<pre>[</pre>
{#each categories as category (category.id)}
{#each data.categories as category (category.id)}
<button
class="cursor-pointer text-left font-mono"
on:click={() => goto(`categories/${category.id}`)}
>{JSON.stringify(category, null, 2)},</button
onclick={() => goto(`categories/${category.id}`)}
>
{JSON.stringify(category, null, 2)},
</button>
{/each}
<pre>]</pre>
</Code>
3 changes: 2 additions & 1 deletion src/routes/api/categories/+page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PageLoad } from './$types';
import type { Category } from '$lib/prisma';

export const load = async ({ fetch }) => {
export const load: PageLoad = async ({ fetch }) => {
const response = await fetch('/api/categories.json');
const categories: Category[] = await response.json();
return { categories };
Expand Down
11 changes: 6 additions & 5 deletions src/routes/api/categories/[id].json/+server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { prismaErrorResponse } from '$lib/api/error';
import { getCategory, updateCategory, deleteCategory } from '$lib/api/category';

// GET /categories/:id.json
export const GET = async ({ params }) => {
const id = parseInt(params.id);
export const GET: RequestHandler = async ({ params }) => {
const id = parseInt(params.id, 10);

try {
const category = await getCategory(id);
Expand All @@ -15,7 +16,7 @@ export const GET = async ({ params }) => {
};

// PATCH /categories/:id.json
export const PATCH = async ({ params, request }) => {
export const PATCH: RequestHandler = async ({ params, request }) => {
const id = parseInt(params.id);

const data = await request.json();
Expand All @@ -42,8 +43,8 @@ export const PATCH = async ({ params, request }) => {
};

// DELETE /categories/:id.json
export const DELETE = async ({ params }) => {
const id = parseInt(params.id);
export const DELETE: RequestHandler = async ({ params }) => {
const id = parseInt(params.id, 10);

try {
const category = await deleteCategory(id);
Expand Down
9 changes: 4 additions & 5 deletions src/routes/api/categories/[id]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<script lang="ts">
import type { PageData } from './$types';
import type { PageProps } from './$types';
import Code from '$lib/components/Code.svelte';

export let data: PageData;
$: ({ category } = data);
let { data }: PageProps = $props();
</script>

<Code>
<pre>{JSON.stringify(category, null, 2)}</pre>
<Code filename="/api/categories/{data.category.id}.json">
<pre>{JSON.stringify(data.category, null, 2)}</pre>
</Code>
3 changes: 2 additions & 1 deletion src/routes/api/categories/[id]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PageLoad } from './$types';
import type { Category } from '$lib/prisma';

export const load = async ({ params, fetch }) => {
export const load: PageLoad = async ({ params, fetch }) => {
const response = await fetch(`/api/categories/${params.id}.json`);
const category: Category = await response.json();
return { category };
Expand Down
3 changes: 2 additions & 1 deletion src/routes/api/categories/minimal.json/+server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { prismaErrorResponse } from '$lib/api/error.js';
import { getMinimalCategories } from '$lib/api/category.js';

// GET /categories/minimal.json
export const GET = async () => {
export const GET: RequestHandler = async () => {
try {
const categories = await getMinimalCategories();
return json(categories);
Expand Down