-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquery.js
More file actions
93 lines (77 loc) · 2.22 KB
/
query.js
File metadata and controls
93 lines (77 loc) · 2.22 KB
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
const { pool } = require("./connectbd");
async function testConnection(){
try {
const result = await pool.query("SELECT NOW()");
console.log("CONECTADO: ", result.rows[0]);
} catch(error) {
console.error("ERROR CONNECTION: ", error);
} finally {
await pool.end();
}
}
const createPlayer = async (name) => {
const query = `
INSERT INTO players (name, points)
VALUES ($1, $2)
RETURNING *
`;
const values = [name, "0"];
const res = await pool.query(query, values)
.then(res => res.rows[0])
.catch(error => {
console.log("ERROR CREATE PLAYER", error);
return {error: "Erro ao postar o player"};
});
return res;
}
const getAll = async () => {
const res = await pool.query("SELECT * FROM players;")
.then(res => res.rows)
.catch(err => {
console.error("ERRO GET ALL PLAYERS:\n", err);
return {error: "Erro ao coletar todos os players"};
});
return res;
}
const patchPoints = async (id, points) => {
const query = `
UPDATE players
SET points = $1
WHERE id = $2
RETURNING *;`;
const values = [points.toString(), id];
const res = await pool
.query(query, values)
.then(res => res.rows[0])
.catch( err =>{
console.error("ERROR PATCH POINTS QUERY: ", err);
return {error: "Erro ao atualizar o player"}
});
return res;
}
const getPlayer = async (id) => {
const query = `
SELECT * FROM players WHERE id=$1;
`;
const res = await pool.query(query, [id])
.then(res => res.rows[0])
.catch(err => {
console.error("ERROR GET PLAYER:\n",err);
return {error: "Erro ao coletar o player"};
});
return res;
}
const deletePlayer = async (id) => {
const query = `
DELETE FROM players
WHERE id = $1;
`
const res = await pool.query(query, [id])
.then(res => { return {message: "Deletado com sucesso!"}})
.catch(err => {
console.error("ERROR DELETE PLAYER:\n",err);
return {error: "Erro ao deletar o player"}
});
return res;
}
module.exports = {createPlayer, getAll, patchPoints, getPlayer, testConnection, deletePlayer};