forked from jeroanan/chinook-graphql-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomer.js
86 lines (70 loc) · 2.3 KB
/
Customer.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
class Customer {
constructor(db) {
this.db = db;
}
retrieveCustomers(args) {
const sql = 'SELECT * FROM Customer ORDER BY LastName';
return this.db.retrieveList(sql);
}
retrieveCustomer(args) {
const id = args.customerId;
const sql = `
SELECT c.*, e.FirstName AS SupportRepFirstName, e.LastName AS SupportRepLastName
FROM Customer c
LEFT JOIN Employee e ON c.SupportRepId=e.EmployeeId
WHERE c.CustomerId = ?`;
return this.db.retrieveRowByFields(sql, [id]);
}
setCustomer(args) {
const customerId = args.customerId;
const firstName = args.firstName;
const lastName = args.lastName;
const address = args.address;
const city = args.city;
const state = args.state;
const postalCode = args.postalCode;
const country = args.country;
const email = args.email;
const supportRepId = args.supportRepId;
const sql = `
UPDATE Customer
SET FirstName = ?, LastName = ?, Address = ?, City = ?, State = ?, PostalCode = ?,
Country = ?, Email = ?, SupportRepId = ?
WHERE CustomerId = ?;
`;
return this.db.runSql(
sql,
[firstName, lastName, address, city, state, postalCode, country, email, supportRepId, customerId]);
}
addCustomer(args) {
const firstName = args.firstName;
const lastName = args.lastName;
const address = args.address;
const city = args.city;
const state = args.state;
const postalCode = args.postalCode;
const country = args.country;
const email = args.email;
const supportRepId = args.supportRepId;
const maxSql = 'SELECT MAX(CustomerId)+1 AS CustomerId FROM Customer;';
this.db.retrieveRowByFields(maxSql, [])
.then((x) => {
const sql = `
INSERT INTO Customer (CustomerId, FirstName, LastName, Address, City, State, PostalCode, Country, Email,
SupportRepId)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
`
this.db.runSql(
sql,
[x.CustomerId, firstName, lastName, address, city, state, postalCode, country, email, supportRepId]);
});
}
deleteCustomer(args) {
const customerId = args.customerId;
const sql = `
DELETE FROM Customer WHERE CustomerId = ?;
`;
this.db.runSql(sql, [customerId]);
}
}
exports.Customer = Customer;