From 1e053c11236fc045938e98061af4209eaa2e72aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Fri, 16 Oct 2020 00:58:56 +0100 Subject: [PATCH] Implemented GetImportsSHA1List & UpdateImportsSHA1List --- z/database.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/z/database.go b/z/database.go index a8776f8..e8f04e0 100644 --- a/z/database.go +++ b/z/database.go @@ -3,6 +3,7 @@ package z import ( "os" "sort" + "strings" "log" "errors" "encoding/json" @@ -151,3 +152,48 @@ func (database *Database) ListEntries(user string) ([]Entry, error) { sort.Slice(entries, func(i, j int) bool { return entries[i].Begin.Before(entries[j].Begin) }) return entries, dberr } + +func (database *Database) GetImportsSHA1List(user string) (map[string]string, error) { + var sha1List = make(map[string]string) + + dberr := database.DB.View(func(tx *buntdb.Tx) error { + value, err := tx.Get(user + ":imports:sha1", false) + if err != nil { + return nil + } + + sha1Entries := strings.Split(value, ",") + + for _, sha1Entry := range sha1Entries { + sha1EntrySplit := strings.Split(sha1Entry, ":") + sha1 := sha1EntrySplit[0] + id := sha1EntrySplit[1] + sha1List[sha1] = id + } + + return nil + }) + + return sha1List, dberr +} + +func (database *Database) UpdateImportsSHA1List(user string, sha1List map[string]string) (error) { + var sha1Entries []string + + for sha1, id := range sha1List { + sha1Entries = append(sha1Entries, sha1 + ":" + id) + } + + value := strings.Join(sha1Entries, ",") + + dberr := database.DB.Update(func(tx *buntdb.Tx) error { + _, _, seterr := tx.Set(user + ":imports:sha1", value, nil) + if seterr != nil { + return seterr + } + + return nil + }) + + return dberr +}