Extended Database

This commit is contained in:
マリウス 2020-10-11 20:36:08 +01:00
parent 566d138f00
commit 3805f249ca
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F

View file

@ -32,7 +32,7 @@ func (database *Database) NewID() (string) {
return id.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() id := database.NewID()
entryJson, jsonerr := json.Marshal(entry) 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 { dberr := database.DB.Update(func(tx *buntdb.Tx) error {
_, _, seterr := tx.Set(id, string(entryJson), nil) if setRunning == true {
return seterr _, _, 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 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) { func (database *Database) ListEntries() ([]Entry, error) {
var entries []Entry var entries []Entry
dberr := database.DB.View(func(tx *buntdb.Tx) error { 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 var entry Entry
json.Unmarshal([]byte(value), &entry) json.Unmarshal([]byte(value), &entry)