This repository was archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomers.coffee
113 lines (76 loc) · 2.73 KB
/
customers.coffee
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
111
112
113
##
# CRUD methods for our customers.
#
# @author: Daniele Gazzelloni <[email protected]>
######################################################################
db = require('./db')
logger = require('./logger')
# Insert a new customer into its Mongodb collection
insertCustomer = (customer, callback) ->
thisCustomer = new db.models.Customers(customer)
thisCustomer.save customer, (error, model) ->
error = (error ? error : 200)
model = (model ? model : {})
callback(error, model)
# Retrieve a customer by id
getCustomer = (id, callback) ->
db.models.Customers.findOne {_id: id}, (error, model) ->
error = (error ? error : 200)
model = (model ? model : {})
callback(error, model)
# Edit a customer
editCustomer = (customer, callback) ->
db.models.Customers.update {_id: customer._id}, customer, {}, (error, model) ->
error = (error ? error : 200)
model = (model ? model : {})
callback(error, model)
# Identify which CRUD operation we should do.
#
# Note: we should implement processVerb() in every macro-module, like
# one for customers, one for users, one for moduleXXX, etc..
# This way we could use the same method syntax in the entire app.
processVerb = (req, verb, callback) ->
# First discrimination
if verb is "GET"
# GET a customer
if req.query.id
getCustomer req.query.id, (error, result) ->
logger.log "*", "getting customer with id=#{req.query.id}";
callback(error, result)
else
logger.log "*", "ERROR: getting customer with id undefined.";
callback(400, {})
else if verb is "POST"
# INSERT a customer
if (req.body.id is null or req.body.id is undefined) and req.body.name and req.body.email
customer = {
name : req.body.name,
email : req.body.email,
barber: req.body.barber
}
insertCustomer customer, (error, result) ->
logger.log "*", "customer #{customer.name} inserted.";
callback(error, result)
# EDIT a customer
else if req.body.id and req.body.name and req.body.email
customer = {
_id : req.body.id,
name : req.body.name,
email : req.body.email,
barber: req.body.barber
}
editCustomer customer, (error, result) ->
logger.log "*", "customer #{customer.name} edited.";
callback(error, result)
else
logger.log "*", "ERROR: wrong parameters for editCustomer/insertCustomer...";
callback(400, {})
# Unrecognized method
else
logger.log "*", "WARNING: unrecognized method...";
callback(400, {})
# Module exports
exports.insertCustomer = insertCustomer
exports.getCustomer = getCustomer
exports.editCustomer = editCustomer
exports.processVerb = processVerb