-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodos.router.js
115 lines (94 loc) · 2.91 KB
/
todos.router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import express from 'express';
import { nanoid } from 'nanoid';
import {
getUserById,
createTodo,
getTodosByUserId,
updateTodoCheckById,
getTodoById,
deleteTodo,
} from '../data/queries.js';
const todosRouter = express.Router();
// Use this to simulate a logged in user
const defaultUserId = 'CHANGE TO VALID USER ID STRING';
// Create a todo as a user
todosRouter.post('/', (req, res) => {
const { title } = req.body;
if (!title) return res.status(400).json({ error: 'Missing Todo Title' });
const fetchedUser = getUserById.get(defaultUserId);
if (!fetchedUser) return res.status(400).json({ error: 'User not found' });
const todoId = nanoid(6);
const todoOwner = fetchedUser.user_id;
const createdAt = Date.now();
const addedTodo = createTodo.get(todoId, todoOwner, title, createdAt);
return res.status(201).json({
todoId,
title,
checked: Boolean(addedTodo.checked),
createdAt: new Date(addedTodo.created_at).toISOString(),
});
});
// List all todos of a user
todosRouter.get('/', (req, res) => {
const fetchedUser = getUserById.get(defaultUserId);
if (!fetchedUser) {
return res.status(400).json({ error: 'Unauthenticated user' });
}
const todos = getTodosByUserId.all(defaultUserId);
return res.status(200).json(
todos.map(({ todo_id, title, checked, created_at }) => ({
todoId: todo_id,
title,
checked: Boolean(checked),
createdAt: new Date(created_at).toISOString(),
}))
);
});
// update the status of a todo
todosRouter.patch('/:id', (req, res) => {
const { checked } = req.body;
const todoId = req.params.id;
const recordedTodo = getTodoById.get(todoId);
if (!recordedTodo) {
return res.status(404).json({ error: 'Todo not found' });
}
if (recordedTodo.todo_owner !== defaultUserId) {
return res
.status(401)
.json({ error: 'User unauthorized to update this todo' });
}
const checkedAt = Date.now();
const updatedCheck = checked ? 1 : 0;
const { todo_id, title, checked_at, created_at } = updateTodoCheckById.get(
updatedCheck,
checkedAt,
recordedTodo.todo_owner,
todoId
);
return res.status(200).json({
message: 'Successfully checked todo',
update: {
todoId: todo_id,
title,
check: Boolean(updatedCheck),
checkedAt: new Date(checked_at).toISOString(),
createdAt: new Date(created_at).toISOString(),
},
});
});
// Delete a todo by ID
todosRouter.delete('/:id', (req, res) => {
const todoId = req.params.id;
const recordedTodo = getTodoById.get(todoId);
if (!recordedTodo) {
return res.status(404).json({ error: 'Todo not found' });
}
if (recordedTodo.todo_owner !== defaultUserId) {
return res
.status(401)
.json({ error: 'User unauthorized to delete this todo' });
}
deleteTodo.run(todoId, defaultUserId);
return res.status(200).json({ message: 'Todo successfully deleted!' });
});
export default todosRouter;