Store created event
This commit is contained in:
parent
e714e66703
commit
d7cee536e7
@ -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,
|
||||
|
54
store.go
54
store.go
@ -2,9 +2,13 @@ package henwen
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"crawshaw.io/sqlite"
|
||||
"crawshaw.io/sqlite/sqlitex"
|
||||
)
|
||||
|
||||
@ -36,6 +40,7 @@ func NewStore(filename string, genString GenString) (*Store, error) {
|
||||
genString: genString,
|
||||
}
|
||||
if needCreate {
|
||||
log.Println("creating schema")
|
||||
err = store.createSchema()
|
||||
if err != nil {
|
||||
defer pool.Close()
|
||||
@ -54,7 +59,7 @@ const schema = `
|
||||
CREATE TABLE event (
|
||||
id INTEGER PRIMARY KEY,
|
||||
alpha_id TEXT NOT NULL,
|
||||
admin_alpha_id TEXT NOT NULL,
|
||||
admin_code TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
earliest_date DATE NOT NULL,
|
||||
latest_date DATE NOT NULL,
|
||||
@ -92,7 +97,7 @@ type CreateEventCommand struct {
|
||||
}
|
||||
|
||||
type CreateEventResult struct {
|
||||
AlphaID, AdminAlphaID string
|
||||
AlphaID, AdminCode string
|
||||
}
|
||||
|
||||
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
|
||||
@ -104,18 +109,18 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
|
||||
return
|
||||
}
|
||||
|
||||
adminAlphaID, err := s.genString()
|
||||
adminCode, err := s.genString()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
const dateFmt = "2006-01-02"
|
||||
const query = `
|
||||
INSERT INTO event(alpha_id, admin_alpha_id, name, earliest_date, latest_date, duration)
|
||||
INSERT INTO event(alpha_id, admin_code, name, earliest_date, latest_date, duration)
|
||||
VALUES (?, ?, ?, ?, ?, ?);`
|
||||
err = sqlitex.Exec(conn, query, nil,
|
||||
alphaID,
|
||||
adminAlphaID,
|
||||
adminCode,
|
||||
cmd.Name,
|
||||
cmd.EarliestDate.Format(dateFmt),
|
||||
cmd.LatestDate.Format(dateFmt),
|
||||
@ -125,7 +130,44 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
|
||||
return
|
||||
}
|
||||
|
||||
result.AdminAlphaID = adminAlphaID
|
||||
result.AdminCode = adminCode
|
||||
result.AlphaID = alphaID
|
||||
return
|
||||
}
|
||||
|
||||
type GetEventQuery struct {
|
||||
AlphaID, AdminCode string
|
||||
}
|
||||
|
||||
type GetEventResult struct {
|
||||
Name string
|
||||
EarliestDate, LatestDate time.Time
|
||||
Duration int
|
||||
}
|
||||
|
||||
func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) error {
|
||||
conn := s.pool.Get(ctx)
|
||||
defer s.pool.Put(conn)
|
||||
|
||||
const dbQuery = `
|
||||
SELECT name, earliest_date, latest_date, duration
|
||||
FROM event
|
||||
WHERE alpha_id = ? AND admin_code = ?;`
|
||||
var found bool
|
||||
err := sqlitex.Exec(conn, dbQuery,
|
||||
func(stmt *sqlite.Stmt) error {
|
||||
found = true
|
||||
fmt.Printf("Name: %s\n", stmt.ColumnText(0))
|
||||
fmt.Printf("Earliest date: %s\n", stmt.ColumnText(1))
|
||||
fmt.Printf("Latest date: %s\n", stmt.ColumnText(2))
|
||||
fmt.Printf("Duration: %d\n", stmt.ColumnInt(3))
|
||||
return nil
|
||||
}, query.AlphaID, query.AdminCode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !found {
|
||||
return errors.New("not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user