-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathprofile.js
45 lines (41 loc) · 1.17 KB
/
profile.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
const express = require("express");
const router = express.Router();
const User = require("../models/User");
const uploader = require("../helpers/multer");
const helpers = require("../helpers/function");
const Property = require("../models/Property");
router.get("/", helpers.isAuth, (req, res) => {
const { user } = req;
Property.find({ owner: user._id }).then(properties => {
res.render("profile", { user, properties });
});
});
router.get("/:id/edit", helpers.isAuth, (req, res) => {
const { id } = req.params;
User.findById(id)
.then(user => {
res.render("profile-form", { user });
})
.catch(err => {
res.render("profile-form", { err });
});
});
router.post(
"/:id/edit",
helpers.isAuth,
uploader.single("image"),
(req, res) => {
const { id: _id } = req.params;
const { email } = req.user;
const image = req.file ? req.file.url : undefined;
const user = image ? { ...req.body, image } : req.body;
User.findOneAndUpdate({ _id, email }, { $set: user })
.then(() => {
res.redirect("/profile");
})
.catch(err => {
res.render("profile-form", { err });
});
}
);
module.exports = router;