diff --git a/src/routes/api/card/all/+server.ts b/src/routes/api/card/all/+server.ts new file mode 100644 index 0000000..1f3c7ff --- /dev/null +++ b/src/routes/api/card/all/+server.ts @@ -0,0 +1,21 @@ +import type { ErrorMessage } from "$lib/api"; +import { getAll, type Card } from "$lib/entities"; +import type { RequestHandler } from "./$types"; + +function isErrorMessage(value: any): value is ErrorMessage { + return value && typeof value.message === 'string'; +} + +export const GET: RequestHandler = async ({ cookies }): Promise => { + const session = cookies.get('session') + if (!session) { + throw new Response(JSON.stringify("no cookies"), { status: 401 }) + } + + const result = await getAll("card", session); + if (isErrorMessage(result)) { + console.log("ERROR") + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +} diff --git a/src/routes/api/card/create/+server.ts b/src/routes/api/card/create/+server.ts new file mode 100644 index 0000000..fdf1eae --- /dev/null +++ b/src/routes/api/card/create/+server.ts @@ -0,0 +1,20 @@ +import type { ErrorMessage } from "$lib/api"; +import { create, type Card } from "$lib/entities"; +import type { RequestHandler } from "./$types"; + +function isErrorMessage(value: any): value is ErrorMessage { + return value && typeof value.message === 'string'; +} + +export const POST: RequestHandler = async ({ request, cookies }): Promise => { + const session = cookies.get('session') + if (!session) { + throw new Response(JSON.stringify("no cookies"), { status: 401 }) + } + const data: Card = await request.json(); + const result = await create("card", data, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +} diff --git a/src/routes/api/card/delete/+server.ts b/src/routes/api/card/delete/+server.ts new file mode 100644 index 0000000..f0babc1 --- /dev/null +++ b/src/routes/api/card/delete/+server.ts @@ -0,0 +1,20 @@ +import type { ErrorMessage } from "$lib/api"; +import { remove, type Card } from "$lib/entities"; +import type { RequestHandler } from "./$types"; + +function isErrorMessage(value: any): value is ErrorMessage { + return value && typeof value.message === 'string'; +} + +export const DELETE: RequestHandler = async ({ request, cookies }): Promise => { + const session = cookies.get('session') + if (!session) { + throw new Response(JSON.stringify("no cookies"), { status: 401 }) + } + const { id }: { id: number } = await request.json(); + const result = await remove("card", id, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +} diff --git a/src/routes/api/card/update/+server.ts b/src/routes/api/card/update/+server.ts new file mode 100644 index 0000000..1641295 --- /dev/null +++ b/src/routes/api/card/update/+server.ts @@ -0,0 +1,20 @@ +import type { ErrorMessage } from "$lib/api"; +import { update, type Card } from "$lib/entities"; +import type { RequestHandler } from "./$types"; + +function isErrorMessage(value: any): value is ErrorMessage { + return value && typeof value.message === 'string'; +} + +export const PUT: RequestHandler = async ({ request, cookies }): Promise => { + const session = cookies.get('session') + if (!session) { + throw new Response(JSON.stringify("no cookies"), { status: 401 }) + } + const data: Card = await request.json(); + const result = await update("card", data.id, data, session); + if (isErrorMessage(result)) { + return new Response(JSON.stringify(result), { status: 500 }) + } + return new Response(JSON.stringify(result), { status: 200 }) +} diff --git a/src/routes/card/+page.svelte b/src/routes/card/+page.svelte new file mode 100644 index 0000000..f6c9edf --- /dev/null +++ b/src/routes/card/+page.svelte @@ -0,0 +1,177 @@ + + +{#if error} +

{error}

+{/if} + +
+

Manage Cards

+ +
+

+ {editingCard ? "Edit Card" : "Add New Card"} +

+
+ + + {#if currentCard.have_credit_line} + + {/if} + +
+ + {#if editingCard} + + {/if} +
+
+
+ +

Cards List

+
    + {#each cards as card} +
  • +
    + {card.name} +
    + Balance: {card.balance}, Credit Line: {card.credit_line},{" "} + Have Credit Line: {card.have_credit_line ? "Yes" : "No"} +
    +
    +
    + + +
    +
  • + {/each} +
+