193 lines
5.4 KiB
Go
193 lines
5.4 KiB
Go
|
package port
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
|
||
|
"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
|
||
|
"gitea.dwysokinski.me/twhelp/corev3/internal/port/internal/apimodel"
|
||
|
)
|
||
|
|
||
|
//nolint:gocyclo
|
||
|
func (h *apiHTTPHandler) ListPlayerTribeChanges(
|
||
|
w http.ResponseWriter,
|
||
|
r *http.Request,
|
||
|
_ apimodel.VersionCodePathParam,
|
||
|
serverKey apimodel.ServerKeyPathParam,
|
||
|
playerID apimodel.PlayerIdPathParam,
|
||
|
params apimodel.ListPlayerTribeChangesParams,
|
||
|
) {
|
||
|
domainParams := domain.NewListTribeChangesParams()
|
||
|
|
||
|
if err := domainParams.SetSort([]domain.TribeChangeSort{domain.TribeChangeSortIDASC}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := domainParams.SetPlayerIDs([]int{playerID}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if params.Sort != nil {
|
||
|
if err := domainParams.PrependSortString(*params.Sort); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
} else {
|
||
|
if err := domainParams.PrependSortString([]string{domain.TribeChangeSortCreatedAtASC.String()}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if params.Before != nil {
|
||
|
if err := domainParams.SetBefore(domain.NullTime{
|
||
|
V: *params.Before,
|
||
|
Valid: true,
|
||
|
}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if params.Since != nil {
|
||
|
if err := domainParams.SetSince(domain.NullTime{
|
||
|
V: *params.Since,
|
||
|
Valid: true,
|
||
|
}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if params.Limit != nil {
|
||
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if params.Cursor != nil {
|
||
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
res, err := h.tribeChangeSvc.ListWithRelations(r.Context(), domainParams)
|
||
|
if err != nil {
|
||
|
h.errorRenderer.render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
renderJSON(w, r, http.StatusOK, apimodel.NewListTribeChangesResponse(res))
|
||
|
}
|
||
|
|
||
|
//nolint:gocyclo
|
||
|
func (h *apiHTTPHandler) ListTribeTribeChanges(
|
||
|
w http.ResponseWriter,
|
||
|
r *http.Request,
|
||
|
_ apimodel.VersionCodePathParam,
|
||
|
serverKey apimodel.ServerKeyPathParam,
|
||
|
tribeID apimodel.TribeIdPathParam,
|
||
|
params apimodel.ListTribeTribeChangesParams,
|
||
|
) {
|
||
|
domainParams := domain.NewListTribeChangesParams()
|
||
|
|
||
|
if err := domainParams.SetSort([]domain.TribeChangeSort{domain.TribeChangeSortIDASC}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if err := domainParams.SetTribeIDs([]int{tribeID}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if params.Sort != nil {
|
||
|
if err := domainParams.PrependSortString(*params.Sort); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
} else {
|
||
|
if err := domainParams.PrependSortString([]string{domain.TribeChangeSortCreatedAtASC.String()}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if params.Before != nil {
|
||
|
if err := domainParams.SetBefore(domain.NullTime{
|
||
|
V: *params.Before,
|
||
|
Valid: true,
|
||
|
}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if params.Since != nil {
|
||
|
if err := domainParams.SetSince(domain.NullTime{
|
||
|
V: *params.Since,
|
||
|
Valid: true,
|
||
|
}); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if params.Limit != nil {
|
||
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if params.Cursor != nil {
|
||
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
||
|
h.errorRenderer.withErrorPathFormatter(formatListTribeChangesErrorPath).render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
res, err := h.tribeChangeSvc.ListWithRelations(r.Context(), domainParams)
|
||
|
if err != nil {
|
||
|
h.errorRenderer.render(w, r, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
renderJSON(w, r, http.StatusOK, apimodel.NewListTribeChangesResponse(res))
|
||
|
}
|
||
|
|
||
|
func formatListTribeChangesErrorPath(segments []domain.ErrorPathSegment) []string {
|
||
|
if segments[0].Model != "ListTribeChangesParams" {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
switch segments[0].Field {
|
||
|
case "cursor":
|
||
|
return []string{"$query", "cursor"}
|
||
|
case "limit":
|
||
|
return []string{"$query", "limit"}
|
||
|
case "sort":
|
||
|
path := []string{"$query", "sort"}
|
||
|
if segments[0].Index >= 0 {
|
||
|
path = append(path, strconv.Itoa(segments[0].Index))
|
||
|
}
|
||
|
return path
|
||
|
default:
|
||
|
return nil
|
||
|
}
|
||
|
}
|