-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notes.js
52 lines (43 loc) · 1.29 KB
/
Notes.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
import {writeFile, readFile} from "fs"
import {promisify} from "util"
import {Note} from "./Note"
const write = promisify(writeFile)
const read = promisify(readFile)
export class Notes {
constructor(fileName) {
this.notes = []
this.fileName = fileName
this.load()
}
load() {
read(this.fileName, 'utf8')
.then(data => JSON.parse(data))
.then(notesList => notesList
.forEach(note => this.add(new Note(note))))
}
getAll() {
return this.notes
}
getById(id) {
if (!this.notes[id - 1]) throw Error(`Cannot find a note with ${id}`)
return this.notes[id - 1]
}
add(note) {
if (!note instanceof (Note))
throw new Error('note is not instance of Note class')
this.notes.push(note)
}
save() {
write(this.fileName, JSON.stringify(this.notes, null, "\t"), "utf8")
}
edit(id, note_partial) {
if (!this.notes[id - 1]) throw Error(`Cannot find a note with ${id}`)
this.notes[id - 1] = {...this.notes[id - 1], ...note_partial}
return this.notes
}
delete(id) {
if (!this.notes[id - 1]) throw Error(`Cannot find a note with ${id}`)
this.notes.splice(id - 1, 1)
return this.notes
}
}