retreive votes of the same day

This commit is contained in:
Matthieu Lamalle 2019-11-27 11:35:56 +01:00
parent 9e3a10941a
commit 68eb55d8a7
5 changed files with 22 additions and 14 deletions

View File

@ -8,4 +8,3 @@ TODO :
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

View File

@ -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
}

BIN
foods.db

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -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}