-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhabitService.js
121 lines (106 loc) · 2.92 KB
/
habitService.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
116
117
118
119
120
121
/**
* This module implements a REST-inspired webservice for the Monopoly DB.
* The database is hosted on ElephantSQL.
*
* Currently, the service supports the player table only.
*
* @author: kvlinden
* @date: Summer, 2020
*/
// Set up the database connection.
const pgp = require('pg-promise')();
const db = pgp({
host: "lallah.db.elephantsql.com",
port: 5432,
database: process.env.USER,
user: process.env.USER,
password: process.env.PASSWORD
});
// Configure the server and its routes.
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const router = express.Router();
router.use(express.json());
router.get("/", readHelloMessage);
router.get("/users", readUsers);
router.get("/buddies", readBuddies)
router.get("/users/:id", readUser);
router.put("/players/:id", updatePlayer);
router.post('/players', createPlayer);
router.delete('/players/:id', deletePlayer);
app.use(router);
app.use(errorHandler);
app.listen(port, () => console.log(`Listening on port ${port}`));
// Implement the CRUD operations.
function errorHandler(err, req, res) {
if (app.get('env') === "development") {
console.log(err);
}
res.sendStatus(err.status || 500);
}
function returnDataOr404(res, data) {
if (data == null) {
res.sendStatus(404);
} else {
res.send(data);
}
}
function readHelloMessage(req, res) {
res.send('Hello, CS 262 habitbudy service!');
}
function readUsers(req, res, next) {
db.many("SELECT * FROM UserTable")
.then(data => {
res.send(data);
})
.catch(err => {
next(err);
})
}
function readBuddies(req, res, next) {
db.many("SELECT * FROM Buddies")
.then(data => {
res.send(data);
})
.catch(err => {
next(err);
})
}
//////////////////Everything below this is unchanged from monopoly
function readUser(req, res, next) {
db.oneOrNone(`SELECT * FROM UserTable WHERE ID=${req.params.id}`)
.then(data => {
returnDataOr404(res, data);
})
.catch(err => {
next(err);
});
}
function updatePlayer(req, res, next) {
db.oneOrNone(`UPDATE Player SET email=$(email), name=$(name) WHERE id=${req.params.id} RETURNING id`, req.body)
.then(data => {
returnDataOr404(res, data);
})
.catch(err => {
next(err);
});
}
function createPlayer(req, res, next) {
db.one(`INSERT INTO Player(email, name) VALUES ($(email), $(name)) RETURNING id`, req.body)
.then(data => {
res.send(data);
})
.catch(err => {
next(err);
});
}
function deletePlayer(req, res, next) {
db.oneOrNone(`DELETE FROM Player WHERE id=${req.params.id} RETURNING id`)
.then(data => {
returnDataOr404(res, data);
})
.catch(err => {
next(err);
});
}