Display event details on admin page
This commit is contained in:
parent
d7cee536e7
commit
ab3aaaea24
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
*.db
|
||||||
|
*.db-shm
|
||||||
|
*.db-wal
|
@ -57,18 +57,22 @@ func pageRoot() h.Term {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
fieldNameEarliest = "earliestDate"
|
fieldNameEarliest = "earliestDate"
|
||||||
fieldNameLatest = "latestDate"
|
fieldNameLatest = "latestDate"
|
||||||
fieldNameEventName = "eventName"
|
fieldNameEventName = "eventName"
|
||||||
|
fieldNameDescription = "eventDescription"
|
||||||
)
|
)
|
||||||
|
|
||||||
func pageCreate() h.Term {
|
func pageCreate() h.Term {
|
||||||
return h.Terms{
|
return h.Terms{
|
||||||
e.H2()(h.Text("Create an event")),
|
e.H2()(h.Text("Create an event")),
|
||||||
e.Form(a.Action(pathDoCreate), a.Method("POST"))(
|
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
|
||||||
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
||||||
e.Input(a.Name(fieldNameEventName)),
|
e.Input(a.Name(fieldNameEventName)),
|
||||||
|
|
||||||
|
e.Label(a.For(fieldNameDescription))(h.Text("Description")),
|
||||||
|
e.Textarea(a.Name(fieldNameEventName), a.Placeholder("What's going on?"))(),
|
||||||
|
|
||||||
e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")),
|
e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")),
|
||||||
e.Input(a.Name(fieldNameEarliest), a.Type("date")),
|
e.Input(a.Name(fieldNameEarliest), a.Type("date")),
|
||||||
|
|
||||||
@ -80,16 +84,56 @@ func pageCreate() h.Term {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pageAdmin() h.Term {
|
func pageAdmin(alphaID, adminCode string) h.Term {
|
||||||
return h.Text("check the log")
|
event, err := store.GetEvent(context.Background(), henwen.GetEventQuery{
|
||||||
|
AlphaID: alphaID,
|
||||||
|
AdminCode: adminCode,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return h.Text(err.Error())
|
||||||
|
}
|
||||||
|
return h.Terms{
|
||||||
|
e.H2()(h.Text("Edit your event")),
|
||||||
|
e.Form(a.Action(pathDoCreate), a.Method(http.MethodPost))(
|
||||||
|
e.Label(a.For(fieldNameEventName))(h.Text("Event name")),
|
||||||
|
e.Input(
|
||||||
|
a.Name(fieldNameEventName),
|
||||||
|
a.Value(event.Name),
|
||||||
|
),
|
||||||
|
e.Br(),
|
||||||
|
|
||||||
|
e.Label(a.For(fieldNameDescription))(h.Text("Description")),
|
||||||
|
e.Textarea(a.Name(fieldNameEventName))(h.Text(event.Description)),
|
||||||
|
e.Br(),
|
||||||
|
|
||||||
|
e.Label(a.For(fieldNameEarliest))(h.Text("Earliest date")),
|
||||||
|
e.Input(
|
||||||
|
a.Name(fieldNameEarliest),
|
||||||
|
a.Type("date"),
|
||||||
|
a.Value(event.EarliestDate.Format(dateFmt)),
|
||||||
|
),
|
||||||
|
e.Br(),
|
||||||
|
|
||||||
|
e.Label(a.For(fieldNameLatest))(h.Text("Latest date")),
|
||||||
|
e.Input(
|
||||||
|
a.Name(fieldNameLatest),
|
||||||
|
a.Type("date"),
|
||||||
|
a.Value(event.LatestDate.Format(dateFmt)),
|
||||||
|
),
|
||||||
|
e.Br(),
|
||||||
|
|
||||||
|
e.Input(a.Type("submit")),
|
||||||
|
),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const keyEventID = "event_id"
|
const keyEventID = "event_id"
|
||||||
const keyAdminCode = "admin_code"
|
const keyAdminCode = "admin_code"
|
||||||
|
|
||||||
func pageDoCreate(name string, earliest, latest time.Time) h.Term {
|
func pageDoCreate(name, description string, earliest, latest time.Time) h.Term {
|
||||||
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
|
event, err := store.CreateEvent(context.Background(), henwen.CreateEventCommand{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
Description: description,
|
||||||
EarliestDate: earliest,
|
EarliestDate: earliest,
|
||||||
LatestDate: latest,
|
LatestDate: latest,
|
||||||
Duration: 1,
|
Duration: 1,
|
||||||
@ -102,6 +146,8 @@ func pageDoCreate(name string, earliest, latest time.Time) h.Term {
|
|||||||
adminQuery.Add(keyAdminCode, event.AdminCode)
|
adminQuery.Add(keyAdminCode, event.AdminCode)
|
||||||
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
|
adminURL := baseURL + pathAdmin + "?" + adminQuery.Encode()
|
||||||
|
|
||||||
|
const dateDisplayFmt = "Monday, January 2, 2006"
|
||||||
|
|
||||||
return h.Terms{
|
return h.Terms{
|
||||||
e.H2()(h.Text("Created event!")),
|
e.H2()(h.Text("Created event!")),
|
||||||
e.P()(
|
e.P()(
|
||||||
@ -114,10 +160,10 @@ func pageDoCreate(name string, earliest, latest time.Time) h.Term {
|
|||||||
h.Text(name),
|
h.Text(name),
|
||||||
|
|
||||||
e.H3()(h.Text("Earliest date")),
|
e.H3()(h.Text("Earliest date")),
|
||||||
h.Text(earliest.Format(time.ANSIC)),
|
h.Text(earliest.Format(dateDisplayFmt)),
|
||||||
|
|
||||||
e.H3()(h.Text("Latest date")),
|
e.H3()(h.Text("Latest date")),
|
||||||
h.Text(latest.Format(time.ANSIC)),
|
h.Text(latest.Format(dateDisplayFmt)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,6 +188,8 @@ func genString() (string, error) {
|
|||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dateFmt = "2006-01-02"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
store, err = henwen.NewStore(dbFileName, genString)
|
store, err = henwen.NewStore(dbFileName, genString)
|
||||||
@ -157,7 +205,6 @@ func main() {
|
|||||||
_ = writePage(w, pageCreate())
|
_ = 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))
|
earliest, err := time.Parse(dateFmt, r.FormValue(fieldNameEarliest))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprint(w, "bad earliest date")
|
fmt.Fprint(w, "bad earliest date")
|
||||||
@ -173,20 +220,13 @@ func main() {
|
|||||||
fmt.Fprint(w, "event name is required")
|
fmt.Fprint(w, "event name is required")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
description := r.FormValue(fieldNameDescription)
|
||||||
|
|
||||||
_ = writePage(w, pageDoCreate(eventName, earliest, latest))
|
_ = writePage(w, pageDoCreate(eventName, description, earliest, latest))
|
||||||
})
|
})
|
||||||
mux.HandleFunc(pathAdmin, func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc(pathAdmin, func(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
err := store.GetEvent(context.Background(), henwen.GetEventQuery{
|
_ = writePage(w, pageAdmin(query.Get(keyEventID), query.Get(keyAdminCode)))
|
||||||
AlphaID: query.Get(keyEventID),
|
|
||||||
AdminCode: query.Get(keyAdminCode),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprint(w, err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
_ = writePage(w, pageAdmin())
|
|
||||||
})
|
})
|
||||||
|
|
||||||
srv := http.Server{
|
srv := http.Server{
|
||||||
|
46
store.go
46
store.go
@ -3,7 +3,6 @@ package henwen
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -61,6 +60,7 @@ const schema = `
|
|||||||
alpha_id TEXT NOT NULL,
|
alpha_id TEXT NOT NULL,
|
||||||
admin_code TEXT NOT NULL,
|
admin_code TEXT NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
|
description TEXT NOT NULL,
|
||||||
earliest_date DATE NOT NULL,
|
earliest_date DATE NOT NULL,
|
||||||
latest_date DATE NOT NULL,
|
latest_date DATE NOT NULL,
|
||||||
duration INTEGER NOT NULL,
|
duration INTEGER NOT NULL,
|
||||||
@ -92,6 +92,7 @@ func (s *Store) createSchema() error {
|
|||||||
|
|
||||||
type CreateEventCommand struct {
|
type CreateEventCommand struct {
|
||||||
Name string
|
Name string
|
||||||
|
Description string
|
||||||
EarliestDate, LatestDate time.Time
|
EarliestDate, LatestDate time.Time
|
||||||
Duration int
|
Duration int
|
||||||
}
|
}
|
||||||
@ -100,6 +101,8 @@ type CreateEventResult struct {
|
|||||||
AlphaID, AdminCode string
|
AlphaID, AdminCode string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const dateFmt = "2006-01-02"
|
||||||
|
|
||||||
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
|
func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result CreateEventResult, err error) {
|
||||||
conn := s.pool.Get(ctx)
|
conn := s.pool.Get(ctx)
|
||||||
defer s.pool.Put(conn)
|
defer s.pool.Put(conn)
|
||||||
@ -114,14 +117,14 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const dateFmt = "2006-01-02"
|
|
||||||
const query = `
|
const query = `
|
||||||
INSERT INTO event(alpha_id, admin_code, name, earliest_date, latest_date, duration)
|
INSERT INTO event(alpha_id, admin_code, name, description, earliest_date, latest_date, duration)
|
||||||
VALUES (?, ?, ?, ?, ?, ?);`
|
VALUES (?, ?, ?, ?, ?, ?, ?);`
|
||||||
err = sqlitex.Exec(conn, query, nil,
|
err = sqlitex.Exec(conn, query, nil,
|
||||||
alphaID,
|
alphaID,
|
||||||
adminCode,
|
adminCode,
|
||||||
cmd.Name,
|
cmd.Name,
|
||||||
|
cmd.Description,
|
||||||
cmd.EarliestDate.Format(dateFmt),
|
cmd.EarliestDate.Format(dateFmt),
|
||||||
cmd.LatestDate.Format(dateFmt),
|
cmd.LatestDate.Format(dateFmt),
|
||||||
cmd.Duration,
|
cmd.Duration,
|
||||||
@ -141,33 +144,44 @@ type GetEventQuery struct {
|
|||||||
|
|
||||||
type GetEventResult struct {
|
type GetEventResult struct {
|
||||||
Name string
|
Name string
|
||||||
|
Description string
|
||||||
EarliestDate, LatestDate time.Time
|
EarliestDate, LatestDate time.Time
|
||||||
Duration int
|
Duration int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) error {
|
func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) (GetEventResult, error) {
|
||||||
conn := s.pool.Get(ctx)
|
conn := s.pool.Get(ctx)
|
||||||
defer s.pool.Put(conn)
|
defer s.pool.Put(conn)
|
||||||
|
|
||||||
const dbQuery = `
|
const dbQuery = `
|
||||||
SELECT name, earliest_date, latest_date, duration
|
SELECT name, description, earliest_date, latest_date, duration
|
||||||
FROM event
|
FROM event
|
||||||
WHERE alpha_id = ? AND admin_code = ?;`
|
WHERE alpha_id = ?;`
|
||||||
|
var result GetEventResult
|
||||||
var found bool
|
var found bool
|
||||||
err := sqlitex.Exec(conn, dbQuery,
|
err := sqlitex.Exec(conn, dbQuery,
|
||||||
func(stmt *sqlite.Stmt) error {
|
func(stmt *sqlite.Stmt) error {
|
||||||
found = true
|
found = true
|
||||||
fmt.Printf("Name: %s\n", stmt.ColumnText(0))
|
|
||||||
fmt.Printf("Earliest date: %s\n", stmt.ColumnText(1))
|
result.Name = stmt.ColumnText(0)
|
||||||
fmt.Printf("Latest date: %s\n", stmt.ColumnText(2))
|
result.Description = stmt.ColumnText(1)
|
||||||
fmt.Printf("Duration: %d\n", stmt.ColumnInt(3))
|
earliestDateString := stmt.ColumnText(2)
|
||||||
return nil
|
latestDateString := stmt.ColumnText(3)
|
||||||
}, query.AlphaID, query.AdminCode)
|
result.Duration = stmt.ColumnInt(4)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
result.EarliestDate, err = time.Parse(dateFmt, earliestDateString)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
result.LatestDate, err = time.Parse(dateFmt, latestDateString)
|
||||||
|
return err
|
||||||
|
}, query.AlphaID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return GetEventResult{}, err
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
return errors.New("not found")
|
return GetEventResult{}, errors.New("not found")
|
||||||
}
|
}
|
||||||
return nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user