2020-10-11 14:12:48 +00:00
|
|
|
package z
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-10-11 20:13:56 +00:00
|
|
|
"errors"
|
2020-10-11 14:12:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/tidwall/buntdb"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
DB *buntdb.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitDatabase() (*Database, error) {
|
|
|
|
db, err := buntdb.Open(":memory:") // TODO: Replace with file
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.CreateIndex("task", "*", buntdb.IndexJSON("task"))
|
|
|
|
db.CreateIndex("project", "*", buntdb.IndexJSON("project"))
|
|
|
|
|
|
|
|
database := Database{db}
|
|
|
|
return &database, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (database *Database) NewID() (string) {
|
|
|
|
id, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Could not generate UUID: %+v", err)
|
|
|
|
}
|
|
|
|
return id.String()
|
|
|
|
}
|
|
|
|
|
2020-10-11 20:13:56 +00:00
|
|
|
func (database *Database) AddEntry(user string, entry Entry, setRunning bool) (string, error) {
|
2020-10-11 14:12:48 +00:00
|
|
|
id := database.NewID()
|
|
|
|
|
|
|
|
entryJson, jsonerr := json.Marshal(entry)
|
|
|
|
if jsonerr != nil {
|
|
|
|
return id, jsonerr
|
|
|
|
}
|
|
|
|
|
|
|
|
dberr := database.DB.Update(func(tx *buntdb.Tx) error {
|
2020-10-11 19:36:08 +00:00
|
|
|
if setRunning == true {
|
2020-10-11 20:13:56 +00:00
|
|
|
_, _, seterr := tx.Set(user + ":status:running", id, nil)
|
2020-10-11 19:36:08 +00:00
|
|
|
if seterr != nil {
|
|
|
|
return seterr
|
|
|
|
}
|
|
|
|
}
|
2020-10-11 20:13:56 +00:00
|
|
|
_, _, seterr := tx.Set(user + ":entry:" + id, string(entryJson), nil)
|
2020-10-11 19:36:08 +00:00
|
|
|
if seterr != nil {
|
|
|
|
return seterr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-10-11 14:12:48 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return id, dberr
|
|
|
|
}
|
|
|
|
|
2020-10-11 20:13:56 +00:00
|
|
|
func (database *Database) GetEntry(user string, entryId string) (Entry, error) {
|
|
|
|
var entry Entry
|
2020-10-11 19:36:08 +00:00
|
|
|
|
|
|
|
dberr := database.DB.View(func(tx *buntdb.Tx) error {
|
2020-10-11 20:13:56 +00:00
|
|
|
tx.AscendKeys(user + ":entry:" + entryId, func(key, value string) bool {
|
|
|
|
json.Unmarshal([]byte(value), &entry)
|
|
|
|
entry.ID = key
|
2020-10-11 19:36:08 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-10-11 20:13:56 +00:00
|
|
|
return entry, dberr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (database *Database) FinishEntry(user string, entry Entry) (string, error) {
|
|
|
|
entryJson, jsonerr := json.Marshal(entry)
|
|
|
|
if jsonerr != nil {
|
|
|
|
return entry.ID, jsonerr
|
|
|
|
}
|
|
|
|
|
|
|
|
dberr := database.DB.Update(func(tx *buntdb.Tx) error {
|
|
|
|
runningEntryId, grerr := tx.Get(user + ":status:running")
|
|
|
|
if grerr != nil {
|
|
|
|
return errors.New("No currently running entry found!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if runningEntryId != entry.ID {
|
|
|
|
return errors.New("Specified entry is not currently running!")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, srerr := tx.Set(user + ":status:running", entry.ID, nil)
|
|
|
|
if srerr != nil {
|
|
|
|
return srerr
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, seerr := tx.Set(user + ":entry:" + entry.ID, string(entryJson), nil)
|
|
|
|
if seerr != nil {
|
|
|
|
return seerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return entry.ID, dberr
|
2020-10-11 19:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (database *Database) GetRunningEntryId(user string) (string, error) {
|
|
|
|
var runningId string = ""
|
|
|
|
|
|
|
|
dberr := database.DB.View(func(tx *buntdb.Tx) error {
|
|
|
|
tx.AscendKeys(user + ":status:running", func(key, value string) bool {
|
|
|
|
runningId = value
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return runningId, dberr
|
|
|
|
}
|
|
|
|
|
2020-10-11 14:12:48 +00:00
|
|
|
func (database *Database) ListEntries() ([]Entry, error) {
|
|
|
|
var entries []Entry
|
|
|
|
|
|
|
|
dberr := database.DB.View(func(tx *buntdb.Tx) error {
|
2020-10-11 19:36:08 +00:00
|
|
|
tx.AscendKeys("*:entry:*", func(key, value string) bool {
|
2020-10-11 14:12:48 +00:00
|
|
|
var entry Entry
|
|
|
|
json.Unmarshal([]byte(value), &entry)
|
|
|
|
|
|
|
|
entry.ID = key
|
|
|
|
|
|
|
|
entries = append(entries, entry)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return entries, dberr
|
|
|
|
}
|