|
| 1 | +const sql = require("./db.js"); |
| 2 | + |
| 3 | +// constructor |
| 4 | +const Customer = function(customer) { |
| 5 | + this.email = customer.email; |
| 6 | + this.name = customer.name; |
| 7 | + this.active = customer.active; |
| 8 | +}; |
| 9 | + |
| 10 | +Customer.create = (newCustomer, result) => { |
| 11 | + sql.query("INSERT INTO customers SET ?", newCustomer, (err, res) => { |
| 12 | + if (err) { |
| 13 | + console.log("error: ", err); |
| 14 | + result(err, null); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + console.log("created customer: ", { id: res.insertId, ...newCustomer }); |
| 19 | + result(null, { id: res.insertId, ...newCustomer }); |
| 20 | + }); |
| 21 | +}; |
| 22 | + |
| 23 | +Customer.findById = (customerId, result) => { |
| 24 | + sql.query(`SELECT * FROM customers WHERE id = ${customerId}`, (err, res) => { |
| 25 | + if (err) { |
| 26 | + console.log("error: ", err); |
| 27 | + result(err, null); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + if (res.length) { |
| 32 | + console.log("found customer: ", res[0]); |
| 33 | + result(null, res[0]); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // not found Customer with the id |
| 38 | + result({ kind: "not_found" }, null); |
| 39 | + }); |
| 40 | +}; |
| 41 | + |
| 42 | +Customer.getAll = result => { |
| 43 | + sql.query("SELECT * FROM customers", (err, res) => { |
| 44 | + if (err) { |
| 45 | + console.log("error: ", err); |
| 46 | + result(null, err); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + console.log("customers: ", res); |
| 51 | + result(null, res); |
| 52 | + }); |
| 53 | +}; |
| 54 | + |
| 55 | +Customer.updateById = (id, customer, result) => { |
| 56 | + sql.query( |
| 57 | + "UPDATE customers SET email = ?, name = ?, active = ? WHERE id = ?", |
| 58 | + [customer.email, customer.name, customer.active, id], |
| 59 | + (err, res) => { |
| 60 | + if (err) { |
| 61 | + console.log("error: ", err); |
| 62 | + result(null, err); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (res.affectedRows == 0) { |
| 67 | + // not found Customer with the id |
| 68 | + result({ kind: "not_found" }, null); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + console.log("updated customer: ", { id: id, ...customer }); |
| 73 | + result(null, { id: id, ...customer }); |
| 74 | + } |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +Customer.remove = (id, result) => { |
| 79 | + sql.query("DELETE FROM customers WHERE id = ?", id, (err, res) => { |
| 80 | + if (err) { |
| 81 | + console.log("error: ", err); |
| 82 | + result(null, err); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (res.affectedRows == 0) { |
| 87 | + // not found Customer with the id |
| 88 | + result({ kind: "not_found" }, null); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + console.log("deleted customer with id: ", id); |
| 93 | + result(null, res); |
| 94 | + }); |
| 95 | +}; |
| 96 | + |
| 97 | +Customer.removeAll = result => { |
| 98 | + sql.query("DELETE FROM customers", (err, res) => { |
| 99 | + if (err) { |
| 100 | + console.log("error: ", err); |
| 101 | + result(null, err); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + console.log(`deleted ${res.affectedRows} customers`); |
| 106 | + result(null, res); |
| 107 | + }); |
| 108 | +}; |
| 109 | + |
| 110 | +module.exports = Customer; |
0 commit comments