init database
This commit is contained in:
parent
84227209ee
commit
a112fa589f
17
bdd/bdd.go
17
bdd/bdd.go
|
@ -1,32 +1,27 @@
|
||||||
package bdd
|
package bdd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/csv"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
bolt "go.etcd.io/bbolt"
|
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BUCKET est le nom de la base
|
// BDD_VOTES est le nom de la base
|
||||||
var BUCKET = "FoodBucket"
|
var BDD_VOTES = "VotesBucket"
|
||||||
|
|
||||||
|
|
||||||
// InitBDD initialise la BDD
|
// InitBDD initialise la BDD
|
||||||
func InitBDD() bolt.DB {
|
func InitBDD() {
|
||||||
db, err := bolt.Open("foods.db", 0600, nil)
|
db, err := bolt.Open("foods.db", 0600, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
db.Update(func(tx *bolt.Tx) error {
|
db.Update(func(tx *bolt.Tx) error {
|
||||||
b, err := tx.CreateBucket([]byte(BUCKET))
|
_, err := tx.CreateBucketIfNotExists([]byte(BDD_VOTES))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("create bucket: %s", err)
|
return fmt.Errorf("create bucket: %s", err)
|
||||||
}
|
}
|
||||||
log.Print(b)
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
50
server.go
50
server.go
|
@ -1,7 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cadoles/foodoles/bdd"
|
||||||
"cadoles/foodoles/foodlist"
|
"cadoles/foodoles/foodlist"
|
||||||
|
votefood "cadoles/foodoles/vote"
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -14,8 +16,8 @@ var Today = time.Now().Format("02/01/2006")
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
http.HandleFunc("/", base)
|
http.HandleFunc("/", base)
|
||||||
http.HandleFunc("/results", results)
|
http.HandleFunc("/results", resultsPage)
|
||||||
|
bdd.InitBDD()
|
||||||
log.Print("ready: listening on localhost:8080\n")
|
log.Print("ready: listening on localhost:8080\n")
|
||||||
|
|
||||||
if err := http.ListenAndServe(":8080", nil); err != nil {
|
if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||||
|
@ -24,18 +26,18 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func base(w http.ResponseWriter, r *http.Request) {
|
func base(w http.ResponseWriter, r *http.Request) {
|
||||||
// TODO: Init la base avec la date du jour
|
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
log.Printf("vote")
|
votePage(w, r)
|
||||||
} else if r.Method == http.MethodGet {
|
} else if r.Method == http.MethodGet {
|
||||||
home(w, r)
|
homePage(w, r)
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
w.Write([]byte("400 - Unsupported Request Method!"))
|
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)
|
foods := foodlist.GetFoodOfTheDay(Today)
|
||||||
paths := []string{
|
paths := []string{
|
||||||
"./templates/index.tmpl",
|
"./templates/index.tmpl",
|
||||||
|
@ -48,19 +50,25 @@ func home(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func results(w http.ResponseWriter, r *http.Request) {
|
func resultsPage(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
paths := []string{
|
||||||
|
"./templates/results.tmpl",
|
||||||
// option := r.Form.Get("option")
|
}
|
||||||
// foodlist.VoteForFood(Today, option)
|
t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
|
||||||
|
err := t.Execute(w, "")
|
||||||
// paths := []string{
|
if err != nil {
|
||||||
// "./templates/results.tmpl",
|
log.Printf("\nExecute error: %v", err)
|
||||||
// }
|
return
|
||||||
// t := template.Must(template.New("results.tmpl").ParseFiles(paths...))
|
}
|
||||||
// err := t.Execute(w, "")
|
}
|
||||||
// if err != nil {
|
|
||||||
// log.Printf("\nExecute error: %v", err)
|
func votePage(w http.ResponseWriter, r *http.Request) {
|
||||||
// return
|
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))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue