more project routes

This commit is contained in:
Oliver Bryan
2025-12-13 21:55:09 +00:00
parent 789b8ed409
commit 168bd8831c
6 changed files with 80 additions and 0 deletions

View File

@@ -89,6 +89,10 @@ const main = async () => {
"/project/create": withCors(routes.projectCreate),
"/project/update": withCors(routes.projectUpdate),
"/project/delete": withCors(routes.projectDelete),
"/projects/by-owner": withCors(routes.projectsByOwner),
"/projects/all": withCors(routes.projectsAll),
"/projects/with-owners": withCors(routes.projectsWithOwners),
"/project/with-owner": withCors(routes.projectWithOwner),
},
});

View File

@@ -7,6 +7,10 @@ import issues from "./issues/all";
import projectCreate from "./project/create";
import projectUpdate from "./project/update";
import projectDelete from "./project/delete";
import projectsByOwner from "./project/by-owner";
import projectsAll from "./project/all";
import projectsWithOwners from "./project/with-owners";
import projectWithOwner from "./project/with-owner";
export const routes = {
issueCreate,
@@ -19,4 +23,8 @@ export const routes = {
projectCreate,
projectUpdate,
projectDelete,
projectsByOwner,
projectsAll,
projectsWithOwners,
projectWithOwner,
};

View File

@@ -0,0 +1,9 @@
import type { BunRequest } from "bun";
import { getAllProjects } from "../../db/queries";
// /projects/all
export default async function projectsAll(req: BunRequest) {
const projects = await getAllProjects();
return Response.json(projects);
}

View File

@@ -0,0 +1,26 @@
import type { BunRequest } from "bun";
import { getProjectsByOwnerID, getUserById } from "../../db/queries";
// /projects/by-owner?ownerId=1
export default async function projectsByOwner(req: BunRequest) {
const url = new URL(req.url);
const ownerId = url.searchParams.get("ownerId");
if (!ownerId) {
return new Response("ownerId is required", { status: 400 });
}
const ownerIdNumber = Number(ownerId);
if (!Number.isInteger(ownerIdNumber)) {
return new Response("ownerId must be an integer", { status: 400 });
}
const owner = await getUserById(ownerIdNumber);
if (!owner) {
return new Response(`user with id ${ownerId} not found`, { status: 404 });
}
const projects = await getProjectsByOwnerID(owner.id);
return Response.json(projects);
}

View File

@@ -0,0 +1,24 @@
import type { BunRequest } from "bun";
import { getProjectWithOwnerByID } from "../../db/queries";
// /project/with-owner?id=1
export default async function projectWithOwnerByID(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 projectId = Number(id);
if (!Number.isInteger(projectId)) {
return new Response("project id must be an integer", { status: 400 });
}
const projectWithOwner = await getProjectWithOwnerByID(projectId);
if (!projectWithOwner || !projectWithOwner.Project) {
return new Response(`project with id ${id} not found`, { status: 404 });
}
return Response.json(projectWithOwner);
}

View File

@@ -0,0 +1,9 @@
import type { BunRequest } from "bun";
import { getProjectsWithOwners } from "../../db/queries";
// /projects/with-owners
export default async function projectsWithOwners(req: BunRequest) {
const projects = await getProjectsWithOwners();
return Response.json(projects);
}