mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 18:33:01 +00:00
backend routes with zod schemas
This commit is contained in:
@@ -1,23 +1,18 @@
|
||||
import { ProjectByCreatorQuerySchema } from "@issue/shared";
|
||||
import type { BunRequest } from "bun";
|
||||
import { getProjectsByCreatorID, getUserById } from "../../db/queries";
|
||||
import { errorResponse, parseQueryParams } from "../../validation";
|
||||
|
||||
// /projects/by-creator?creatorId=1
|
||||
export default async function projectsByCreator(req: BunRequest) {
|
||||
const url = new URL(req.url);
|
||||
const creatorId = url.searchParams.get("creatorId");
|
||||
const parsed = parseQueryParams(url, ProjectByCreatorQuerySchema);
|
||||
if ("error" in parsed) return parsed.error;
|
||||
|
||||
if (!creatorId) {
|
||||
return new Response("creatorId is required", { status: 400 });
|
||||
}
|
||||
const { creatorId } = parsed.data;
|
||||
|
||||
const creatorIdNumber = Number(creatorId);
|
||||
if (!Number.isInteger(creatorIdNumber)) {
|
||||
return new Response("creatorId must be an integer", { status: 400 });
|
||||
}
|
||||
|
||||
const creator = await getUserById(creatorIdNumber);
|
||||
const creator = await getUserById(creatorId);
|
||||
if (!creator) {
|
||||
return new Response(`user with id ${creatorId} not found`, { status: 404 });
|
||||
return errorResponse(`user with id ${creatorId} not found`, "USER_NOT_FOUND", 404);
|
||||
}
|
||||
|
||||
const projects = await getProjectsByCreatorID(creator.id);
|
||||
|
||||
Reference in New Issue
Block a user