mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 10:33:01 +00:00
backend routes with zod schemas
This commit is contained in:
@@ -1,35 +1,28 @@
|
||||
import { IssueCreateRequestSchema } from "@issue/shared";
|
||||
import type { AuthedRequest } from "../../auth/middleware";
|
||||
import { createIssue, getProjectByID, getProjectByKey } from "../../db/queries";
|
||||
import { createIssue, getProjectByID } from "../../db/queries";
|
||||
import { errorResponse, parseJsonBody } from "../../validation";
|
||||
|
||||
// /issue/create?projectId=1&title=Testing&description=Description&status=TO%20DO
|
||||
// OR
|
||||
// /issue/create?projectKey=projectKey&title=Testing&description=Description&status=TO%20DO
|
||||
export default async function issueCreate(req: AuthedRequest) {
|
||||
const url = new URL(req.url);
|
||||
const projectId = url.searchParams.get("projectId");
|
||||
const projectKey = url.searchParams.get("projectKey");
|
||||
const parsed = await parseJsonBody(req, IssueCreateRequestSchema);
|
||||
if ("error" in parsed) return parsed.error;
|
||||
|
||||
let project = null;
|
||||
if (projectId) {
|
||||
project = await getProjectByID(Number(projectId));
|
||||
} else if (projectKey) {
|
||||
project = await getProjectByKey(projectKey);
|
||||
} else {
|
||||
return new Response("missing project key or project id", { status: 400 });
|
||||
}
|
||||
const { projectId, title, description = "", status, assigneeId, sprintId } = parsed.data;
|
||||
|
||||
const project = await getProjectByID(projectId);
|
||||
if (!project) {
|
||||
return new Response(`project not found: provided ${projectId ?? projectKey}`, { status: 404 });
|
||||
return errorResponse(`project not found: ${projectId}`, "PROJECT_NOT_FOUND", 404);
|
||||
}
|
||||
|
||||
const title = url.searchParams.get("title") || "Untitled Issue";
|
||||
const description = url.searchParams.get("description") || "";
|
||||
const sprintIdParam = url.searchParams.get("sprintId");
|
||||
const sprintId = sprintIdParam ? Number(sprintIdParam) : undefined;
|
||||
const assigneeIdParam = url.searchParams.get("assigneeId");
|
||||
const assigneeId = assigneeIdParam ? Number(assigneeIdParam) : undefined;
|
||||
const status = url.searchParams.get("status") || undefined;
|
||||
|
||||
const issue = await createIssue(project.id, title, description, req.userId, sprintId, assigneeId, status);
|
||||
const issue = await createIssue(
|
||||
project.id,
|
||||
title,
|
||||
description,
|
||||
req.userId,
|
||||
sprintId ?? undefined,
|
||||
assigneeId ?? undefined,
|
||||
status,
|
||||
);
|
||||
|
||||
return Response.json(issue);
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { IssueDeleteRequestSchema } from "@issue/shared";
|
||||
import type { BunRequest } from "bun";
|
||||
import { deleteIssue } from "../../db/queries";
|
||||
import { errorResponse, parseJsonBody } from "../../validation";
|
||||
|
||||
// /issue/delete?id=1
|
||||
export default async function issueDelete(req: BunRequest) {
|
||||
const url = new URL(req.url);
|
||||
const id = url.searchParams.get("id");
|
||||
if (!id) {
|
||||
return new Response("missing issue id", { status: 400 });
|
||||
}
|
||||
const parsed = await parseJsonBody(req, IssueDeleteRequestSchema);
|
||||
if ("error" in parsed) return parsed.error;
|
||||
|
||||
const result = await deleteIssue(Number(id));
|
||||
const { id } = parsed.data;
|
||||
|
||||
const result = await deleteIssue(id);
|
||||
if (result.rowCount === 0) {
|
||||
return new Response(`no issue with id ${id} found`, { status: 404 });
|
||||
return errorResponse(`no issue with id ${id} found`, "ISSUE_NOT_FOUND", 404);
|
||||
}
|
||||
|
||||
return new Response(`issue with id ${id} deleted`, { status: 200 });
|
||||
return Response.json({ success: true });
|
||||
}
|
||||
|
||||
@@ -1,41 +1,26 @@
|
||||
import { IssueUpdateRequestSchema } from "@issue/shared";
|
||||
import type { BunRequest } from "bun";
|
||||
import { updateIssue } from "../../db/queries";
|
||||
import { errorResponse, parseJsonBody } from "../../validation";
|
||||
|
||||
// /issue/update?id=1&title=Testing&description=Description&assigneeId=2&status=IN%20PROGRESS
|
||||
// assigneeId can be "null" to unassign
|
||||
export default async function issueUpdate(req: BunRequest) {
|
||||
const url = new URL(req.url);
|
||||
const id = url.searchParams.get("id");
|
||||
if (!id) {
|
||||
return new Response("missing issue id", { status: 400 });
|
||||
const parsed = await parseJsonBody(req, IssueUpdateRequestSchema);
|
||||
if ("error" in parsed) return parsed.error;
|
||||
|
||||
const { id, title, description, status, assigneeId, sprintId } = parsed.data;
|
||||
|
||||
// check that at least one field is being updated
|
||||
if (
|
||||
title === undefined &&
|
||||
description === undefined &&
|
||||
status === undefined &&
|
||||
assigneeId === undefined &&
|
||||
sprintId === undefined
|
||||
) {
|
||||
return errorResponse("no updates provided", "NO_UPDATES", 400);
|
||||
}
|
||||
|
||||
const title = url.searchParams.get("title") || undefined;
|
||||
const description = url.searchParams.get("description") || undefined;
|
||||
const sprintIdParam = url.searchParams.get("sprintId");
|
||||
const assigneeIdParam = url.searchParams.get("assigneeId");
|
||||
const status = url.searchParams.get("status") || undefined;
|
||||
|
||||
// parse sprintId: "null" means unassign, number means assign, undefined means no change
|
||||
let sprintId: number | null | undefined;
|
||||
if (sprintIdParam === "null") {
|
||||
sprintId = null;
|
||||
} else if (sprintIdParam) {
|
||||
sprintId = Number(sprintIdParam);
|
||||
}
|
||||
// same for assigneeId
|
||||
let assigneeId: number | null | undefined;
|
||||
if (assigneeIdParam === "null") {
|
||||
assigneeId = null;
|
||||
} else if (assigneeIdParam) {
|
||||
assigneeId = Number(assigneeIdParam);
|
||||
}
|
||||
|
||||
if (!title && !description && sprintId === undefined && assigneeId === undefined && !status) {
|
||||
return new Response("no updates provided", { status: 400 });
|
||||
}
|
||||
|
||||
const issue = await updateIssue(Number(id), {
|
||||
const issue = await updateIssue(id, {
|
||||
title,
|
||||
description,
|
||||
sprintId,
|
||||
|
||||
Reference in New Issue
Block a user