-
Notifications
You must be signed in to change notification settings - Fork 201
/
server.js
136 lines (120 loc) · 3.84 KB
/
server.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import express from 'express';
import Database from 'better-sqlite3';
const app = express();
app.use(express.json());
app.get('/', (req, res) => {
return res.status(200).send({'message': 'SHIPTIVITY API. Read documentation to see API docs'});
});
// We are keeping one connection alive for the rest of the life application for simplicity
const db = new Database('./clients.db');
// Don't forget to close connection when server gets terminated
const closeDb = () => db.close();
process.on('SIGTERM', closeDb);
process.on('SIGINT', closeDb);
/**
* Validate id input
* @param {any} id
*/
const validateId = (id) => {
if (Number.isNaN(id)) {
return {
valid: false,
messageObj: {
'message': 'Invalid id provided.',
'long_message': 'Id can only be integer.',
},
};
}
const client = db.prepare('select * from clients where id = ? limit 1').get(id);
if (!client) {
return {
valid: false,
messageObj: {
'message': 'Invalid id provided.',
'long_message': 'Cannot find client with that id.',
},
};
}
return {
valid: true,
};
}
/**
* Validate priority input
* @param {any} priority
*/
const validatePriority = (priority) => {
if (Number.isNaN(priority)) {
return {
valid: false,
messageObj: {
'message': 'Invalid priority provided.',
'long_message': 'Priority can only be positive integer.',
},
};
}
return {
valid: true,
}
}
/**
* Get all of the clients. Optional filter 'status'
* GET /api/v1/clients?status={status} - list all clients, optional parameter status: 'backlog' | 'in-progress' | 'complete'
*/
app.get('/api/v1/clients', (req, res) => {
const status = req.query.status;
if (status) {
// status can only be either 'backlog' | 'in-progress' | 'complete'
if (status !== 'backlog' && status !== 'in-progress' && status !== 'complete') {
return res.status(400).send({
'message': 'Invalid status provided.',
'long_message': 'Status can only be one of the following: [backlog | in-progress | complete].',
});
}
const clients = db.prepare('select * from clients where status = ?').all(status);
return res.status(200).send(clients);
}
const statement = db.prepare('select * from clients');
const clients = statement.all();
return res.status(200).send(clients);
});
/**
* Get a client based on the id provided.
* GET /api/v1/clients/{client_id} - get client by id
*/
app.get('/api/v1/clients/:id', (req, res) => {
const id = parseInt(req.params.id , 10);
const { valid, messageObj } = validateId(id);
if (!valid) {
res.status(400).send(messageObj);
}
return res.status(200).send(db.prepare('select * from clients where id = ?').get(id));
});
/**
* Update client information based on the parameters provided.
* When status is provided, the client status will be changed
* When priority is provided, the client priority will be changed with the rest of the clients accordingly
* Note that priority = 1 means it has the highest priority (should be on top of the swimlane).
* No client on the same status should not have the same priority.
* This API should return list of clients on success
*
* PUT /api/v1/clients/{client_id} - change the status of a client
* Data:
* status (optional): 'backlog' | 'in-progress' | 'complete',
* priority (optional): integer,
*
*/
app.put('/api/v1/clients/:id', (req, res) => {
const id = parseInt(req.params.id , 10);
const { valid, messageObj } = validateId(id);
if (!valid) {
res.status(400).send(messageObj);
}
let { status, priority } = req.body;
let clients = db.prepare('select * from clients').all();
const client = clients.find(client => client.id === id);
/* ---------- Update code below ----------*/
return res.status(200).send(clients);
});
app.listen(3001);
console.log('app running on port ', 3001);