87 lines
1.5 KiB
Go
87 lines
1.5 KiB
Go
package vote
|
|
|
|
import (
|
|
"cadoles/foodoles/bdd"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// VotesOfTheDay est le résultat des votes du jour
|
|
type VotesOfTheDay struct {
|
|
Date string
|
|
Votes []Vote
|
|
}
|
|
|
|
// Vote est un vote
|
|
type Vote struct {
|
|
Food string
|
|
Voices int
|
|
}
|
|
|
|
// ForFood vote pour un choix du jour
|
|
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 {
|
|
vo := VotesOfTheDay{time.Now().Format("02/01/2006"), nil}
|
|
duplicate := FoodVotesList()
|
|
|
|
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
|
|
}
|
|
|
|
// FoodVotesList return a list of votes for food
|
|
func FoodVotesList() []Vote {
|
|
f := []Vote{}
|
|
|
|
db, err := bdd.OpenDB()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
lvotes, _ := bdd.GetVotesOfTheDay(db)
|
|
|
|
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
|
|
}
|