From 3805f249ca595fe47e22c28588a2321bf3697c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sun, 11 Oct 2020 20:36:08 +0100 Subject: [PATCH] Extended Database --- z/database.go | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/z/database.go b/z/database.go index 1e8fe4b..f0de35d 100644 --- a/z/database.go +++ b/z/database.go @@ -32,7 +32,7 @@ func (database *Database) NewID() (string) { return id.String() } -func (database *Database) AddEntry(entry Entry) (string, error) { +func (database *Database) AddEntry(entry Entry, setRunning bool) (string, error) { id := database.NewID() entryJson, jsonerr := json.Marshal(entry) @@ -41,18 +41,58 @@ func (database *Database) AddEntry(entry Entry) (string, error) { } dberr := database.DB.Update(func(tx *buntdb.Tx) error { - _, _, seterr := tx.Set(id, string(entryJson), nil) - return seterr + if setRunning == true { + _, _, seterr := tx.Set(entry.User + ":status:running", id, nil) + if seterr != nil { + return seterr + } + } + _, _, seterr := tx.Set(entry.User + ":entry:" + id, string(entryJson), nil) + if seterr != nil { + return seterr + } + + return nil }) return id, dberr } +func (database *Database) AddRunningEntryId(user string, id string) (string, error) { + var runningId string = "" + + dberr := database.DB.View(func(tx *buntdb.Tx) error { + tx.AscendKeys(user + ":running", func(key, value string) bool { + runningId = value + return true + }) + + return nil + }) + + return runningId, dberr +} + +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 +} + func (database *Database) ListEntries() ([]Entry, error) { var entries []Entry dberr := database.DB.View(func(tx *buntdb.Tx) error { - tx.Ascend("task", func(key, value string) bool { + tx.AscendKeys("*:entry:*", func(key, value string) bool { var entry Entry json.Unmarshal([]byte(value), &entry)