diff --git a/bdd/bdd.go b/bdd/bdd.go index b2e7853..4475c14 100644 --- a/bdd/bdd.go +++ b/bdd/bdd.go @@ -1,32 +1,27 @@ package bdd import ( - "bufio" - "encoding/csv" "fmt" - bolt "go.etcd.io/bbolt" - "io" "log" - "os" + + bolt "go.etcd.io/bbolt" ) -// BUCKET est le nom de la base -var BUCKET = "FoodBucket" - +// BDD_VOTES est le nom de la base +var BDD_VOTES = "VotesBucket" // InitBDD initialise la BDD -func InitBDD() bolt.DB { +func InitBDD() { db, err := bolt.Open("foods.db", 0600, nil) if err != nil { log.Fatal(err) } db.Update(func(tx *bolt.Tx) error { - b, err := tx.CreateBucket([]byte(BUCKET)) + _, err := tx.CreateBucketIfNotExists([]byte(BDD_VOTES)) if err != nil { return fmt.Errorf("create bucket: %s", err) } - log.Print(b) return nil }) } diff --git a/foods.db b/foods.db new file mode 100644 index 0000000..655109f Binary files /dev/null and b/foods.db differ diff --git a/server b/server index b15afc2..de9bf4e 100755 Binary files a/server and b/server differ diff --git a/server.go b/server.go index 7ee3f23..6d1693c 100644 --- a/server.go +++ b/server.go @@ -1,7 +1,9 @@ package main import ( + "cadoles/foodoles/bdd" "cadoles/foodoles/foodlist" + votefood "cadoles/foodoles/vote" "html/template" "log" "net/http" @@ -14,8 +16,8 @@ var Today = time.Now().Format("02/01/2006") func main() { http.HandleFunc("/", base) - http.HandleFunc("/results", results) - + http.HandleFunc("/results", resultsPage) + bdd.InitBDD() log.Print("ready: listening on localhost:8080\n") if err := http.ListenAndServe(":8080", nil); err != nil { @@ -24,18 +26,18 @@ func main() { } func base(w http.ResponseWriter, r *http.Request) { - // TODO: Init la base avec la date du jour + if r.Method == http.MethodPost { - log.Printf("vote") + votePage(w, r) } else if r.Method == http.MethodGet { - home(w, r) + homePage(w, r) } else { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("400 - Unsupported Request Method!")) } } -func home(w http.ResponseWriter, r *http.Request) { +func homePage(w http.ResponseWriter, r *http.Request) { foods := foodlist.GetFoodOfTheDay(Today) paths := []string{ "./templates/index.tmpl", @@ -48,19 +50,25 @@ func home(w http.ResponseWriter, r *http.Request) { } } -func results(w http.ResponseWriter, r *http.Request) { - r.ParseForm() - - // option := r.Form.Get("option") - // foodlist.VoteForFood(Today, option) - - // paths := []string{ - // "./templates/results.tmpl", - // } - // t := template.Must(template.New("results.tmpl").ParseFiles(paths...)) - // err := t.Execute(w, "") - // if err != nil { - // log.Printf("\nExecute error: %v", err) - // return - // } +func resultsPage(w http.ResponseWriter, r *http.Request) { + paths := []string{ + "./templates/results.tmpl", + } + t := template.Must(template.New("results.tmpl").ParseFiles(paths...)) + err := t.Execute(w, "") + if err != nil { + log.Printf("\nExecute error: %v", err) + return + } +} + +func votePage(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + option := r.Form.Get("option") + log.Print("vote for : ", option) + votefood.ForFood(Today, option) + + w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/json") + w.Write([]byte("Success vote for: " + option)) } diff --git a/vote/vote.go b/vote/vote.go new file mode 100644 index 0000000..03c3bcc --- /dev/null +++ b/vote/vote.go @@ -0,0 +1,18 @@ +package vote + +// 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(Today string, Food string) { + return +}