244 lines
7.5 KiB
Go
244 lines
7.5 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"mixmaker/backend/internal/domain"
|
|
)
|
|
|
|
func (s *Server) cancelEvent(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.CancelEvent(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) deleteEvent(w http.ResponseWriter, r *http.Request) {
|
|
err := s.service.DeleteEvent(r.Context(), who(r).account, chi.URLParam(r, "eventID"))
|
|
respond(w, nil, err, http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) closeRegistration(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
RulesetID string `json:"rulesetId"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.CloseRegistration(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.RulesetID, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) generateWorkflowBalance(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.GenerateBalance(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) getRoster(w http.ResponseWriter, r *http.Request) {
|
|
out, err := s.store.GetRoster(r.Context(), chi.URLParam(r, "eventID"))
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) selectWorkflowBalance(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Candidate domain.BalanceCandidate `json:"candidate"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.SelectWorkflowBalance(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.Candidate, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) swapRoster(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
TeamAID string `json:"teamAId"`
|
|
PlayerAID string `json:"playerAId"`
|
|
TeamBID string `json:"teamBId"`
|
|
PlayerBID string `json:"playerBId"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.SwapRoster(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.TeamAID, in.PlayerAID, in.TeamBID, in.PlayerBID, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) substituteRoster(w http.ResponseWriter, r *http.Request) {
|
|
s.handleSubstitute(w, r, false)
|
|
}
|
|
|
|
func (s *Server) emergencySubstitute(w http.ResponseWriter, r *http.Request) {
|
|
s.handleSubstitute(w, r, true)
|
|
}
|
|
|
|
func (s *Server) handleSubstitute(w http.ResponseWriter, r *http.Request, emergency bool) {
|
|
var in struct {
|
|
TeamID string `json:"teamId"`
|
|
OutgoingPlayerID string `json:"outgoingPlayerId"`
|
|
ReservePlayerID string `json:"reservePlayerId"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.SubstituteRoster(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.TeamID, in.OutgoingPlayerID, in.ReservePlayerID, in.ExpectedVersion, emergency)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) setRosterCaptain(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
TeamID string `json:"teamId"`
|
|
PlayerID string `json:"playerId"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.SetRosterCaptain(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.TeamID, in.PlayerID, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) confirmRosters(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
ExpectedRosterVersion int `json:"expectedRosterVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.ConfirmRosters(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.ExpectedVersion, in.ExpectedRosterVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) startScrim(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
out, err := s.service.StartScrim(r.Context(), who(r).account, chi.URLParam(r, "eventID"), in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusCreated)
|
|
}
|
|
|
|
func (s *Server) tossSeriesCoin(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Seed string `json:"seed"`
|
|
ActingTeamID string `json:"actingTeamId"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
series, err := s.store.GetSeries(r.Context(), chi.URLParam(r, "id"))
|
|
if err == nil {
|
|
err = s.authorizeTeam(r, series.EventID, in.ActingTeamID)
|
|
}
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
out, err := s.service.TossSeriesCoin(r.Context(), who(r).account, series.ID, in.Seed, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) banSeriesMap(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
TeamID string `json:"teamId"`
|
|
Map string `json:"map"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
series, err := s.store.GetSeries(r.Context(), chi.URLParam(r, "id"))
|
|
if err == nil {
|
|
err = s.authorizeTeam(r, series.EventID, in.TeamID)
|
|
}
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
out, err := s.service.BanSeriesMap(r.Context(), who(r).account, series.ID, in.TeamID, in.Map, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) pickSeriesMap(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
TeamID string `json:"teamId"`
|
|
Map string `json:"map"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
series, err := s.store.GetSeries(r.Context(), chi.URLParam(r, "id"))
|
|
if err == nil {
|
|
err = s.authorizeTeam(r, series.EventID, in.TeamID)
|
|
}
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
out, err := s.service.PickSeriesMap(r.Context(), who(r).account, series.ID, in.TeamID, in.Map, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) banSeriesHero(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
TeamID string `json:"teamId"`
|
|
Hero string `json:"hero"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
series, err := s.store.GetSeries(r.Context(), chi.URLParam(r, "id"))
|
|
if err == nil {
|
|
err = s.authorizeTeam(r, series.EventID, in.TeamID)
|
|
}
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
out, err := s.service.BanSeriesHero(r.Context(), who(r).account, series.ID, in.TeamID, in.Hero, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|
|
|
|
func (s *Server) recordSeriesResult(w http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
Outcome domain.MapOutcome `json:"outcome"`
|
|
ActingTeamID string `json:"actingTeamId"`
|
|
ExpectedVersion int `json:"expectedVersion"`
|
|
}
|
|
if !decode(w, r, &in) {
|
|
return
|
|
}
|
|
series, err := s.store.GetSeries(r.Context(), chi.URLParam(r, "id"))
|
|
if err == nil {
|
|
err = s.authorizeTeam(r, series.EventID, in.ActingTeamID)
|
|
}
|
|
if err != nil {
|
|
writeError(w, err)
|
|
return
|
|
}
|
|
out, err := s.service.RecordSeriesResult(r.Context(), who(r).account, series.ID, in.Outcome, in.ExpectedVersion)
|
|
respond(w, out, err, http.StatusOK)
|
|
}
|