mvp
This commit is contained in:
114
db/db.go
Normal file
114
db/db.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type DownloadStatus string
|
||||
|
||||
var (
|
||||
NotStarted DownloadStatus = "NotStarted"
|
||||
InProgress DownloadStatus = "InProgress"
|
||||
Done DownloadStatus = "Done"
|
||||
Error DownloadStatus = "Error"
|
||||
)
|
||||
|
||||
type Item struct {
|
||||
Id int `json:"id"`
|
||||
Date string `json:"date"`
|
||||
URL string `json:"url"`
|
||||
Title string `json:"title"`
|
||||
Approved bool `json:"approved"`
|
||||
Status DownloadStatus `json:"status"`
|
||||
FileName string `json:"file_name"`
|
||||
Progress string `json:"-"`
|
||||
}
|
||||
|
||||
type Jdb struct {
|
||||
Items []Item `json:"items"`
|
||||
}
|
||||
|
||||
type Db struct {
|
||||
items []Item
|
||||
mutex sync.Mutex
|
||||
lastId int
|
||||
path string
|
||||
}
|
||||
|
||||
func (d *Db) Add(i Item) int {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
i.Id = d.lastId
|
||||
d.lastId++
|
||||
d.items = append(d.items, i)
|
||||
d.save()
|
||||
return i.Id
|
||||
}
|
||||
|
||||
func (d *Db) Update(id int, persist bool, f func(*Item)) error {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
for i, _ := range d.items {
|
||||
if d.items[i].Id == id {
|
||||
f(&d.items[i])
|
||||
if persist {
|
||||
d.save()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Invalid id: %d", id)
|
||||
}
|
||||
|
||||
func (d *Db) save() error {
|
||||
data, err := json.Marshal(Jdb{d.items})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(d.path, data, 0644)
|
||||
}
|
||||
|
||||
func (d *Db) Run(f func(*Jdb)) {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
f(&Jdb{d.items})
|
||||
}
|
||||
|
||||
func (d *Db) Save() error {
|
||||
d.mutex.Lock()
|
||||
defer d.mutex.Unlock()
|
||||
return d.save()
|
||||
}
|
||||
|
||||
func Load(path string) (*Db, error) {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return &Db{
|
||||
path: path,
|
||||
lastId: 0,
|
||||
}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
var jd Jdb
|
||||
err = json.Unmarshal(data, &jd)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := 0
|
||||
for _, item := range jd.Items {
|
||||
if item.Id > m {
|
||||
m = item.Id
|
||||
}
|
||||
}
|
||||
return &Db{
|
||||
items: jd.Items,
|
||||
path: path,
|
||||
lastId: m + 1,
|
||||
}, nil
|
||||
}
|
Reference in New Issue
Block a user