foodoles/bdd/bdd.go

127 lines
2.9 KiB
Go
Raw Normal View History

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-27 10:06:24 +01:00
"log"
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()
}
2019-11-27 10:06:24 +01:00
// InitDB init the db
2019-12-13 16:05:19 +01:00
func InitDB() error {
2019-11-27 10:06:24 +01:00
db, err := OpenDB()
if err != nil {
log.Printf("\nOpenDB error: %v", err)
2019-12-13 16:05:19 +01:00
return err
2019-11-27 10:06:24 +01:00
}
2019-12-13 16:05:19 +01:00
defer func() {
if err1 := CloseDB(db); err1 != nil {
err = err1
}
}()
return nil
2019-11-27 10:06:24 +01:00
}
2019-11-26 09:07:19 +01:00
// 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
2019-11-27 11:35:56 +01:00
func GetVotesOfTheDay(db *bolt.DB) ([]string, error) {
2019-11-26 09:07:19 +01:00
res := []string{}
2019-11-27 11:35:56 +01:00
2019-11-26 09:07:19 +01:00
err := db.View(func(tx *bolt.Tx) error {
2019-11-27 11:35:56 +01:00
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
})
2019-11-27 10:06:24 +01:00
return nil
})
2019-11-27 12:09:15 +01:00
//fmt.Print(res)
2019-11-27 10:06:24 +01:00
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))
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
}
2019-11-27 11:35:56 +01:00
// 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
}