-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.js
70 lines (65 loc) · 1.83 KB
/
queries.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
const Pool = require('pg').Pool;
const pool = new Pool({
user: process.env.USER,
password: process.env.PASSWORD,
host : process.env.HOST,
database :process.env.DB,
port: process.env.PORT1
});
const getNotes = (request,response) => {
pool.query('SELECT * from notes', (error,results)=> {
if(error){
throw error;
}
response.status(200).json(results.rows);
});
};
const postNote = (request,response) => {
//console.log(request);
const note = request.body.note;
console.log(note);
pool.query('insert into notes (note) values ($1)',[note],(error,results) => {
if (error){
throw error;
};
response.json({"msg":"The note has been added successfullly."})
});
};
const deleteNote = (request,response) => {
const id = request.body.id;
pool.query('delete from notes where id = $1',[id],(error,results) => {
if (error){
throw error;
}
response.json({"msg":"The note has been deleted successfullly."})
});
};
const updateNote = (request,response) => {
const id = request.body.id;
const note = request.body.note;
pool.query('update notes set note = $1 where id = $2',[note,id],(error,results) => {
if(error){
throw error;
}
response.json({"msg":"The note has been updated successfullly."})
});
};
const fetchNote = (request,response) => {
const id = request.body.id;
pool.query('select * from notes where id=$1',[id],(error,results)=>{
if(error){
throw error;
}
if (results.rows.length==0){
response.json({"msg":"The ID could not be found."})
}
response.status(200).json(results.rows);
});
};
module.exports = {
getNotes,
postNote,
deleteNote,
updateNote,
fetchNote
}