-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathnotesController.js
More file actions
123 lines (95 loc) · 3.26 KB
/
notesController.js
File metadata and controls
123 lines (95 loc) · 3.26 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
119
120
121
122
123
const Note = require('../models/Note')
const User = require('../models/User')
const asyncHandler = require('express-async-handler')
// @desc Get all notes
// @route GET /notes
// @access Private
const getAllNotes = asyncHandler(async (req, res) => {
// Get all notes from MongoDB
const notes = await Note.find().lean()
// If no notes
if (!notes?.length) {
return res.status(400).json({ message: 'No notes found' })
}
// Add username to each note before sending the response
// See Promise.all with map() here: https://youtu.be/4lqJBBEpjRE
// You could also do this with a for...of loop
const notesWithUser = await Promise.all(notes.map(async (note) => {
const user = await User.findById(note.user).lean().exec()
return { ...note, username: user.username }
}))
res.json(notesWithUser)
})
// @desc Create new note
// @route POST /notes
// @access Private
const createNewNote = asyncHandler(async (req, res) => {
const { user, title, text } = req.body
// Confirm data
if (!user || !title || !text) {
return res.status(400).json({ message: 'All fields are required' })
}
// Check for duplicate title
const duplicate = await Note.findOne({ title }).lean().exec()
if (duplicate) {
return res.status(409).json({ message: 'Duplicate note title' })
}
// Create and store the new user
const note = await Note.create({ user, title, text })
if (note) { // Created
return res.status(201).json({ message: 'New note created' })
} else {
return res.status(400).json({ message: 'Invalid note data received' })
}
})
// @desc Update a note
// @route PATCH /notes
// @access Private
const updateNote = asyncHandler(async (req, res) => {
const { id, user, title, text, completed } = req.body
// Confirm data
if (!id || !user || !title || !text || typeof completed !== 'boolean') {
return res.status(400).json({ message: 'All fields are required' })
}
// Confirm note exists to update
const note = await Note.findById(id).exec()
if (!note) {
return res.status(400).json({ message: 'Note not found' })
}
// Check for duplicate title
const duplicate = await Note.findOne({ title }).lean().exec()
// Allow renaming of the original note
if (duplicate && duplicate?._id.toString() !== id) {
return res.status(409).json({ message: 'Duplicate note title' })
}
note.user = user
note.title = title
note.text = text
note.completed = completed
const updatedNote = await note.save()
res.json(`'${updatedNote.title}' updated`)
})
// @desc Delete a note
// @route DELETE /notes
// @access Private
const deleteNote = asyncHandler(async (req, res) => {
const { id } = req.body
// Confirm data
if (!id) {
return res.status(400).json({ message: 'Note ID required' })
}
// Confirm note exists to delete
const note = await Note.findById(id).exec()
if (!note) {
return res.status(400).json({ message: 'Note not found' })
}
const result = await note.deleteOne()
const reply = `Note '${result.title}' with ID ${result._id} deleted`
res.json(reply)
})
module.exports = {
getAllNotes,
createNewNote,
updateNote,
deleteNote
}