From 219695f79530591795dcc21ffc8f08ecc3ccd60a Mon Sep 17 00:00:00 2001 From: Miltiadis Kallianis Date: Fri, 2 Sep 2022 13:18:58 +0200 Subject: [PATCH] cleanUp: use tx.Get for getting values from DB tx.AscendKeys can be used for collecting multiple keys, values. In our case we only collect one key, value therefore use tx.Get --- z/database.go | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/z/database.go b/z/database.go index 5dc2806..bd73f76 100644 --- a/z/database.go +++ b/z/database.go @@ -1,14 +1,15 @@ package z import ( - "os" - "sort" - "strings" - "log" - "errors" - "encoding/json" - "github.com/tidwall/buntdb" - "github.com/google/uuid" + "encoding/json" + "errors" + "log" + "os" + "sort" + "strings" + + "github.com/google/uuid" + "github.com/tidwall/buntdb" ) type Database struct { @@ -68,21 +69,20 @@ func (database *Database) AddEntry(user string, entry Entry, setRunning bool) (s } func (database *Database) GetEntry(user string, entryId string) (Entry, error) { - var entry Entry + var entry Entry - dberr := database.DB.View(func(tx *buntdb.Tx) error { - tx.AscendKeys(user + ":entry:" + entryId, func(key, value string) bool { - json.Unmarshal([]byte(value), &entry) + dberr := database.DB.View(func(tx *buntdb.Tx) error { + value, err := tx.Get(user + ":entry:" + entryId) + if err != nil { + return err + } + json.Unmarshal([]byte(value), &entry) - entry.SetIDFromDatabaseKey(key) + entry.ID = entryId + return nil + }) - return true - }) - - return nil - }) - - return entry, dberr + return entry, dberr } func (database *Database) UpdateEntry(user string, entry Entry) (string, error) { @@ -164,11 +164,11 @@ 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 - }) - + value, err := tx.Get(user + ":status:running") + if err != nil { + return err + } + runningId = value return nil })