diff --git a/back/store.go b/back/store.go index 9b53c25..276ab95 100644 --- a/back/store.go +++ b/back/store.go @@ -133,17 +133,17 @@ func (s *Store) CreateEvent(ctx context.Context, cmd CreateEventCommand) (result return } -type GetEventQuery struct { +type GetEventMetadataQuery struct { AlphaID string } -type GetEventResult struct { +type GetEventMetadataResult struct { Name string Description string 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) 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 FROM event WHERE alpha_id = ?;` - var result GetEventResult + var result GetEventMetadataResult var found bool err := sqlitex.Exec(conn, dbQuery, func(stmt *sqlite.Stmt) error { @@ -171,11 +171,38 @@ func (s *Store) GetEvent(ctx context.Context, query GetEventQuery) (GetEventResu return err }, query.AlphaID) if err != nil { - return GetEventResult{}, err + return GetEventMetadataResult{}, err } if !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 } + +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 +} diff --git a/front/server.go b/front/server.go index b99ec8b..bfd503d 100644 --- a/front/server.go +++ b/front/server.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "net/url" + "strconv" "time" 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) { 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), }) // 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) { query := r.URL.Query() + eventID := query.Get(keyEventID) // TODO authenticate with admin code - event, err := h.store.GetEvent(context.Background(), back.GetEventQuery{ - AlphaID: query.Get(keyEventID), + metadata, err := h.store.GetEventMetadata(context.Background(), back.GetEventMetadataQuery{ + AlphaID: eventID, }) // TODO return 404 if event not found if err != nil { @@ -264,24 +266,33 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) { 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{ e.Form()( e.Label(a.For(fieldNameEventName))(hm.Text("Event name")), e.Input( a.Name(fieldNameEventName), - a.Value(event.Name), + a.Value(metadata.Name), ), e.Br(), 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.Label(a.For(fieldNameEarliest))(hm.Text("Earliest date")), e.Input( a.Name(fieldNameEarliest), a.Type("date"), - a.Value(event.EarliestDate.Format(formDateLayout)), + a.Value(metadata.EarliestDate.Format(formDateLayout)), ), e.Br(), @@ -289,12 +300,14 @@ func (h *handler) handleAdmin(w http.ResponseWriter, r *http.Request) { e.Input( a.Name(fieldNameLatest), a.Type("date"), - a.Value(event.LatestDate.Format(formDateLayout)), + a.Value(metadata.LatestDate.Format(formDateLayout)), ), e.Br(), 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) }