-
Notifications
You must be signed in to change notification settings - Fork 0
/
noteController.js
60 lines (56 loc) · 1.91 KB
/
noteController.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
import pkg from 'pg';
import pool from '../db.js';
export const getNotes = async (req, res) => {
try {
if(req.session.user_id)
{
const notesResult = await pool.query('SELECT * FROM notes WHERE user_id = $1::integer ORDER BY created_at DESC', [req.session.user_id]);
res.json(notesResult.rows);
}
else {
res.status(501).json({ message: "User ID is not a part of the session" });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
};
export const createNote = async (req, res) => {
try {
if(req.session.user_id)
{
const { note } = req.body;
const newNote = await pool.query(
'INSERT INTO notes (user_id, note, created_at) VALUES ($1::integer, $2::text, to_timestamp($3)) RETURNING note_id, note, created_at',
[req.session.user_id, note, Date.now() / 1000.0]
);
res.json(newNote.rows[0]);
}
else {
res.status(501).json({ message: "User ID is not a part of the session" });
}
} catch (err) {
res.status(500).json({ message: err.message });
}
};
export const updateNote = async (req, res) => {
try {
const { note_id } = req.params;
const { note } = req.body;
const updatedNote = await pool.query(
'UPDATE notes SET note = $1 WHERE note_id = $2 RETURNING note_id, note, created_at',
[note, note_id]
);
res.json(updatedNote.rows[0]);
} catch (err) {
res.status(500).json({ message: err.message });
}
};
export const deleteNote = async (req, res) => {
try {
const { note_id } = req.params;
await pool.query('DELETE FROM notes WHERE note_id = $1', [note_id]);
res.json({ message: 'Note deleted' });
} catch (err) {
res.status(500).json({ message: err.message });
}
};