-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.js
69 lines (60 loc) · 1.86 KB
/
commands.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
const connection = require("./config/connection");
const mongoose = require("mongoose");
const data = require("./config/commands");
//Add a customer
const addcustomer = (customer) => {
data.create(customer)
.then(() => {
console.log("Customer created");
process.exit()
})
.catch(err => { console.log({ error: err.message }); process.exit(); })
}
//List all customers
const listcustomers = () => {
data.find()
.select("-__v")
.then(customers => {
console.log(customers);
console.log(`Total Customers: ${customers.length}`);
process.exit();
})
.catch(err => { console.log({ error: err.message }); process.exit(); })
}
//To find a customer
const findcustomer = (name) => {
let search = new RegExp(name, 'i');
data.find({ $or: [{ firstname: search }, { lastname: search }] })
.select("firstname lastname phone email")
.then(customers => {
console.log(customers);
console.log(`Total matches: ${customers.length}`);
process.exit();
})
.catch(err => { console.log({ error: err.message }); process.exit(); })
}
//Update a customer
const updatecustomer = (_id, new_customer) => {
data.updateOne({ _id: _id }, new_customer)
.then(() => {
console.log("Customer Updated");
process.exit();
})
.catch(err => { console.log({ error: err.message }); process.exit(); })
}
//Remove a customer
const removecustomer = (_id) => {
data.deleteOne({ _id: _id })
.then(() => {
console.log("Customer Removed");
process.exit();
})
.catch(err => { console.log({ error: err.message }); process.exit(); });
}
module.exports = {
findcustomer,
addcustomer,
updatecustomer,
listcustomers,
removecustomer
}