2019-11-21 13:54:40 +01:00
|
|
|
package bdd
|
|
|
|
|
|
|
|
import (
|
2019-11-26 09:07:19 +01:00
|
|
|
"bytes"
|
2019-11-21 13:54:40 +01:00
|
|
|
"fmt"
|
2019-11-26 09:07:19 +01:00
|
|
|
"time"
|
2019-11-21 13:54:40 +01:00
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
"github.com/boltdb/bolt"
|
2019-11-21 16:43:02 +01:00
|
|
|
)
|
2019-11-21 13:54:40 +01:00
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
// OpenDB a key-value store.
|
|
|
|
func OpenDB() (*bolt.DB, error) {
|
2019-11-21 13:54:40 +01:00
|
|
|
db, err := bolt.Open("foods.db", 0600, nil)
|
|
|
|
if err != nil {
|
2019-11-26 09:07:19 +01:00
|
|
|
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)
|
2019-11-21 13:54:40 +01:00
|
|
|
}
|
2019-11-26 09:07:19 +01:00
|
|
|
return db, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseDB closes the key-value store file.
|
|
|
|
func CloseDB(db *bolt.DB) error {
|
|
|
|
return db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddVote ajoute un vote à la bdd
|
|
|
|
func AddVote(db *bolt.DB, vote string, date time.Time) error {
|
2019-11-21 13:54:40 +01:00
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
err := db.Update(func(tx *bolt.Tx) error {
|
|
|
|
err := tx.Bucket([]byte("DB")).Bucket([]byte("VOTES")).Put([]byte(date.Format(time.RFC3339)), []byte(vote))
|
2019-11-21 13:54:40 +01:00
|
|
|
if err != nil {
|
2019-11-26 09:07:19 +01:00
|
|
|
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(time.Now().AddDate(0, 0, 0).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))
|
2019-11-21 13:54:40 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-11-26 09:07:19 +01:00
|
|
|
return res, err
|
2019-11-21 13:54:40 +01:00
|
|
|
}
|