Store created event

This commit is contained in:
2020-03-31 08:47:40 -06:00
parent e714e66703
commit d7cee536e7
2 changed files with 92 additions and 22 deletions

View File

@ -2,12 +2,14 @@ package main
import (
"bytes"
"context"
"crypto/rand"
"fmt"
"io"
"log"
"math/big"
"net/http"
"net/url"
"time"
h "gitlab.codemonkeysoftware.net/b/hatmill"
@ -23,9 +25,10 @@ const baseURL = "http://localhost:8080"
const dbFileName = "./henwen.db"
const (
PathRoot = "/"
PathCreate = "/create"
PathDoCreate = "/create/do"
pathRoot = "/"
pathCreate = "/create"
pathDoCreate = "/create/do"
pathAdmin = "/admin"
)
var store *henwen.Store
@ -47,7 +50,7 @@ func writePage(w io.Writer, contents h.Term) error {
func pageRoot() h.Term {
return h.Terms{
e.H2()(h.Text("Welcome!")),
e.A(a.Href(PathCreate))(
e.A(a.Href(pathCreate))(
h.Text("Create event"),
),
}
@ -62,7 +65,7 @@ const (
func pageCreate() h.Term {
return h.Terms{
e.H2()(h.Text("Create an event")),
e.Form(a.Action(PathDoCreate), a.Method("POST"))(
e.Form(a.Action(pathDoCreate), a.Method("POST"))(
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
e.Input(a.Name(fieldNameEventName)),
@ -77,20 +80,27 @@ func pageCreate() h.Term {
}
}
func makeAdminURL(eventID, adminCode string) string {
return fmt.Sprintf("%s/%s/admin/%s", baseURL, eventID, adminCode)
func pageAdmin() h.Term {
return h.Text("check the log")
}
const keyEventID = "event_id"
const keyAdminCode = "admin_code"
func pageDoCreate(name string, earliest, latest time.Time) h.Term {
id, err := genString()
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
Name: name,
EarliestDate: earliest,
LatestDate: latest,
Duration: 1,
})
if err != nil {
return h.Text(err.Error())
}
adminCode, err := genString()
if err != nil {
return h.Text(err.Error())
}
adminURL := makeAdminURL(id, adminCode)
var adminQuery = make(url.Values)
adminQuery.Add(keyEventID, event.AlphaID)
adminQuery.Add(keyAdminCode, event.AdminCode)
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
return h.Terms{
e.H2()(h.Text("Created event!")),
@ -133,14 +143,20 @@ func genString() (string, error) {
}
func main() {
var err error
store, err = henwen.NewStore(dbFileName, genString)
if err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
mux.HandleFunc(PathRoot, func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(pathRoot, func(w http.ResponseWriter, r *http.Request) {
_ = writePage(w, pageRoot())
})
mux.HandleFunc(PathCreate, func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(pathCreate, func(w http.ResponseWriter, r *http.Request) {
_ = writePage(w, pageCreate())
})
mux.HandleFunc(PathDoCreate, func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(pathDoCreate, func(w http.ResponseWriter, r *http.Request) {
const dateFmt = "2006-01-02"
earliest, err := time.Parse(dateFmt, r.FormValue(fieldNameEarliest))
if err != nil {
@ -160,6 +176,18 @@ func main() {
_ = writePage(w, pageDoCreate(eventName, earliest, latest))
})
mux.HandleFunc(pathAdmin, func(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
err := store.GetEvent(context.Background(), henwen.GetEventQuery{
AlphaID: query.Get(keyEventID),
AdminCode: query.Get(keyAdminCode),
})
if err != nil {
fmt.Fprint(w, err.Error())
return
}
_ = writePage(w, pageAdmin())
})
srv := http.Server{
Addr: Addr,