113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
package bdd
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/boltdb/bolt"
|
|
)
|
|
|
|
// OpenDB a key-value store.
|
|
func OpenDB() (*bolt.DB, error) {
|
|
db, err := bolt.Open("foods.db", 0600, nil)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not open db, %v", err)
|
|
}
|
|
err = db.Update(func(tx *bolt.Tx) error {
|
|
root, err := tx.CreateBucketIfNotExists([]byte("DB"))
|
|
if err != nil {
|
|
return fmt.Errorf("could not create root bucket: %v", err)
|
|
}
|
|
_, err = root.CreateBucketIfNotExists([]byte("VOTES"))
|
|
if err != nil {
|
|
return fmt.Errorf("could not create votes bucket: %v", err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("could not set up buckets, %v", err)
|
|
}
|
|
return db, nil
|
|
|
|
}
|
|
|
|
// CloseDB closes the key-value store file.
|
|
func CloseDB(db *bolt.DB) error {
|
|
return db.Close()
|
|
}
|
|
|
|
// InitDB init the db
|
|
func InitDB() {
|
|
db, err := OpenDB()
|
|
if err != nil {
|
|
log.Printf("\nOpenDB error: %v", err)
|
|
return
|
|
}
|
|
CloseDB(db)
|
|
}
|
|
|
|
// AddVote ajoute un vote à la bdd
|
|
func AddVote(db *bolt.DB, vote string, date time.Time) error {
|
|
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
|
err := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Put([]byte(date.Format(time.RFC3339)), []byte(vote))
|
|
if err != nil {
|
|
return fmt.Errorf("could not insert vote: %v", err)
|
|
}
|
|
return nil
|
|
})
|
|
fmt.Println("Added vote")
|
|
return err
|
|
}
|
|
|
|
// GetAllVotes liste tous les votes
|
|
func GetAllVotes(db *bolt.DB) ([]string, error) {
|
|
|
|
res := []string{}
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
b := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES"))
|
|
b.ForEach(func(k, v []byte) error {
|
|
res = append(res, string(v))
|
|
return nil
|
|
})
|
|
return nil
|
|
})
|
|
return res, err
|
|
}
|
|
|
|
// GetVotesOfTheDay liste tous les votes du jour
|
|
func GetVotesOfTheDay(db *bolt.DB, date time.Time) ([]string, error) {
|
|
res := []string{}
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor()
|
|
min := []byte(time.Now().AddDate(0, 0, -1).Format(time.RFC3339))
|
|
max := []byte(date.Format(time.RFC3339))
|
|
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
|
|
res = append(res, string(v))
|
|
fmt.Println(string(k))
|
|
fmt.Println(string(v))
|
|
}
|
|
return nil
|
|
})
|
|
return res, err
|
|
}
|
|
|
|
// GetVotesOfTheRange liste tous les votes du jour
|
|
func GetVotesOfTheRange(db *bolt.DB, min time.Time, max time.Time) ([]string, error) {
|
|
res := []string{}
|
|
err := db.View(func(tx *bolt.Tx) error {
|
|
c := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Cursor()
|
|
mino := []byte(min.Format(time.RFC3339))
|
|
maxo := []byte(max.Format(time.RFC3339))
|
|
for k, v := c.Seek(mino); k != nil && bytes.Compare(k, maxo) <= 0; k, v = c.Next() {
|
|
res = append(res, string(v))
|
|
fmt.Println(string(k))
|
|
fmt.Println(string(v))
|
|
}
|
|
return nil
|
|
})
|
|
return res, err
|
|
}
|