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
14 changes: 14 additions & 0 deletions apps/backend/lambdas/projects/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ export const handler = async (event: any): Promise<APIGatewayProxyResult> => {
return json(200, projects);
}

// 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}> : {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wrote util helper funcs to validate fields bc 1. detailed err messages, 2. ensure data being processed is in correct format, 3. faster performance - its not up in main yet, so probably just note for future work to use those methods to validate fields here!

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

return json(404, { message: 'Not Found', path: normalizedPath, method });
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/lambdas/projects/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/pg": "^8.15.6",
"js-yaml": "^4.1.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"start-server-and-test": "^2.1.1"
},
"dependencies": {
Expand Down
21 changes: 21 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,24 @@ test("get projects test 🌞", async () => {
console.log(body);
expect(body.length).toBeGreaterThan(0);
});
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");
});