-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
110 lines (99 loc) · 2.53 KB
/
logic.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
const mongoose = require('mongoose')
const assert = require('assert')
mongoose.Promise = global.Promise
mongoose.connect('mongodb://localhost:27017/contact-manager', { useNewUrlParser: true }, (err, database) => {
assert.equal(null, err)
})
const db = mongoose.connection
// Converts value to lowercase
function toLower(v) {
return v.toLowerCase()
}
// Define a contact Schema
const contactSchema = mongoose.Schema({
firstname: { type: String, set: toLower },
lastname: { type: String, set: toLower },
phone: { type: String, set: toLower },
email: { type: String, set: toLower }
})
// Define model as an interface with the database
const Contact = mongoose.model('Contact', contactSchema)
/**
* @function [addContact]
* @returns {String} Status
*/
const addContact = (contact) => {
Contact.create(contact, (err) => {
assert.equal(null, err)
console.info('New contact added')
db.close()
})
}
/**
* @function [getContact]
* @returns {Json} contacts
*/
const getContact = (name) => {
// Define search criteria. The search here is case-insensitive and inexact.
const search = new RegExp(name, 'i')
Contact.find({$or: [{firstname: search}, {lastname: search}]})
.exec((err, contact) => {
assert.equal(null, err)
console.info(contact)
console.info(`${contact.length}`)
db.close()
})
}
/**
* @function [getContactList]
* @returns {Sting} status
*/
const updateContact = (_id, contact) => {
Contact.update({ _id }, contact)
.exec((err, status) => {
assert.equal(null, err)
console.info('Updated successfully')
db.close()
})
}
/**
* @function [deleteContact]
* @returns {String} status
*/
const deleteContact = (_id) => {
Contact.deleteOne({ _id })
.exec((err, status) => {
assert.equal(null, err)
console.info('Deleted successfully')
db.close()
})
}
/**
* @function [getContactList]
* @returns [contactlist] contacts
*/
const getContactList = () => {
Contact.find()
.exec((err, contacts) => {
assert.equal(null, err)
console.info(contacts)
console.info(`${contacts.length} matches`)
db.close()
})
}
/**
* @function [getContacts]
* @returns [contacts] contacts
*/
const getContacts = () => {
return Contact.find().exec()
}
// Export all methods
module.exports = {
addContact,
getContact,
getContacts,
getContactList,
updateContact,
deleteContact
}