2024-03-05 06:42:37 +00:00
|
|
|
package port
|
|
|
|
|
|
|
|
import (
|
2024-03-07 05:52:03 +00:00
|
|
|
"context"
|
2024-03-05 06:42:37 +00:00
|
|
|
"net/http"
|
2024-03-07 05:52:03 +00:00
|
|
|
"slices"
|
2024-03-06 07:14:31 +00:00
|
|
|
"strconv"
|
2024-03-05 06:42:37 +00:00
|
|
|
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
|
|
|
"gitea.dwysokinski.me/twhelp/corev3/internal/port/internal/apimodel"
|
2024-03-07 05:52:03 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2024-03-05 06:42:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *apiHTTPHandler) ListVillages(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
serverKey apimodel.ServerKeyPathParam,
|
|
|
|
params apimodel.ListVillagesParams,
|
|
|
|
) {
|
|
|
|
domainParams := domain.NewListVillagesParams()
|
|
|
|
|
|
|
|
if err := domainParams.SetSort([]domain.VillageSort{domain.VillageSortIDASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-06 07:14:31 +00:00
|
|
|
if params.Coords != nil {
|
|
|
|
if err := domainParams.SetCoordsString(*params.Coords); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 06:42:37 +00:00
|
|
|
if params.Limit != nil {
|
|
|
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cursor != nil {
|
|
|
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 06:35:26 +00:00
|
|
|
res, err := h.villageSvc.ListWithRelations(r.Context(), domainParams)
|
2024-03-05 06:42:37 +00:00
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewListVillagesResponse(res))
|
|
|
|
}
|
|
|
|
|
2024-03-09 07:38:23 +00:00
|
|
|
func (h *apiHTTPHandler) ListPlayerVillages(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
serverKey apimodel.ServerKeyPathParam,
|
|
|
|
playerID apimodel.PlayerIdPathParam,
|
|
|
|
params apimodel.ListPlayerVillagesParams,
|
|
|
|
) {
|
|
|
|
domainParams := domain.NewListVillagesParams()
|
|
|
|
|
|
|
|
if err := domainParams.SetSort([]domain.VillageSort{domain.VillageSortIDASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetPlayerIDs([]int{playerID}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Limit != nil {
|
|
|
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cursor != nil {
|
|
|
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := h.villageSvc.ListWithRelations(r.Context(), domainParams)
|
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewListVillagesResponse(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *apiHTTPHandler) ListTribeVillages(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
serverKey apimodel.ServerKeyPathParam,
|
|
|
|
tribeID apimodel.TribeIdPathParam,
|
|
|
|
params apimodel.ListTribeVillagesParams,
|
|
|
|
) {
|
|
|
|
domainParams := domain.NewListVillagesParams()
|
|
|
|
|
|
|
|
if err := domainParams.SetSort([]domain.VillageSort{domain.VillageSortIDASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetTribeIDs([]int{tribeID}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Limit != nil {
|
|
|
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cursor != nil {
|
|
|
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListVillagesErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := h.villageSvc.ListWithRelations(r.Context(), domainParams)
|
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewListVillagesResponse(res))
|
|
|
|
}
|
|
|
|
|
2024-03-07 05:52:03 +00:00
|
|
|
func (h *apiHTTPHandler) GetVillage(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
_ apimodel.ServerKeyPathParam,
|
|
|
|
_ apimodel.VillageIdPathParam,
|
|
|
|
) {
|
|
|
|
village, _ := villageFromContext(r.Context())
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewGetVillageResponse(village))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *apiHTTPHandler) villageMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
routeCtx := chi.RouteContext(ctx)
|
|
|
|
server, serverOK := serverFromContext(ctx)
|
|
|
|
villageIDIdx := slices.Index(routeCtx.URLParams.Keys, "villageId")
|
|
|
|
|
|
|
|
if !serverOK || villageIDIdx < 0 {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
villageID, err := strconv.Atoi(routeCtx.URLParams.Values[villageIDIdx])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
village, err := h.villageSvc.GetWithRelations(
|
|
|
|
ctx,
|
|
|
|
villageID,
|
|
|
|
server.Key(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatGetVillageErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(villageToContext(ctx, village)))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type villageCtxKey struct{}
|
|
|
|
|
|
|
|
func villageToContext(ctx context.Context, v domain.VillageWithRelations) context.Context {
|
|
|
|
return context.WithValue(ctx, villageCtxKey{}, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func villageFromContext(ctx context.Context) (domain.VillageWithRelations, bool) {
|
|
|
|
v, ok := ctx.Value(villageCtxKey{}).(domain.VillageWithRelations)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2024-03-05 06:42:37 +00:00
|
|
|
func formatListVillagesErrorPath(segments []domain.ErrorPathSegment) []string {
|
|
|
|
if segments[0].Model != "ListVillagesParams" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch segments[0].Field {
|
|
|
|
case "cursor":
|
|
|
|
return []string{"$query", "cursor"}
|
|
|
|
case "limit":
|
|
|
|
return []string{"$query", "limit"}
|
2024-03-06 07:14:31 +00:00
|
|
|
case "coords":
|
|
|
|
path := []string{"$query", "coords"}
|
|
|
|
if segments[0].Index >= 0 {
|
|
|
|
path = append(path, strconv.Itoa(segments[0].Index))
|
|
|
|
}
|
|
|
|
return path
|
2024-03-05 06:42:37 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-03-07 05:52:03 +00:00
|
|
|
|
|
|
|
func formatGetVillageErrorPath(segments []domain.ErrorPathSegment) []string {
|
|
|
|
if segments[0].Model != "ListVillagesParams" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch segments[0].Field {
|
|
|
|
case "ids":
|
|
|
|
return []string{"$path", "villageId"}
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|