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/game/handler.go

59 lines
1.8 KiB
Go
Raw Normal View History

2019-05-27 21:46:09 +00:00
package game
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 game data.Game
if err := s.QueryOne(&game, &filter.Filter{"_id": *id}); err != nil {
return weberror.NewPage(*t, err, http.StatusBadRequest), http.StatusBadRequest
}
var groups []data.Group
if err := s.Query(&groups, &filter.Filter{}); err != nil {
return weberror.NewPage(*t, err, http.StatusBadRequest), http.StatusBadRequest
}
return newDetailPage(*t, game, groups), 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 games []data.Game
if err := s.Query(&games, &filter.Filter{}); err != nil {
return weberror.NewPage(*t, err, http.StatusBadRequest), http.StatusBadRequest
}
return newMasterPage(*t, games), http.StatusOK
}))
})
}