diff options
| author | Fuwn <[email protected]> | 2021-07-14 23:06:33 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-07-14 23:06:33 +0000 |
| commit | 2ead92e075ecdceec9be7c852ad318299bc8d87e (patch) | |
| tree | 00c85a647830929ce8b3993bfb48e4b2154baff7 /pkg | |
| parent | feat(space): :gemini: (diff) | |
| download | space-2ead92e075ecdceec9be7c852ad318299bc8d87e.tar.xz space-2ead92e075ecdceec9be7c852ad318299bc8d87e.zip | |
feat(database): persistant hits
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/database/database.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/pkg/database/database.go b/pkg/database/database.go new file mode 100644 index 0000000..9d14fa9 --- /dev/null +++ b/pkg/database/database.go @@ -0,0 +1,59 @@ +// Copyright (C) 2021-2021 Fuwn +// SPDX-License-Identifier: GPL-3.0-only + +package database + +import ( + "github.com/sonyarouje/simdb" +) + +var driver *simdb.Driver + +func init() { + var err error + driver, err = simdb.New(".space/database") + if err != nil { + panic(err) + } +} + +type Hit struct { + Path string `json:"path"` + Count int `json:"count"` +} + +func (c Hit) ID() (jsonField string, value interface{}) { + value = c.Path + jsonField = "path" + return +} + +func Get(path string) int { + var hit Hit + + err := driver.Open(Hit{}).Where("path", "=", path).First().AsEntity(&hit) + if err != nil { + return 0 + } + + return hit.Count +} + +func Create(path string) { + driver.Insert(Hit{ + Path: path, + Count: 0, + }) +} + +func Increment(path string) { + var hit Hit + + err := driver.Open(Hit{}).Where("path", "=", path).First().AsEntity(&hit) + if err != nil { + return + } + + hit.Count = hit.Count + 1 + driver.Update(hit) +} |