-
Notifications
You must be signed in to change notification settings - Fork 0
/
rideController.js
80 lines (79 loc) · 2.11 KB
/
rideController.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
// Import contact model
Ride = require('./rideModel');
// Handle index actions
exports.index = function (req, res) {
Ride.get(function (err, rides) {
if (err) {
res.json({
status: "error",
message: err,
});
}
res.json({
status: "success",
message: "Rides retrieved successfully",
data: rides
});
});
};
// Handle create contact actions
exports.new = function (req, res) {
var ride = new Ride();
ride.name = req.body.name ? req.body.name : ride.name;
ride.location = req.body.location;
// save the ride and check for errors
ride.save(function (err) {
// if (err)
// res.json(err);
res.json({
message: 'New ride created!',
data: ride
});
});
};
// Handle view contact info
exports.view = function (req, res) {
Ride.findById(req.params.ride_id, function (err, ride) {
if (err)
res.send(err);
res.json({
message: 'OK',
data: ride
});
});
};
// Handle update contact info
exports.update = function (req, res) {
Ride.findById(req.params.ride_id, function (err, ride) {
if (err)
res.send(err);
ride.name = req.body.name ? req.body.name : ride.name;
ride.gen_id = ride.name;
ride.location = req.body.location ? req.body.location : ride.location;
console.log("Received PUT on id:" + req.params.ride_id);
console.log(" -> with Body location: "+ req.body.location);
// save the ride and check for errors
ride.save(function (err) {
if (err)
res.json(err);
else
res.json({
message: 'Ride Info updated',
data: ride
});
});
});
};
// Handle delete contact
exports.delete = function (req, res) {
Ride.remove({
_id: req.params.ride_id
}, function (err, contact) {
if (err)
res.send(err);
res.json({
status: "success",
message: 'Ride deleted'
});
});
};