-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
69 lines (59 loc) · 1.1 KB
/
database.go
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
package main
import (
"encoding/json"
"io/ioutil"
"log"
)
// Entry is a single media title.
type Entry struct {
Title string
TitleSort string
State string
Year string
TagID string
Future string
Info string
Collection string
Rating int
EpisodeDone int
EpisodeTotal int
}
// Database is the entirety of all media titles.
type Database struct {
Movies []Entry
Series []Entry
Anime []Entry
Games []Entry
Books []Entry
}
// NewDatabase creates a new Database and returns it.
func newDatabase() *Database {
d := &Database{}
d.load()
d.save()
return d
}
// Load fetches the user's database from a file.
func (d *Database) load() {
path := databasePath()
cont, err := ioutil.ReadFile(path)
if err != nil {
return
}
err = json.Unmarshal(cont, d)
if err != nil {
log.Fatal(err)
}
}
// Save takes the database and saves it to a file.
func (d *Database) save() {
cont, err := json.Marshal(d)
if err != nil {
log.Fatal(err)
}
path := databasePath()
err = ioutil.WriteFile(path, cont, 0644)
if err != nil {
log.Print(err)
}
}