122 lines
2.8 KiB
Go
122 lines
2.8 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) ([]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 {
|
|
hop, _ := time.Parse(time.RFC3339, string(k))
|
|
if DateEqual(hop, time.Now()) {
|
|
res = append(res, string(v))
|
|
return nil
|
|
}
|
|
return nil
|
|
})
|
|
return nil
|
|
})
|
|
//fmt.Print(res)
|
|
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))
|
|
}
|
|
return nil
|
|
})
|
|
return res, err
|
|
}
|
|
|
|
// DateEqual check if dates are equal
|
|
func DateEqual(date1, date2 time.Time) bool {
|
|
y1, m1, d1 := date1.Date()
|
|
y2, m2, d2 := date2.Date()
|
|
|
|
return y1 == y2 && m1 == m2 && d1 == d2
|
|
}
|