foodoles/bdd/bdd.go

113 lines
2.7 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
func InitDB() {
db, err := OpenDB()
if err != nil {
log.Printf("\nOpenDB error: %v", err)
return
}
CloseDB(db)
}
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
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))
2019-11-27 10:06:24 +01:00
max := []byte(date.Format(time.RFC3339))
2019-11-26 09:07:19 +01:00
for k, v := c.Seek(min); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() {
res = append(res, string(v))
2019-11-27 10:06:24 +01:00
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))
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
}