-
Notifications
You must be signed in to change notification settings - Fork 0
/
webservice.js
57 lines (49 loc) · 1.48 KB
/
webservice.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
const db = require('./connectToCassandra.js');
const client = db.client;
const uuid = require('cassandra-driver').types.Uuid;
const getAllUsers = (req, res) => {
client.execute("SELECT * FROM users.users", (err, result) => {
if (err) {
res.status(400);
return res.json(err);
}
return res.json(result.rows);
});
}
const getUserByLastName = (req, res) => {
const lastName = req.query.lastname;
const lastNameQuery = "SELECT lastname, age, city, email, firstname FROM users.users WHERE lastname = ? ALLOW FILTERING";
const params = [lastName]
client.execute(lastNameQuery, params, {prepare: true}, (err, result) => {
if (err) {
res.status(400);
return res.json(err);
}
return res.json(result.rows);
});
}
const createUser = (req, res) => {
const lastname = req.body.lastname;
const age = parseInt(req.body.age);
const city = req.body.city;
const email = req.body.email;
const firstname = req.body.firstname;
const id = uuid.random();
const insert = "INSERT INTO users.users (id, lastname, age, city, email, firstname) VALUES (?, ?, ?, ?, ?, ?)";
const params = [id, lastname, age, city, email, firstname];
client.execute(insert, params, { prepare : true}, (err, result) => {
if(err) {
res.status(400);
return res.json(err);
}
if(result != undefined) {
res.status(201);
return res.json("Insert was successful");
}
});
}
module.exports = {
getAllUsers,
getUserByLastName,
createUser
};