all fixes for Project.creatorId and Organisation tables

This commit is contained in:
Oliver Bryan
2025-12-22 20:13:08 +00:00
parent 551a868be9
commit 4bf4f832b7
17 changed files with 176 additions and 96 deletions

View File

@@ -7,12 +7,12 @@ import issueUpdate from "./issue/update";
import issuesInProject from "./issues/[projectBlob]";
import issues from "./issues/all";
import projectsAll from "./project/all";
import projectsByOwner from "./project/by-owner";
import projectsByCreator from "./project/by-creator";
import projectCreate from "./project/create";
import projectDelete from "./project/delete";
import projectUpdate from "./project/update";
import projectWithOwner from "./project/with-owner";
import projectsWithOwners from "./project/with-owners";
import projectWithCreator from "./project/with-creator";
import projectsWithCreators from "./project/with-creators";
export const routes = {
issueCreate,
@@ -25,10 +25,10 @@ export const routes = {
projectCreate,
projectUpdate,
projectDelete,
projectsByOwner,
projectsByCreator,
projectsAll,
projectsWithOwners,
projectWithOwner,
projectsWithCreators,
projectWithCreator,
authRegister,
authLogin,

View File

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

View File

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

View File

@@ -1,26 +0,0 @@
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

@@ -1,16 +1,17 @@
import type { BunRequest } from "bun";
import { createProject, getUserById, getProjectByBlob } from "../../db/queries";
import { createProject, getProjectByBlob, getUserById } from "../../db/queries";
// /project/create?blob=BLOB&name=Testing&ownerId=1
// /project/create?blob=BLOB&name=Testing&creatorId=1&organisationId=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");
const creatorId = url.searchParams.get("creatorId");
const organisationId = url.searchParams.get("organisationId");
if (!blob || !name || !ownerId) {
if (!blob || !name || !creatorId || !organisationId) {
return new Response(
`missing parameters: ${!blob ? "blob " : ""}${!name ? "name " : ""}${!ownerId ? "ownerId" : ""}`,
`missing parameters: ${!blob ? "blob " : ""}${!name ? "name " : ""}${!creatorId ? "creatorId " : ""}${!organisationId ? "organisationId" : ""}`,
{ status: 400 },
);
}
@@ -21,12 +22,12 @@ export default async function projectCreate(req: BunRequest) {
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 creator = await getUserById(parseInt(creatorId, 10));
if (!creator) {
return new Response(`creator with id ${creatorId} not found`, { status: 404 });
}
const project = await createProject(blob, name, owner.id);
const project = await createProject(blob, name, creator.id, parseInt(organisationId, 10));
return Response.json(project);
}

View File

@@ -1,13 +1,14 @@
import type { BunRequest } from "bun";
import { getProjectByBlob, getProjectByID, getUserById, updateProject } from "../../db/queries";
// /project/update?id=1&blob=NEW&name=new%20name&ownerId=1
// /project/update?id=1&blob=NEW&name=new%20name&creatorId=1&organisationId=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;
const creatorId = url.searchParams.get("creatorId") || undefined;
const organisationId = url.searchParams.get("organisationId") || undefined;
if (!id) {
return new Response(`project id is required`, { status: 400 });
@@ -18,8 +19,8 @@ export default async function projectUpdate(req: BunRequest) {
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`, {
if (!blob && !name && !creatorId && !organisationId) {
return new Response(`at least one of blob, name, creatorId, or organisationId must be provided`, {
status: 400,
});
}
@@ -29,15 +30,16 @@ export default async function projectUpdate(req: BunRequest) {
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 newCreator = creatorId ? await getUserById(Number(creatorId)) : null;
if (creatorId && !newCreator) {
return new Response(`user with id ${creatorId} does not exist`, { status: 400 });
}
const project = await updateProject(Number(id), {
blob: blob,
name: name,
ownerId: newOwner?.id,
creatorId: newCreator?.id,
organisationId: organisationId ? Number(organisationId) : undefined,
});
return Response.json(project);

View File

@@ -1,8 +1,8 @@
import type { BunRequest } from "bun";
import { getProjectWithOwnerByID } from "../../db/queries";
import { getProjectWithCreatorByID } from "../../db/queries";
// /project/with-owner?id=1
export default async function projectWithOwnerByID(req: BunRequest) {
// /project/with-creator?id=1
export default async function projectWithCreatorByID(req: BunRequest) {
const url = new URL(req.url);
const id = url.searchParams.get("id");
@@ -15,10 +15,10 @@ export default async function projectWithOwnerByID(req: BunRequest) {
return new Response("project id must be an integer", { status: 400 });
}
const projectWithOwner = await getProjectWithOwnerByID(projectId);
if (!projectWithOwner || !projectWithOwner.Project) {
const projectWithCreator = await getProjectWithCreatorByID(projectId);
if (!projectWithCreator || !projectWithCreator.Project) {
return new Response(`project with id ${id} not found`, { status: 404 });
}
return Response.json(projectWithOwner);
return Response.json(projectWithCreator);
}

View File

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

View File

@@ -1,9 +0,0 @@
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);
}