Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions apps/backend/lambdas/projects/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,31 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
return json(200, projects);
}

// GET /projects/{id}
if (rawPath.startsWith('/') && rawPath.split('/').length === 2 && method === 'GET') {
const id = rawPath.split('/')[1];
if (!id) return json(400, { message: 'id is required' });
const project = await db.selectFrom("branch.projects").where("project_id", "=", Number(id)).selectAll().executeTakeFirst();
if (!project) return json(404, { message: `Project not found for id: ${id}` });
return json(200, project);
}


// PUT /projects/{id}
if (rawPath.startsWith('/') && rawPath.split('/').length === 2 && method === 'PUT') {
const id = rawPath.split('/')[1];
if (!id) return json(400, { message: 'id is required' });
const body = event.body ? JSON.parse(event.body) as Record<string, {name:string, total_budget:number}> : {};
const updatedProject = await db
.updateTable("branch.projects")
.set(body)
.where("project_id", "=", Number(id))
.returning(["project_id", "name", "total_budget"]) // control returned fields
.executeTakeFirst();
if (!updatedProject) return json(404, { message: `Project not found for id: ${id}` });
return json(200, updatedProject);
}
// <<< ROUTES-END
// POST /projects
if ((normalizedPath === '' || normalizedPath === '/' || normalizedPath === '/projects') && method === 'POST') {
let body: Record<string, unknown>;
Expand Down
39 changes: 39 additions & 0 deletions apps/backend/lambdas/projects/test/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,42 @@ test("get projects test 🌞", async () => {
console.log(body);
expect(body.length).toBeGreaterThan(0);
});

test("get project by id test 🌞", async () => {
let res = await fetch("http://localhost:3000/projects/1")
expect(res.status).toBe(200);
let body = await res.json();
console.log(body);
expect(body.project_id).toBe(1);
expect(body.name).toContain("Project 1");
});

test("project get 400 test 🌞", async () => {
let res = await fetch("http://localhost:3000/projects/1000", {
method: "GET",
});
expect(res.status).toBe(404);
let body = await res.json();
expect(body.message).toBe("Project not found for id: 1000");
});
test("update project test 🌞", async () => {
let res = await fetch("http://localhost:3000/projects/1", {
method: "PUT",
body: JSON.stringify({ name: "Project 1 Updated", total_budget: 2000 }),
});
expect(res.status).toBe(200);
let body = await res.json();
expect(body.project_id).toBe(1);
expect(body.name).toContain("Project 1 Updated");
expect(Number(body.total_budget)).toBe(Number(2000.00));
});

test("project put 404 test 🌞", async () => {
let res = await fetch("http://localhost:3000/projects/1000", {
method: "PUT",
body: JSON.stringify({ name: "Project 1 Updated", total_budget: 2000 }),
});
expect(res.status).toBe(404);
let body = await res.json();
expect(body.message).toBe("Project not found for id: 1000");
});