Skip to content

Commit 6806a97

Browse files
committed
change endpoints to async/await
1 parent 89b3c90 commit 6806a97

File tree

2 files changed

+64
-105
lines changed

2 files changed

+64
-105
lines changed

client/src/demo/Demo.tsx

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const useStyles = makeStyles({
3838
const Demo = () => {
3939
const classes = useStyles();
4040
const [newTodo, setNewTodo] = useState<string>('');
41-
const [isRequesting, setIsRequesting] = useState<boolean>(false);
4241
const [todos, setTodos] = useState<Todo[]>([]);
4342

4443
useEffect(() => {
@@ -67,20 +66,18 @@ const Demo = () => {
6766

6867
const onSubmitCreateTodo = async (e: React.FormEvent) => {
6968
e.preventDefault();
70-
if (newTodo && !isRequesting) {
71-
setIsRequesting(true);
69+
if (newTodo) {
7270
try {
7371
const res = await axios({
7472
method: 'POST',
7573
url: '/api/create_todo',
76-
data: { todo: newTodo }
74+
data: { todoContent: newTodo }
7775
});
78-
if (res.status === 200) {
76+
if (res.status === 201) {
7977
await getTodos();
8078
}
8179
} catch (err) {
82-
setIsRequesting(false);
83-
throw err;
80+
console.error(err);
8481
} finally {
8582
setNewTodo('');
8683
}
@@ -92,54 +89,46 @@ const Demo = () => {
9289
value: string | boolean,
9390
column: string
9491
) => {
95-
if (!isRequesting) {
96-
setIsRequesting(true);
97-
try {
98-
const response = await axios({
99-
method: 'PUT',
100-
url: '/api/update_todo',
101-
params: { id },
102-
data: { value, column }
103-
});
104-
return response;
105-
} catch (err) {
106-
throw err;
107-
} finally {
108-
setIsRequesting(false);
109-
}
92+
try {
93+
const response = await axios({
94+
method: 'PUT',
95+
url: '/api/update_todo',
96+
params: { id },
97+
data: { value, column }
98+
});
99+
return response;
100+
} catch (err) {
101+
console.error(err);
110102
}
111103
};
112104

113105
const onDeleteTodo = async (id: number) => {
114-
if (!isRequesting) {
115-
setIsRequesting(true);
116-
try {
117-
const res = await axios({
118-
method: 'DELETE',
119-
url: '/api/delete_todo',
120-
params: { id },
121-
data: {}
122-
});
123-
if (res.status === 200) {
124-
const newTodos = todos.filter((todo) => todo.id !== id);
125-
setTodos([...newTodos]);
126-
}
127-
} catch (err) {
128-
console.error(err);
129-
} finally {
130-
setIsRequesting(false);
106+
try {
107+
const res = await axios({
108+
method: 'DELETE',
109+
url: '/api/delete_todo',
110+
params: { id },
111+
data: {}
112+
});
113+
if (res.status === 200) {
114+
const newTodos = todos.filter((todo) => todo.id !== id);
115+
setTodos([...newTodos]);
131116
}
117+
} catch (err) {
118+
console.error(err);
132119
}
133120
};
134121

135122
const getTodos = async () => {
136123
try {
137-
const res = await axios.get('/api/get_todos');
124+
const res = await axios({
125+
method: 'GET',
126+
url: '/api/get_todos',
127+
params: { orderBy: 'created_at', direction: 'desc' }
128+
});
138129
setTodos(res.data.data);
139130
} catch (err) {
140-
throw err;
141-
} finally {
142-
setIsRequesting(false);
131+
console.error(err);
143132
}
144133
};
145134

@@ -214,7 +203,7 @@ const Demo = () => {
214203
</TableRow>
215204
</TableHead>
216205
<TableBody>
217-
{todos.map((todo) => {
206+
{todos?.map((todo) => {
218207
return (
219208
<TableRow
220209
hover

server/src/api/routes/routes.ts

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,47 @@
11
import { Router } from 'express';
2-
import { pool } from '../db-config';
2+
import { pool as db } from '../db-config';
33

44
export const router = Router();
55

66
router.get('/get_todos', async (req, res) => {
7-
pool
8-
.connect()
9-
.then((client) => {
10-
client.query(
11-
`SELECT * FROM Todolist ORDER BY created_at DESC`,
12-
(err, queryRes) => {
13-
if (err) {
14-
console.log(err.stack);
15-
res.status(500).send();
16-
} else res.json({ data: queryRes.rows });
17-
client.release();
18-
}
19-
);
20-
})
21-
.catch((err) => console.log(err));
7+
const { orderBy, direction } = req.query;
8+
try {
9+
const sqlRes = await db.query(
10+
`SELECT * FROM Todolist ORDER BY ${orderBy} ${direction}`
11+
);
12+
res.json({ data: sqlRes.rows });
13+
} catch (err) {
14+
throw err;
15+
}
2216
});
2317

2418
router.post('/create_todo', async (req, res) => {
25-
const { todo } = req.body;
26-
const sqlQuery = {
27-
text: `INSERT INTO Todolist(todo) VALUES($1)`,
28-
values: [todo]
29-
};
30-
pool
31-
.connect()
32-
.then((client) => {
33-
client.query(sqlQuery, (err) => {
34-
if (err) {
35-
console.log(err.stack);
36-
res.status(500).send();
37-
} else res.status(200).send();
38-
client.release();
39-
});
40-
})
41-
.catch((err) => console.log(err));
19+
const { todoContent } = req.body;
20+
try {
21+
await db.query(`INSERT INTO Todolist(todo) VALUES ($1)`, [todoContent]);
22+
res.status(201).send();
23+
} catch (err) {
24+
throw err;
25+
}
4226
});
4327

44-
router.put('/update_todo', (req, res) => {
28+
router.put('/update_todo', async (req, res) => {
4529
const { id } = req.query;
4630
const { value, column } = req.body;
47-
const sqlQuery = `UPDATE Todolist SET ${column} = ${value} WHERE id=${id}`;
48-
pool
49-
.connect()
50-
.then((client) => {
51-
client.query(sqlQuery, (err) => {
52-
if (err) {
53-
res.status(500).send();
54-
} else res.status(200).send();
55-
client.release();
56-
});
57-
})
58-
.catch((err) => console.log(err));
31+
try {
32+
await db.query(`UPDATE Todolist SET ${column} = ${value} WHERE id=${id}`);
33+
res.status(200).send();
34+
} catch (err) {
35+
throw err;
36+
}
5937
});
6038

61-
router.delete('/delete_todo/', (req, res) => {
39+
router.delete('/delete_todo/', async (req, res) => {
6240
const { id } = req.query;
63-
const sqlQuery = `DELETE FROM Todolist WHERE id=${id}`;
64-
65-
pool
66-
.connect()
67-
.then((client) => {
68-
client.query(sqlQuery, (err) => {
69-
if (err) {
70-
res.status(500).send();
71-
console.log(err.stack);
72-
} else res.status(200).send();
73-
});
74-
client.release();
75-
})
76-
.catch((err) => console.log(err));
41+
try {
42+
await db.query(`DELETE FROM Todolist WHERE id=${id}`);
43+
res.status(200).send();
44+
} catch (err) {
45+
throw err;
46+
}
7747
});

0 commit comments

Comments
 (0)