forked from Minecraft/canopeas
34 lines
822 B
Go
34 lines
822 B
Go
|
package database
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"git.jojodev.com/Minecraft/canopeas/discord/track"
|
||
|
"go.etcd.io/bbolt"
|
||
|
)
|
||
|
|
||
|
// LoadTracker loads the tracks from the database
|
||
|
func (db *Database) LoadTracker() (track.Tracker, error) {
|
||
|
t := track.New()
|
||
|
return t, db.db.View(func(tx *bbolt.Tx) error {
|
||
|
return tx.Bucket(trackBucket).ForEach(func(k, v []byte) error {
|
||
|
var users map[string]struct{}
|
||
|
if err := json.Unmarshal(v, &users); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
t[string(k)] = users
|
||
|
return nil
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// SetTrack sets a list of trackers for a word
|
||
|
func (db *Database) SetTrack(word string, trackers map[string]struct{}) error {
|
||
|
return db.db.Update(func(tx *bbolt.Tx) error {
|
||
|
v, err := json.Marshal(trackers)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return tx.Bucket(trackBucket).Put([]byte(word), v)
|
||
|
})
|
||
|
}
|