-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
118 lines (89 loc) · 3.55 KB
/
router.js
File metadata and controls
118 lines (89 loc) · 3.55 KB
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
114
115
116
117
118
const express = require("express");
const router = express.Router();
//importing controllers to use it with express routers
const ride = require('./models/Ride.js');
const user = require('./models/User.js')
const controllers = require('./controllers/index.js');
router.get("/", function(request, response) {
response.render("home", {
title: "Flash Rides - Flash speed commuting service.",
layout: "guest.hbs"
});
});
router.get("/dashboard", function(request, response) {
response.render("dashboard", {
title: "User Dashboard",
page_name: "dashboard",
layout: "user.hbs"
});
});
router.get("/dashboard/rides", function(request, response) {
response.render("rides", {
title: "User Rides",
layout: "user.hbs",
page_name: "rides",
});
});
router.get("/dashboard/billing", function(request, response) {
response.render("billing", {
title: "User Billing",
layout: "user.hbs",
page_name: "billing"
});
});
router.get("/dashboard/profile", function(request, response) {
response.render("profile", {
title: "User Support",
layout: "user.hbs",
page_name: "profile"
});
});
router.get("/dashboard/support", function(request, response) {
response.render("support", {
title: "User Support",
layout: "user.hbs",
page_name: "support"
});
});
router.post('/signup', controllers.UserController.register);
router.post('/login', controllers.UserController.login);
router.post("/logout", controllers.UserController.logout);
/**<=========post-route-to-create-ride-respect-user-start=================>*/
// Route for creating a new Ride and updating user "rides" field with it
router.post("/user/:id", function (req, res) {
// Create a new note and pass the req.body to the entry
ride.create(req.body)
.then(function (dbRide) {
// If a Ride was created successfully, find one Product with an `_id` equal to `req.params.id`. Update the user to be associated with the new Ride
// { new: true } tells the query that we want it to return the updated user -- it returns the original by default
// Since our mongoose query returns a promise, we can chain another `.then` which receives the result of the query
return user.findOneAndUpdate({ _id: req.params.id }, { $push: { rides: dbRide._id } }, { new: true });
})
.then(function (dbUser) {
// If we were able to successfully update a user, send it back to the client
res.json(dbUser);
})
.catch(function (err) {
// If an error occurred, send it to the client
res.json(err);
});
});
/**<=========post-route-to-create-ride-respect-user-end=================>*/
/**<================get-route-to-get-user-with-ride-details-start========================>*/
// Route for retrieving a Product by id and populating it's Review.
router.get("/user/:id", function(req, res) {
// Using the id passed in the id parameter, prepare a query that finds the matching one in our db...
user.findOne({ _id: req.params.id })
// ..and populate all of the notes associated with it
.populate('rides')
.then(function(dbUser) {
// If we were able to successfully find an user with the given id, send it back to the client
res.json(dbUser)
})
.catch(function(err) {
// If an error occurred, send it to the client
res.json(err);
});
});
/**<================Get-Route-To-Get-User-With-Ride-Details-End========================>*/
module.exports = router;