This repository has been archived on 2019-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
scoreboard/features/group/handler.go

59 lines
1.8 KiB
Go
Raw Normal View History

2019-05-27 21:46:09 +00:00
package group
import (
"errors"
"html/template"
"net/http"
"git.cliffbreak.de/haveachin/scoreboard/data"
"git.cliffbreak.de/haveachin/scoreboard/features"
"git.cliffbreak.de/haveachin/scoreboard/features/weberror"
"git.cliffbreak.de/haveachin/scoreboard/response"
"git.cliffbreak.de/haveachin/scoreboard/service/filter"
"git.cliffbreak.de/haveachin/scoreboard/service/id"
"github.com/go-chi/chi"
)
func queryOneHandler(s features.Service, t *template.Template) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.WebHandler(w, response.WebHandlerFunc(func() (response.Page, int) {
if chi.URLParam(r, "id") == "" {
return weberror.NewPage(*t, errors.New("No id in request"), http.StatusBadRequest), http.StatusBadRequest
}
id, err := id.ParseFromRequest(r)
if err != nil {
return weberror.NewPage(*t, err, http.StatusBadRequest), http.StatusBadRequest
}
var group data.Group
if err := s.QueryOne(&group, &filter.Filter{"_id": *id}); err != nil {
return weberror.NewPage(*t, err, http.StatusBadRequest), http.StatusBadRequest
}
var games []data.Game
if err := s.Query(&games, &filter.Filter{}); err != nil {
return weberror.NewPage(*t, err, http.StatusBadRequest), http.StatusBadRequest
}
return newDetailPage(*t, group, games), http.StatusOK
}))
})
}
func queryHandler(s features.Service, t *template.Template) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response.WebHandler(w, response.WebHandlerFunc(func() (response.Page, int) {
var groups []data.Group
if err := s.Query(&groups, &filter.Filter{}); err != nil {
return weberror.NewPage(*t, err, http.StatusBadRequest), http.StatusBadRequest
}
return newMasterPage(*t, groups), http.StatusOK
}))
})
}