Extended Database

This commit is contained in:
マリウス 2020-10-11 21:13:56 +01:00
parent e1ccf487a7
commit 44cc4e00f2
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F

View file

@ -2,6 +2,7 @@ package z
import ( import (
"log" "log"
"errors"
"encoding/json" "encoding/json"
"github.com/tidwall/buntdb" "github.com/tidwall/buntdb"
"github.com/google/uuid" "github.com/google/uuid"
@ -32,7 +33,7 @@ func (database *Database) NewID() (string) {
return id.String() return id.String()
} }
func (database *Database) AddEntry(entry Entry, setRunning bool) (string, error) { func (database *Database) AddEntry(user string, entry Entry, setRunning bool) (string, error) {
id := database.NewID() id := database.NewID()
entryJson, jsonerr := json.Marshal(entry) entryJson, jsonerr := json.Marshal(entry)
@ -42,12 +43,12 @@ func (database *Database) AddEntry(entry Entry, setRunning bool) (string, error)
dberr := database.DB.Update(func(tx *buntdb.Tx) error { dberr := database.DB.Update(func(tx *buntdb.Tx) error {
if setRunning == true { if setRunning == true {
_, _, seterr := tx.Set(entry.User + ":status:running", id, nil) _, _, seterr := tx.Set(user + ":status:running", id, nil)
if seterr != nil { if seterr != nil {
return seterr return seterr
} }
} }
_, _, seterr := tx.Set(entry.User + ":entry:" + id, string(entryJson), nil) _, _, seterr := tx.Set(user + ":entry:" + id, string(entryJson), nil)
if seterr != nil { if seterr != nil {
return seterr return seterr
} }
@ -58,19 +59,52 @@ func (database *Database) AddEntry(entry Entry, setRunning bool) (string, error)
return id, dberr return id, dberr
} }
func (database *Database) AddRunningEntryId(user string, id string) (string, error) { func (database *Database) GetEntry(user string, entryId string) (Entry, error) {
var runningId string = "" var entry Entry
dberr := database.DB.View(func(tx *buntdb.Tx) error { dberr := database.DB.View(func(tx *buntdb.Tx) error {
tx.AscendKeys(user + ":running", func(key, value string) bool { tx.AscendKeys(user + ":entry:" + entryId, func(key, value string) bool {
runningId = value json.Unmarshal([]byte(value), &entry)
entry.ID = key
return true return true
}) })
return nil return nil
}) })
return runningId, dberr 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
} }
func (database *Database) GetRunningEntryId(user string) (string, error) { func (database *Database) GetRunningEntryId(user string) (string, error) {