-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththought-controller.js
134 lines (122 loc) · 4.03 KB
/
thought-controller.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const { User, Thought } = require('../models');
const thoughtController = {
// get all thoughts
getAllThoughts(req, res) {
Thought.find({})
.populate({
path: 'user',
select: '-__v'
})
.select('-__v')
.then(dbThoughtData => res.json(dbThoughtData))
.catch(err => {
console.log(err);
res.status(400).json(err);
});
},
// get one thought by id
getThoughtById({ params }, res) {
Thought.findOne({ _id: params.id })
.populate({
path: 'user',
select: '-__v'
})
.select('-__v')
.then(dbThoughtData => {
// If no Thought is found, send 404
if (!dbThoughtData) {
res.status(404).json({ message: 'No Thought found with this id!' });
return;
}
res.json(dbThoughtData);
})
.catch(err => {
console.log(err);
res.status(400).json(err);
});
},
// createThought
createThought({ body }, res) {
Thought.create(body)
.then(({ _id}) => {
return User.findOneAndUpdate(
{ username: body.username },
{ $push: { thoughts: _id }},
{ new: true}
);
})
.then(dbUserData => {
if (!dbUserData) {
res.status(404).json({ message: 'No User found with this username!'});
return;
}
res.json(dbUserData);
})
.catch(err => res.json(err));
},
// update thought by id
updateThought({ params, body }, res) {
Thought.findOneAndUpdate({ _id: params.id }, body, { new: true, runValidators: true })
.then(dbThoughtData => {
if (!dbThoughtData) {
res.status(404).json({ message: 'No thought found with this id!' });
return;
}
res.json(dbThoughtData);
})
.catch(err => res.status(400).json(err));
},
// delete thought
deleteThought({ params }, res) {
Thought.findOneAndDelete({ _id: params.id })
.then(dbThoughtData => {
if (!dbThoughtData) {
res.status(404).json({ message: 'No thought found with this id!' });
return;
}
res.json(dbThoughtData);
})
.catch(err => res.status(400).json(err));
},
// create reaction in a single thought
createReaction({ params, body }, res) {
Thought.findOneAndUpdate(
{ _id: params.thoughtId },
{ $push: { reactions: body }},
{ new: true, runValidators: true }
)
.then(dbThoughtData => {
if (!dbThoughtData) {
res.status(404).json({ message: 'No thought found with this id!'});
return;
}
res.json(dbThoughtData);
})
.catch(err => res.json(err));
},
// delete reaction
deleteReaction({ params }, res) {
Thought.findOneAndUpdate(
{ _id: params.thoughtId },
{ $pull: { reactions: { reactionId: params.reactionId } } },
{ new: true }
)
.then(dbThoughtData => res.json(dbThoughtData))
.catch(err => res.json(err));
},
// get all reactions
getAllReactions(req, res) {
Thought.find({})
.populate({
path: 'thoughts',
select: '-__v'
})
.select('-__v')
.then(dbThoughtData => res.json(dbThoughtData))
.catch(err => {
console.log(err);
res.status(400).json(err);
});
}
}
module.exports = thoughtController;