Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update todo.ts #1137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
83 changes: 48 additions & 35 deletions week-10/1-postgres-simple/src/db/todo.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
import { client } from "..";
/*
* Function should insert a new todo for this user
* Should return a todo object
* {
* title: string,
* description: string,
* done: boolean,
* id: number
* }
*/
export async function createTodo(userId: number, title: string, description: string) {


// Function to create a new todo for a user
export async function createTodo(userId, title, description) {
const result = await client.query(`
INSERT INTO todos (user_id, title, description, done)
VALUES ($1, $2, $3, $4)
RETURNING id, title, description, done;
`, [userId, title, description, false]);

return {
id: result.rows[0].id,
title: result.rows[0].title,
description: result.rows[0].description,
done: result.rows[0].done
};
}
/*
* mark done as true for this specific todo.
* Should return a todo object
* {
* title: string,
* description: string,
* done: boolean,
* id: number
* }
*/
export async function updateTodo(todoId: number) {

// Function to mark a todo as done
export async function updateTodo(todoId) {
const result = await client.query(`
UPDATE todos
SET done = true
WHERE id = $1
RETURNING id, title, description, done;
`, [todoId]);

if (result.rows.length === 0) {
throw new Error(`Todo with ID ${todoId} not found.`);
}

return {
id: result.rows[0].id,
title: result.rows[0].title,
description: result.rows[0].description,
done: result.rows[0].done
};
}

/*
* Get all the todos of a given user
* Should return an array of todos
* [{
* title: string,
* description: string,
* done: boolean,
* id: number
* }]
*/
export async function getTodos(userId: number) {
// Function to get all todos for a user
export async function getTodos(userId) {
const result = await client.query(`
SELECT id, title, description, done
FROM todos
WHERE user_id = $1;
`, [userId]);

}
return result.rows.map(row => ({
id: row.id,
title: row.title,
description: row.description,
done: row.done
}));
}