init database

This commit is contained in:
Matthieu Lamalle 2019-11-21 16:43:02 +01:00
parent 84227209ee
commit a112fa589f
5 changed files with 53 additions and 32 deletions

View File

@ -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
})
}

BIN
foods.db Normal file

Binary file not shown.

BIN
server

Binary file not shown.

View File

@ -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))
}

18
vote/vote.go Normal file
View File

@ -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
}