2019-11-20 17:11:29 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-21 16:43:02 +01:00
|
|
|
"cadoles/foodoles/bdd"
|
2019-11-21 13:54:40 +01:00
|
|
|
"cadoles/foodoles/foodlist"
|
2019-11-26 09:07:19 +01:00
|
|
|
"cadoles/foodoles/vote"
|
2019-11-20 17:11:29 +01:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
http.HandleFunc("/", Base)
|
|
|
|
http.HandleFunc("/results", ResultsPage)
|
|
|
|
db, err := bdd.OpenDB()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("\nOpenDB error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bdd.CloseDB(db)
|
|
|
|
|
|
|
|
vote.GetVotesOfTheDay()
|
|
|
|
|
|
|
|
log.Print("\nready: listening on localhost:8080\n")
|
2019-11-21 13:54:40 +01:00
|
|
|
|
2019-11-20 17:11:29 +01:00
|
|
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
// Base is the entry point to all requests
|
|
|
|
func Base(w http.ResponseWriter, r *http.Request) {
|
2019-11-21 16:43:02 +01:00
|
|
|
|
2019-11-20 17:11:29 +01:00
|
|
|
if r.Method == http.MethodPost {
|
2019-11-26 09:07:19 +01:00
|
|
|
VotePage(w, r)
|
2019-11-20 17:11:29 +01:00
|
|
|
} else if r.Method == http.MethodGet {
|
2019-11-26 09:07:19 +01:00
|
|
|
HomePage(w, r)
|
2019-11-20 17:11:29 +01:00
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write([]byte("400 - Unsupported Request Method!"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
// HomePage is the homepage of the app
|
|
|
|
func HomePage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
foods := foodlist.GetFoodOfTheDay()
|
2019-11-21 13:54:40 +01:00
|
|
|
paths := []string{
|
|
|
|
"./templates/index.tmpl",
|
2019-11-20 17:11:29 +01:00
|
|
|
}
|
2019-11-21 13:54:40 +01:00
|
|
|
t := template.Must(template.New("index.tmpl").ParseFiles(paths...))
|
|
|
|
err := t.Execute(w, foods)
|
2019-11-20 17:11:29 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("\nExecute error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
// ResultsPage is the page displaiyng the votes results
|
|
|
|
func ResultsPage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
db, err := bdd.OpenDB()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("\nOpenDB error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//vote, _ := bdd.GetVotesOfTheDay(db, time.Now().AddDate(0, 0, -1))
|
|
|
|
bdd.CloseDB(db)
|
2019-11-21 16:43:02 +01:00
|
|
|
paths := []string{
|
|
|
|
"./templates/results.tmpl",
|
|
|
|
}
|
|
|
|
t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
|
2019-11-26 09:07:19 +01:00
|
|
|
err = t.Execute(w, "")
|
2019-11-21 16:43:02 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("\nExecute error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 13:54:40 +01:00
|
|
|
|
2019-11-26 09:07:19 +01:00
|
|
|
// VotePage is the endpoint to add votes
|
|
|
|
func VotePage(w http.ResponseWriter, r *http.Request) {
|
2019-11-21 16:43:02 +01:00
|
|
|
r.ParseForm()
|
|
|
|
option := r.Form.Get("option")
|
2019-11-26 09:07:19 +01:00
|
|
|
|
2019-11-21 16:43:02 +01:00
|
|
|
log.Print("vote for : ", option)
|
2019-11-26 09:07:19 +01:00
|
|
|
vote.ForFood(option)
|
2019-11-21 13:54:40 +01:00
|
|
|
|
2019-11-21 16:43:02 +01:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write([]byte("Success vote for: " + option))
|
2019-11-20 17:11:29 +01:00
|
|
|
}
|