Show total responses on admin page
This commit is contained in:
parent
00425829c8
commit
051e5f4601
@ -133,17 +133,17 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetEventQuery struct {
|
type GetEventMetadataQuery struct {
|
||||||
AlphaID string
|
AlphaID string
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetEventResult struct {
|
type GetEventMetadataResult struct {
|
||||||
Name string
|
Name string
|
||||||
Description string
|
Description string
|
||||||
EarliestDate, LatestDate time.Time
|
EarliestDate, LatestDate time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) (GetEventResult, error) {
|
func (s *Store) GetEventMetadata(ctx context.Context, query GetEventMetadataQuery) (GetEventMetadataResult, error) {
|
||||||
conn := s.pool.Get(ctx)
|
conn := s.pool.Get(ctx)
|
||||||
defer s.pool.Put(conn)
|
defer s.pool.Put(conn)
|
||||||
|
|
||||||
@ -151,7 +151,7 @@ func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) (GetEventResu
|
|||||||
SELECT name, description, earliest_date, latest_date
|
SELECT name, description, earliest_date, latest_date
|
||||||
FROM event
|
FROM event
|
||||||
WHERE alpha_id = ?;`
|
WHERE alpha_id = ?;`
|
||||||
var result GetEventResult
|
var result GetEventMetadataResult
|
||||||
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 {
|
||||||
@ -171,11 +171,38 @@ func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) (GetEventResu
|
|||||||
return err
|
return err
|
||||||
}, query.AlphaID)
|
}, query.AlphaID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return GetEventResult{}, err
|
return GetEventMetadataResult{}, err
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
// TODO return a constant or a specific error type for Not Found
|
// TODO return a constant or a specific error type for Not Found
|
||||||
return GetEventResult{}, errors.New("not found")
|
return GetEventMetadataResult{}, errors.New("not found")
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetEventResponsesQuery struct {
|
||||||
|
AlphaID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetEventResponsesResult struct {
|
||||||
|
TotalResponses int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Store) GetEventResponses(ctx context.Context, query GetEventResponsesQuery) (GetEventResponsesResult, error) {
|
||||||
|
conn := s.pool.Get(ctx)
|
||||||
|
defer s.pool.Put(conn)
|
||||||
|
|
||||||
|
var result GetEventResponsesResult
|
||||||
|
const dbQuery = `
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM response
|
||||||
|
JOIN event ON response.event_id = event.id
|
||||||
|
WHERE event.alpha_id = ?;`
|
||||||
|
err := sqlitex.Exec(conn, dbQuery,
|
||||||
|
func(stmt *sqlite.Stmt) error {
|
||||||
|
result.TotalResponses = stmt.ColumnInt(0)
|
||||||
|
return nil
|
||||||
|
}, query.AlphaID)
|
||||||
|
// TODO return an error if the event does not exist?
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
hm "gitlab.codemonkeysoftware.net/b/hatmill"
|
hm "gitlab.codemonkeysoftware.net/b/hatmill"
|
||||||
@ -122,7 +123,7 @@ func voteForm(disabled bool, st voteState) hm.Term {
|
|||||||
|
|
||||||
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) handleVote(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
event, err := h.store.GetEvent(context.Background(), back.GetEventQuery{
|
event, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
|
||||||
AlphaID: query.Get(keyEventID),
|
AlphaID: query.Get(keyEventID),
|
||||||
})
|
})
|
||||||
// TODO return 404 if event not found
|
// TODO return 404 if event not found
|
||||||
@ -254,9 +255,10 @@ func (h *handler) handleDoCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
|
eventID := query.Get(keyEventID)
|
||||||
// TODO authenticate with admin code
|
// TODO authenticate with admin code
|
||||||
event, err := h.store.GetEvent(context.Background(), back.GetEventQuery{
|
metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{
|
||||||
AlphaID: query.Get(keyEventID),
|
AlphaID: eventID,
|
||||||
})
|
})
|
||||||
// TODO return 404 if event not found
|
// TODO return 404 if event not found
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -264,24 +266,33 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
responses, err := h.store.GetEventResponses(context.Background(), back.GetEventResponsesQuery{
|
||||||
|
AlphaID: eventID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprint(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO show results (number of responses, grid)
|
||||||
body := hm.Terms{
|
body := hm.Terms{
|
||||||
e.Form()(
|
e.Form()(
|
||||||
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
|
e.Label(a.For(fieldNameEventName))(hm.Text("Event name")),
|
||||||
e.Input(
|
e.Input(
|
||||||
a.Name(fieldNameEventName),
|
a.Name(fieldNameEventName),
|
||||||
a.Value(event.Name),
|
a.Value(metadata.Name),
|
||||||
),
|
),
|
||||||
e.Br(),
|
e.Br(),
|
||||||
|
|
||||||
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
|
e.Label(a.For(fieldNameDescription))(hm.Text("Description")),
|
||||||
e.Textarea(a.Name(fieldNameDescription))(hm.Text(event.Description)),
|
e.Textarea(a.Name(fieldNameDescription))(hm.Text(metadata.Description)),
|
||||||
e.Br(),
|
e.Br(),
|
||||||
|
|
||||||
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
|
e.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")),
|
||||||
e.Input(
|
e.Input(
|
||||||
a.Name(fieldNameEarliest),
|
a.Name(fieldNameEarliest),
|
||||||
a.Type("date"),
|
a.Type("date"),
|
||||||
a.Value(event.EarliestDate.Format(formDateLayout)),
|
a.Value(metadata.EarliestDate.Format(formDateLayout)),
|
||||||
),
|
),
|
||||||
e.Br(),
|
e.Br(),
|
||||||
|
|
||||||
@ -289,12 +300,14 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) {
|
|||||||
e.Input(
|
e.Input(
|
||||||
a.Name(fieldNameLatest),
|
a.Name(fieldNameLatest),
|
||||||
a.Type("date"),
|
a.Type("date"),
|
||||||
a.Value(event.LatestDate.Format(formDateLayout)),
|
a.Value(metadata.LatestDate.Format(formDateLayout)),
|
||||||
),
|
),
|
||||||
e.Br(),
|
e.Br(),
|
||||||
|
|
||||||
e.Input(a.Type("submit")),
|
e.Input(a.Type("submit")),
|
||||||
),
|
),
|
||||||
|
e.H3()(hm.Text("Responses")),
|
||||||
|
e.P()(hm.Text(strconv.Itoa(responses.TotalResponses))),
|
||||||
}
|
}
|
||||||
_ = h.writePage(w, "Edit your event", body)
|
_ = h.writePage(w, "Edit your event", body)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user