Skip to content

Commit a272e86

Browse files
committed
Solution of week-10 and some small changes in test file
1 parent 33afa18 commit a272e86

File tree

5 files changed

+147
-64
lines changed

5 files changed

+147
-64
lines changed

week-10/1-postgres-simple/src/db/__tests__/db.test.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,45 @@ describe('Todo Operations', () => {
5252
const description = 'Test Description';
5353
const todo = await createTodo(userId, title, description);
5454

55-
expect(todo).toHaveProperty('id');
56-
expect(todo.title).toEqual(title);
57-
expect(todo.description).toEqual(description);
58-
expect(todo.done).toEqual(false);
55+
expect(todo?.id).toBeDefined();
56+
expect(todo?.title).toEqual(title);
57+
expect(todo?.description).toEqual(description);
58+
expect(todo?.done).toEqual(false);
5959
});
6060

61-
test('updateTodo marks a todo as done', async () => {
62-
// First, create a todo to update
63-
const { id: todoId } = await createTodo(userId, 'Update Test', 'To be updated');
64-
65-
const updatedTodo = await updateTodo(todoId);
66-
expect(updatedTodo.done).toEqual(true);
67-
});
61+
test('updateTodo marks a todo as done', async () => {
62+
// First, create a todo to update
63+
const createdTodo = await createTodo(userId, 'Update Test', 'To be updated');
64+
65+
// Ensure createdTodo is defined before extracting its id
66+
expect(createdTodo).toBeDefined();
67+
68+
// Extract id using optional chaining
69+
const todoId = createdTodo?.id;
70+
71+
// Ensure todoId is not undefined
72+
expect(todoId).toBeDefined();
73+
74+
if (todoId !== undefined) {
75+
const updatedTodo = await updateTodo(todoId);
76+
77+
// Ensure updatedTodo is defined
78+
expect(updatedTodo).toBeDefined();
79+
80+
// Use optional chaining for safer property access
81+
expect(updatedTodo?.done).toEqual(true);
82+
} else {
83+
// Handle the case where todoId is undefined
84+
fail('Failed to create todo for update');
85+
}
86+
});
87+
6888

6989
test('getTodos retrieves all todos for a user', async () => {
7090
// Assuming there are already todos created in previous tests
7191
const todos = await getTodos(userId);
72-
73-
expect(todos.length).toBeGreaterThan(0);
74-
todos.forEach(todo => {
92+
expect(todos?.length).toBeGreaterThan(0);
93+
todos?.forEach(todo => {
7594
expect(todo).toHaveProperty('id');
7695
expect(todo.user_id).toEqual(userId);
7796
});
Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,58 @@
1+
// todo.ts
2+
13
import { client } from "..";
2-
/*
3-
* Function should insert a new todo for this user
4-
* Should return a todo object
5-
* {
6-
* title: string,
7-
* description: string,
8-
* done: boolean,
9-
* id: number
10-
* }
11-
*/
12-
export async function createTodo(userId: number, title: string, description: string) {
13-
4+
5+
interface Todo {
6+
id: number;
7+
user_id: number;
8+
title: string;
9+
description: string;
10+
done: boolean;
1411
}
15-
/*
16-
* mark done as true for this specific todo.
17-
* Should return a todo object
18-
* {
19-
* title: string,
20-
* description: string,
21-
* done: boolean,
22-
* id: number
23-
* }
24-
*/
25-
export async function updateTodo(todoId: number) {
2612

13+
// todo.ts
14+
15+
16+
export async function createTodo(userId: number, title: string, description: string){
17+
try {
18+
19+
const result = await client.query('INSERT INTO todos(user_id, title, description, done) VALUES ($1, $2, $3, $4) RETURNING *', [userId, title, description, false]);
20+
21+
if (result.rows.length > 0) {
22+
return result.rows[0] as Todo|undefined;
23+
} else {
24+
console.error("Failed to create todo");
25+
return undefined;
26+
}
27+
} catch (err) {
28+
console.error(err);
29+
30+
}
2731
}
2832

29-
/*
30-
* Get all the todos of a given user
31-
* Should return an array of todos
32-
* [{
33-
* title: string,
34-
* description: string,
35-
* done: boolean,
36-
* id: number
37-
* }]
38-
*/
39-
export async function getTodos(userId: number) {
4033

34+
35+
export async function updateTodo(todoId: number){
36+
try {
37+
const result = await client.query('UPDATE todos SET done = $1 WHERE id = $2 RETURNING *', [true, todoId]);
38+
39+
if (result.rows.length > 0) {
40+
return result.rows[0] as Todo|undefined;
41+
} else {
42+
console.error("Failed to update todo");
43+
return undefined;
44+
}
45+
} catch (err) {
46+
console.error(err);
47+
}
48+
}
49+
50+
export async function getTodos(userId: number){
51+
try {
52+
53+
const result = await client.query('SELECT * FROM todos WHERE user_id = $1', [userId]);
54+
return result.rows ;
55+
} catch (err) {
56+
console.error(err);
57+
}
4158
}

week-10/1-postgres-simple/src/db/user.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,29 @@ import { client } from "..";
99
* name: string
1010
* }
1111
*/
12-
export async function createUser(username: string, password: string, name: string) {
13-
12+
interface User {
13+
id: number;
14+
username: string;
15+
password: string;
16+
name: string;
1417
}
1518

16-
/*
17-
* Should return the User object
18-
* {
19-
* username: string,
20-
* password: string,
21-
* name: string
22-
* }
23-
*/
24-
export async function getUser(userId: number) {
25-
19+
20+
21+
export async function createUser(username: string, password: string, name: string){
22+
try {
23+
const result = await client.query('INSERT INTO users(username, password, name) VALUES($1, $2, $3) ', [username, password, name]);
24+
return result.rows[0];
25+
} catch (err) {
26+
console.error(err);
27+
}
2628
}
29+
30+
export async function getUser(userId: number){
31+
try {
32+
const result = await client.query('SELECT * FROM users WHERE id = $1', [userId]);
33+
return result.rows[0] as User;
34+
} catch (err) {
35+
console.error(err);
36+
}
37+
}

week-10/2-prisma-simple/src/db/todo.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { PrismaClient } from '@prisma/client';
22

33
const prisma = new PrismaClient();
44

5+
interface todotype {
6+
7+
title: string,
8+
description: string,
9+
done: boolean,
10+
id: number
11+
12+
}
513
/*
614
* Function should insert a new todo for this user
715
* Should return a todo object
@@ -13,7 +21,11 @@ const prisma = new PrismaClient();
1321
* }
1422
*/
1523
export async function createTodo(userId: number, title: string, description: string) {
16-
24+
const result = await prisma.todo.create({ data:{ userId:userId,title:title,description:description
25+
}})
26+
27+
return result as todotype;
28+
1729
}
1830
/*
1931
* mark done as true for this specific todo.
@@ -26,7 +38,16 @@ export async function createTodo(userId: number, title: string, description: str
2638
* }
2739
*/
2840
export async function updateTodo(todoId: number) {
41+
const result= await prisma.todo.update({
42+
where:{
43+
id:todoId
44+
},
45+
data:{
46+
done:true
47+
}
48+
})
2949

50+
return result as todotype;
3051
}
3152

3253
/*
@@ -40,5 +61,10 @@ export async function updateTodo(todoId: number) {
4061
* }]
4162
*/
4263
export async function getTodos(userId: number) {
43-
64+
const result = await prisma.todo.findMany({
65+
where:{
66+
userId:userId
67+
}
68+
})
69+
return result ;
4470
}

week-10/2-prisma-simple/src/db/user.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ const prisma = new PrismaClient();
1212
* name: string
1313
* }
1414
*/
15-
export async function createUser(username: string, password: string, name: string) {
15+
interface usertype {
1616

17+
username: string,
18+
password: string,
19+
name: string,
20+
id:number
21+
22+
}
23+
export async function createUser(username: string, password: string, name: string) {
24+
const res= await prisma.user.create({data:{username:username,password:password,name:name}});
25+
return res as usertype;
1726
}
1827

1928
/*
@@ -25,5 +34,6 @@ export async function createUser(username: string, password: string, name: strin
2534
* }
2635
*/
2736
export async function getUser(userId: number) {
28-
37+
const result= await prisma.user.findFirst({ where:{id:userId}});
38+
return result as usertype;
2939
}

0 commit comments

Comments
 (0)