mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 10:33:01 +00:00
Add 'packages/backend/' from commit 'acce648ee5e7e3a3006451e637c0db654820cc48'
git-subtree-dir: packages/backend git-subtree-mainline:d0babd62afgit-subtree-split:acce648ee5
This commit is contained in:
22
packages/backend/src/routes/index.ts
Normal file
22
packages/backend/src/routes/index.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import issueCreate from "./issue/create";
|
||||
import issueDelete from "./issue/delete";
|
||||
import issueUpdate from "./issue/update";
|
||||
import issuesInProject from "./issues/[projectBlob]";
|
||||
import issues from "./issues/all";
|
||||
|
||||
import projectCreate from "./project/create";
|
||||
import projectUpdate from "./project/update";
|
||||
import projectDelete from "./project/delete";
|
||||
|
||||
export const routes = {
|
||||
issueCreate,
|
||||
issueDelete,
|
||||
issueUpdate,
|
||||
|
||||
issuesInProject,
|
||||
issues,
|
||||
|
||||
projectCreate,
|
||||
projectUpdate,
|
||||
projectDelete,
|
||||
};
|
||||
30
packages/backend/src/routes/issue/create.ts
Normal file
30
packages/backend/src/routes/issue/create.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { createIssue, getProjectByID, getProjectByBlob } from "../../db/queries";
|
||||
|
||||
// /issue/create?projectId=1&title=Testing&description=Description
|
||||
// OR
|
||||
// /issue/create?projectBlob=projectBlob&title=Testing&description=Description
|
||||
export default async function issueCreate(req: BunRequest) {
|
||||
const url = new URL(req.url);
|
||||
const projectId = url.searchParams.get("projectId");
|
||||
const projectBlob = url.searchParams.get("projectBlob");
|
||||
|
||||
let project = null;
|
||||
if (projectId) {
|
||||
project = await getProjectByID(Number(projectId));
|
||||
} else if (projectBlob) {
|
||||
project = await getProjectByBlob(projectBlob);
|
||||
} else {
|
||||
return new Response("missing project blob or project id", { status: 400 });
|
||||
}
|
||||
if (!project) {
|
||||
return new Response(`project not found: provided ${projectId ?? projectBlob}`, { status: 404 });
|
||||
}
|
||||
|
||||
const title = url.searchParams.get("title") || "Untitled Issue";
|
||||
const description = url.searchParams.get("description") || "";
|
||||
|
||||
const issue = await createIssue(project.id, title, description);
|
||||
|
||||
return Response.json(issue);
|
||||
}
|
||||
18
packages/backend/src/routes/issue/delete.ts
Normal file
18
packages/backend/src/routes/issue/delete.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { deleteIssue } from "../../db/queries";
|
||||
|
||||
// /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 result = await deleteIssue(Number(id));
|
||||
if (result.rowCount === 0) {
|
||||
return new Response(`no issue with id ${id} found`, { status: 404 });
|
||||
}
|
||||
|
||||
return new Response(`issue with id ${id} deleted`, { status: 200 });
|
||||
}
|
||||
24
packages/backend/src/routes/issue/update.ts
Normal file
24
packages/backend/src/routes/issue/update.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { updateIssue } from "../../db/queries";
|
||||
|
||||
// /issue/update?id=1&title=Testing&description=Description
|
||||
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 title = url.searchParams.get("title") || undefined;
|
||||
const description = url.searchParams.get("description") || undefined;
|
||||
if (!title && !description) {
|
||||
return new Response("no updates provided", { status: 400 });
|
||||
}
|
||||
|
||||
const issue = await updateIssue(Number(id), {
|
||||
title,
|
||||
description,
|
||||
});
|
||||
|
||||
return Response.json(issue);
|
||||
}
|
||||
14
packages/backend/src/routes/issues/[projectBlob].ts
Normal file
14
packages/backend/src/routes/issues/[projectBlob].ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { getIssuesByProject, getProjectByBlob } from "../../db/queries";
|
||||
|
||||
export default async function issuesInProject(req: BunRequest<"/issues/:projectBlob">) {
|
||||
const { projectBlob } = req.params;
|
||||
|
||||
const project = await getProjectByBlob(projectBlob);
|
||||
if (!project) {
|
||||
return new Response(`project not found: provided ${projectBlob}`, { status: 404 });
|
||||
}
|
||||
const issues = await getIssuesByProject(project.id);
|
||||
|
||||
return Response.json(issues);
|
||||
}
|
||||
8
packages/backend/src/routes/issues/all.ts
Normal file
8
packages/backend/src/routes/issues/all.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { getIssues } from "../../db/queries";
|
||||
|
||||
export default async function issuesAll(req: BunRequest) {
|
||||
const issues = await getIssues();
|
||||
|
||||
return Response.json(issues);
|
||||
}
|
||||
32
packages/backend/src/routes/project/create.ts
Normal file
32
packages/backend/src/routes/project/create.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { createProject, getUserById, getProjectByBlob } from "../../db/queries";
|
||||
|
||||
// /project/create?blob=BLOB&name=Testing&ownerId=1
|
||||
export default async function projectCreate(req: BunRequest) {
|
||||
const url = new URL(req.url);
|
||||
const blob = url.searchParams.get("blob");
|
||||
const name = url.searchParams.get("name");
|
||||
const ownerId = url.searchParams.get("ownerId");
|
||||
|
||||
if (!blob || !name || !ownerId) {
|
||||
return new Response(
|
||||
`missing parameters: ${!blob ? "blob " : ""}${!name ? "name " : ""}${!ownerId ? "ownerId" : ""}`,
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// check if project with blob already exists
|
||||
const existingProject = await getProjectByBlob(blob);
|
||||
if (existingProject) {
|
||||
return new Response(`project with blob ${blob} already exists`, { status: 400 });
|
||||
}
|
||||
|
||||
const owner = await getUserById(parseInt(ownerId, 10));
|
||||
if (!owner) {
|
||||
return new Response(`owner with id ${ownerId} not found`, { status: 404 });
|
||||
}
|
||||
|
||||
const project = await createProject(blob, name, owner.id);
|
||||
|
||||
return Response.json(project);
|
||||
}
|
||||
21
packages/backend/src/routes/project/delete.ts
Normal file
21
packages/backend/src/routes/project/delete.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { getProjectByID, deleteProject } from "../../db/queries";
|
||||
|
||||
// /project/delete?id=1
|
||||
export default async function projectDelete(req: BunRequest) {
|
||||
const url = new URL(req.url);
|
||||
const id = url.searchParams.get("id");
|
||||
|
||||
if (!id) {
|
||||
return new Response(`project id is required`, { status: 400 });
|
||||
}
|
||||
|
||||
const existingProject = await getProjectByID(Number(id));
|
||||
if (!existingProject) {
|
||||
return new Response(`project with id ${id} does not exist`, { status: 404 });
|
||||
}
|
||||
|
||||
await deleteProject(Number(id));
|
||||
|
||||
return new Response(`project with id ${id} deleted successfully`, { status: 200 });
|
||||
}
|
||||
44
packages/backend/src/routes/project/update.ts
Normal file
44
packages/backend/src/routes/project/update.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { BunRequest } from "bun";
|
||||
import { getProjectByBlob, getProjectByID, getUserById, updateProject } from "../../db/queries";
|
||||
|
||||
// /project/update?id=1&blob=NEW&name=new%20name&ownerId=1
|
||||
export default async function projectUpdate(req: BunRequest) {
|
||||
const url = new URL(req.url);
|
||||
const id = url.searchParams.get("id");
|
||||
const blob = url.searchParams.get("blob") || undefined;
|
||||
const name = url.searchParams.get("name") || undefined;
|
||||
const ownerId = url.searchParams.get("ownerId") || undefined;
|
||||
|
||||
if (!id) {
|
||||
return new Response(`project id is required`, { status: 400 });
|
||||
}
|
||||
|
||||
const existingProject = await getProjectByID(Number(id));
|
||||
if (!existingProject) {
|
||||
return new Response(`project with id ${id} does not exist`, { status: 404 });
|
||||
}
|
||||
|
||||
if (!blob && !name && !ownerId) {
|
||||
return new Response(`at least one of blob, name, or ownerId must be provided`, {
|
||||
status: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const projectWithBlob = blob ? await getProjectByBlob(blob) : null;
|
||||
if (projectWithBlob && projectWithBlob.id !== Number(id)) {
|
||||
return new Response(`a project with blob "${blob}" already exists`, { status: 400 });
|
||||
}
|
||||
|
||||
const newOwner = ownerId ? await getUserById(Number(ownerId)) : null;
|
||||
if (ownerId && !newOwner) {
|
||||
return new Response(`user with id ${ownerId} does not exist`, { status: 400 });
|
||||
}
|
||||
|
||||
const project = await updateProject(Number(id), {
|
||||
blob: blob,
|
||||
name: name,
|
||||
ownerId: newOwner?.id,
|
||||
});
|
||||
|
||||
return Response.json(project);
|
||||
}
|
||||
Reference in New Issue
Block a user