diff --git a/README.md b/README.md index 91f3d8b..ba4de07 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,4 @@ TODO : Refactor en utilisant "github.com/go-chi/chi" et "github.com/go-chi/chi/middleware" Gérer la configuration avec "gopkg.in/ini.v1" Afficher une liste de restaurants locaux répondant au vote du jour - Afficher le résultat du vote - Récupérer les vote du jour j \ No newline at end of file + Afficher le résultat du vote \ No newline at end of file diff --git a/bdd/bdd.go b/bdd/bdd.go index 94d3c3e..7da612d 100644 --- a/bdd/bdd.go +++ b/bdd/bdd.go @@ -78,19 +78,22 @@ func GetAllVotes(db *bolt.DB) ([]string, error) { } // GetVotesOfTheDay liste tous les votes du jour -func GetVotesOfTheDay(db *bolt.DB, date time.Time) ([]string, error) { +func GetVotesOfTheDay(db *bolt.DB) ([]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)) - } + 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 } @@ -103,10 +106,16 @@ func GetVotesOfTheRange(db *bolt.DB, min time.Time, max time.Time) ([]string, er 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 } + +// 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 +} diff --git a/foods.db b/foods.db index b21f79b..6fed49e 100644 Binary files a/foods.db and b/foods.db differ diff --git a/server b/server index b12ea66..1e8f239 100755 Binary files a/server and b/server differ diff --git a/vote/vote.go b/vote/vote.go index 77bcf48..8bc7d57 100644 --- a/vote/vote.go +++ b/vote/vote.go @@ -57,7 +57,7 @@ func FoodVotesList() []Vote { log.Fatal(err) } - lvotes, _ := bdd.GetVotesOfTheRange(db, time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 0)) + lvotes, _ := bdd.GetVotesOfTheDay(db) for _, fo := range lvotes { vf := Vote{fo, 1}