-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes-controller.js
More file actions
82 lines (59 loc) · 2.36 KB
/
notes-controller.js
File metadata and controls
82 lines (59 loc) · 2.36 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
const Note = require("../models/note-model");
// --------------------------------------------------- //
// --------------------- CREATE --------------------- //
// --------------------------------------------------- //
const agregarNota = async( req, res ) => {
const { title, description } = req.body;
const errors = [];
if ( !title ) {
errors.push({text: 'Por favor, agregue un título'});
}
if ( !description ) {
errors.push({text: 'Por favor, agregue una descripción'});
}
if ( errors.length > 0 ){
res.render('users/lobby', { errors });
} else {
const newNote = new Note({ title, description });
newNote.user = req.user.id;
await newNote.save();
console.log(`\n${req.user.username} ha agregado una nueva nota:\n${newNote}`);
req.flash('succes_msg', 'Nota agregada exitosamente');
res.redirect('/lobby');
};
};
// --------------------------------------------------- //
// ---------------------- READ ---------------------- //
// --------------------------------------------------- //
const mostrarNotas = async( req, res ) => {
const notes = await Note.find({ user: req.user.id }).sort({date: "-1"});
res.render("notes/all-notes", { notes });
};
// --------------------------------------------------- //
// --------------------- UPDATE --------------------- //
// --------------------------------------------------- //
const formularioEditarNota = async( req, res) => {
const note = await Note.findById( req.params.id )
res.render( 'notes/edit-note', { note } );
}
const editarNota = async( req, res) => {
const { title, description } = req.body;
notaCambiada = await Note.findByIdAndUpdate( req.params.id, { title, description });
req.flash("success_msg", "Nota actualizada, satisfactoriamente.");
res.redirect("/notes/mostrarNotas");
};
// --------------------------------------------------- //
// --------------------- DELETE --------------------- //
// --------------------------------------------------- //
const eliminarNota = async( req, res ) => {
await Note.findByIdAndDelete(req.params.id);
req.flash("success_msg", "Nota eliminada, satisfactoriamente.");
res.redirect('/notes/mostrarNotas');
};
module.exports = {
agregarNota,
mostrarNotas,
formularioEditarNota,
editarNota,
eliminarNota,
};