-
Notifications
You must be signed in to change notification settings - Fork 2
/
users.js
57 lines (50 loc) · 1.39 KB
/
users.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
const User = require('../models/user')
const Book = require('../models/book')
function usersAll(req, res) {
User
.find()
.then(users => res.json(users))
.catch(err => res.json(err))
}
function userShow(req, res) {
User
.findById(req.params.id)
.then(user => res.status(200).json(user))
.catch(err => res.json(err))
}
function userUpdate(req, res) {
User
.findById(req.params.id)
.then(user => {
Object.assign(user, req.body)
return user.save()
})
.then(user => res.status(200).json(user))
.catch(err => res.status(500).json(err))
}
function userDelete(req, res) {
const promiseArray = [
Book
.remove({owner: req.params.id}),
User
.findByIdAndRemove(req.params.id)
.exec()
]
Promise.all(promiseArray)
.then(() => res.sendStatus(204))
.catch(err => res.status(500).json(err))
}
function librariesAll(req, res) {
User
.find()
.populate('booksOwned')
.then(libraries => res.json(libraries.map(library => ({ libraryName: library.libraryName, libraryDescription: library.libraryDescription, libraryPicture: library.libraryPicture, location: library.location, books: library.booksOwned, owner: library._id }))))
.catch(err => res.status(404).json(err))
}
module.exports = {
usersAll: usersAll,
userShow: userShow,
userUpdate: userUpdate,
userDelete: userDelete,
librariesAll: librariesAll
}