-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes-service.js
More file actions
93 lines (72 loc) · 1.91 KB
/
notes-service.js
File metadata and controls
93 lines (72 loc) · 1.91 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
let database = {};
//=====================================================
// POST
// Creating a new note
async function createNewNote(req) {
//generating new id
const maxSchemas = Object.keys(database).length;
const id = maxSchemas === 0 ? 0 : maxSchemas;
// getting current data and time
const date = String(new Date());
const idString = id.toString();
// Add note to our database
database[idString] = {
_id: id,
date: date,
notes: req.body.notes,
};
// Saving our new note
const result = {
message: "You have added a new note ( ´∀`)b",
id,
};
return result;
}
//=====================================================
// GET
async function getAllNotes() {
//return await Note.find({});
}
// Function to find a note given an Id + error catch it
// side note: not for notes that doesn't exist since that returns a null
async function getById(res, id) {
// adding a try catch to test if a note, given an Id, exists
try {
return database[id.toString()]; //Note.findById(id).exec();
} catch (error) {
return res
.status(500)
.send(
"An error occurred with when trying to finding this note by it's ID \n (this is possibly a server error and not that the note doesn't exits) ఠ్ఠᗣఠ్ఠ )"
);
}
}
//=====================================================
// PATCH
async function updateById(id, notes) {
// getting current data and time
const date = String(new Date());
const idString = id.toString();
return (database[idString] = {
_id: id,
date: date,
notes: notes,
});
}
//=====================================================
// DELETE
async function delById(idAsString) {
delete database[idAsString];
}
async function deleteAllNotes() {
database = {};
}
//-----------------------------------------------
module.exports = {
createNewNote,
getAllNotes,
getById,
updateById,
delById,
deleteAllNotes,
};