mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-16 06:58:46 +00:00
/api/category/[id], /api/category/create Endpoint 구현
This commit is contained in:
33
src/routes/api/category/[id]/+server.ts
Normal file
33
src/routes/api/category/[id]/+server.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { error, json } from "@sveltejs/kit";
|
||||
import { z } from "zod";
|
||||
import { authorize } from "$lib/server/modules/auth";
|
||||
import { categoryInfoResponse, type CategoryInfoResponse } from "$lib/server/schemas";
|
||||
import { getCategoryInformation } from "$lib/server/services/category";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const GET: RequestHandler = async ({ locals, params }) => {
|
||||
const { userId } = await authorize(locals, "activeClient");
|
||||
|
||||
const zodRes = z
|
||||
.object({
|
||||
id: z.union([z.enum(["root"]), z.coerce.number().int().positive()]),
|
||||
})
|
||||
.safeParse(params);
|
||||
if (!zodRes.success) error(400, "Invalid path parameters");
|
||||
const { id } = zodRes.data;
|
||||
|
||||
const { metadata, categories } = await getCategoryInformation(userId, id);
|
||||
return json(
|
||||
categoryInfoResponse.parse({
|
||||
metadata: metadata && {
|
||||
parent: metadata.parentId,
|
||||
mekVersion: metadata.mekVersion,
|
||||
dek: metadata.encDek,
|
||||
dekVersion: metadata.dekVersion.toISOString(),
|
||||
name: metadata.encName.ciphertext,
|
||||
nameIv: metadata.encName.iv,
|
||||
},
|
||||
subCategories: categories,
|
||||
} satisfies CategoryInfoResponse),
|
||||
);
|
||||
};
|
||||
23
src/routes/api/category/create/+server.ts
Normal file
23
src/routes/api/category/create/+server.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { error, text } from "@sveltejs/kit";
|
||||
import { authorize } from "$lib/server/modules/auth";
|
||||
import { categoryCreateRequest } from "$lib/server/schemas";
|
||||
import { createCategory } from "$lib/server/services/category";
|
||||
import type { RequestHandler } from "./$types";
|
||||
|
||||
export const POST: RequestHandler = async ({ locals, request }) => {
|
||||
const { userId } = await authorize(locals, "activeClient");
|
||||
|
||||
const zodRes = categoryCreateRequest.safeParse(await request.json());
|
||||
if (!zodRes.success) error(400, "Invalid request body");
|
||||
const { parent, mekVersion, dek, dekVersion, name, nameIv } = zodRes.data;
|
||||
|
||||
await createCategory({
|
||||
userId,
|
||||
parentId: parent,
|
||||
mekVersion,
|
||||
encDek: dek,
|
||||
dekVersion: new Date(dekVersion),
|
||||
encName: { ciphertext: name, iv: nameIv },
|
||||
});
|
||||
return text("Category created", { headers: { "Content-Type": "text/plain" } });
|
||||
};
|
||||
Reference in New Issue
Block a user