set bdd
This commit is contained in:
72
vote/vote.go
72
vote/vote.go
@ -1,5 +1,12 @@
|
||||
package vote
|
||||
|
||||
import (
|
||||
"cadoles/foodoles/bdd"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// VotesOfTheDay est le résultat des votes du jour
|
||||
type VotesOfTheDay struct {
|
||||
Date string
|
||||
@ -13,6 +20,69 @@ type Vote struct {
|
||||
}
|
||||
|
||||
// ForFood vote pour un choix du jour
|
||||
func ForFood(Today string, Food string) {
|
||||
func ForFood(Food string) {
|
||||
db, err := bdd.OpenDB()
|
||||
if err != nil {
|
||||
log.Printf("\nOpenDB error: %v", err)
|
||||
return
|
||||
}
|
||||
bdd.AddVote(db, Food, time.Now().AddDate(0, 0, 0))
|
||||
bdd.CloseDB(db)
|
||||
return
|
||||
}
|
||||
|
||||
// GetVotesOfTheDay récupère les votes du jour
|
||||
func GetVotesOfTheDay() VotesOfTheDay {
|
||||
time.Now()
|
||||
vo := VotesOfTheDay{time.Now().Format("02/01/2006"), nil}
|
||||
duplicate := FoodList()
|
||||
|
||||
dupmap := dupcount(duplicate)
|
||||
|
||||
fmt.Println(dupmap)
|
||||
|
||||
for k, v := range dupmap {
|
||||
vv := Vote{k, v}
|
||||
vo.Votes = append(vo.Votes, vv)
|
||||
}
|
||||
fmt.Println(vo)
|
||||
return vo
|
||||
}
|
||||
|
||||
// FoodList return a list of food
|
||||
func FoodList() []Vote {
|
||||
f := []Vote{}
|
||||
|
||||
db, err := bdd.OpenDB()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
lvotes, _ := bdd.GetVotesOfTheDay(db, time.Now())
|
||||
|
||||
for _, fo := range lvotes {
|
||||
vf := Vote{fo, 1}
|
||||
f = append(f, vf)
|
||||
}
|
||||
bdd.CloseDB(db)
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func dupcount(list []Vote) map[string]int {
|
||||
|
||||
duplicatefrequency := make(map[string]int)
|
||||
|
||||
for _, item := range list {
|
||||
// check if the item/element exist in the duplicate_frequency map
|
||||
|
||||
_, exist := duplicatefrequency[item.Food]
|
||||
|
||||
if exist {
|
||||
duplicatefrequency[item.Food]++ // increase counter by 1 if already in the map
|
||||
} else {
|
||||
duplicatefrequency[item.Food] = 1 // else start counting from 1
|
||||
}
|
||||
}
|
||||
return duplicatefrequency
|
||||
}
|
||||
|
Reference in New Issue
Block a user