2024-03-11 06:41:32 +00:00
|
|
|
package port
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-03-11 09:05:29 +00:00
|
|
|
"strconv"
|
2024-03-11 06:41:32 +00:00
|
|
|
|
2024-04-06 04:32:29 +00:00
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/domain"
|
|
|
|
"gitea.dwysokinski.me/twhelp/core/internal/port/internal/apimodel"
|
2024-03-11 06:41:32 +00:00
|
|
|
)
|
|
|
|
|
2024-03-25 07:29:37 +00:00
|
|
|
const apiEnnoblementSortMaxLength = 1
|
|
|
|
|
|
|
|
var apiEnnoblementSortAllowedValues = []domain.EnnoblementSort{
|
|
|
|
domain.EnnoblementSortCreatedAtASC,
|
|
|
|
domain.EnnoblementSortCreatedAtDESC,
|
|
|
|
}
|
|
|
|
|
2024-03-11 09:05:29 +00:00
|
|
|
//nolint:gocyclo
|
2024-03-11 06:41:32 +00:00
|
|
|
func (h *apiHTTPHandler) ListEnnoblements(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
serverKey apimodel.ServerKeyPathParam,
|
|
|
|
params apimodel.ListEnnoblementsParams,
|
|
|
|
) {
|
|
|
|
domainParams := domain.NewListEnnoblementsParams()
|
|
|
|
|
2024-03-11 09:05:29 +00:00
|
|
|
if err := domainParams.SetSort([]domain.EnnoblementSort{domain.EnnoblementSortIDASC}); err != nil {
|
2024-03-11 06:41:32 +00:00
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:08:51 +00:00
|
|
|
if params.Sort != nil {
|
2024-03-27 06:39:39 +00:00
|
|
|
if err := domainParams.PrependSortString(
|
|
|
|
*params.Sort,
|
|
|
|
apiEnnoblementSortAllowedValues,
|
|
|
|
apiEnnoblementSortMaxLength,
|
|
|
|
); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := domainParams.PrependSort([]domain.EnnoblementSort{domain.EnnoblementSortCreatedAtASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 12:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).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(formatListEnnoblementsErrorPath).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(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Limit != nil {
|
|
|
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cursor != nil {
|
|
|
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := h.ennoblementSvc.ListWithRelations(r.Context(), domainParams)
|
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewListEnnoblementsResponse(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (h *apiHTTPHandler) ListPlayerEnnoblements(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
serverKey apimodel.ServerKeyPathParam,
|
|
|
|
playerID apimodel.PlayerIdPathParam,
|
|
|
|
params apimodel.ListPlayerEnnoblementsParams,
|
|
|
|
) {
|
|
|
|
domainParams := domain.NewListEnnoblementsParams()
|
|
|
|
|
|
|
|
if err := domainParams.SetSort([]domain.EnnoblementSort{domain.EnnoblementSortIDASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetPlayerIDs([]int{playerID}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Sort != nil {
|
2024-03-27 06:39:39 +00:00
|
|
|
if err := domainParams.PrependSortString(
|
|
|
|
*params.Sort,
|
|
|
|
apiEnnoblementSortAllowedValues,
|
|
|
|
apiEnnoblementSortMaxLength,
|
|
|
|
); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := domainParams.PrependSort([]domain.EnnoblementSort{domain.EnnoblementSortCreatedAtASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 12:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).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(formatListEnnoblementsErrorPath).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(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Limit != nil {
|
|
|
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cursor != nil {
|
|
|
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := h.ennoblementSvc.ListWithRelations(r.Context(), domainParams)
|
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewListEnnoblementsResponse(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (h *apiHTTPHandler) ListTribeEnnoblements(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
serverKey apimodel.ServerKeyPathParam,
|
|
|
|
tribeID apimodel.TribeIdPathParam,
|
|
|
|
params apimodel.ListTribeEnnoblementsParams,
|
|
|
|
) {
|
|
|
|
domainParams := domain.NewListEnnoblementsParams()
|
|
|
|
|
|
|
|
if err := domainParams.SetSort([]domain.EnnoblementSort{domain.EnnoblementSortIDASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetTribeIDs([]int{tribeID}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Sort != nil {
|
2024-03-27 06:39:39 +00:00
|
|
|
if err := domainParams.PrependSortString(
|
|
|
|
*params.Sort,
|
|
|
|
apiEnnoblementSortAllowedValues,
|
|
|
|
apiEnnoblementSortMaxLength,
|
|
|
|
); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := domainParams.PrependSort([]domain.EnnoblementSort{domain.EnnoblementSortCreatedAtASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 12:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).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(formatListEnnoblementsErrorPath).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(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Limit != nil {
|
|
|
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cursor != nil {
|
|
|
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := h.ennoblementSvc.ListWithRelations(r.Context(), domainParams)
|
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewListEnnoblementsResponse(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:gocyclo
|
|
|
|
func (h *apiHTTPHandler) ListVillageEnnoblements(
|
|
|
|
w http.ResponseWriter,
|
|
|
|
r *http.Request,
|
|
|
|
_ apimodel.VersionCodePathParam,
|
|
|
|
serverKey apimodel.ServerKeyPathParam,
|
|
|
|
villageID apimodel.VillageIdPathParam,
|
|
|
|
params apimodel.ListVillageEnnoblementsParams,
|
|
|
|
) {
|
|
|
|
domainParams := domain.NewListEnnoblementsParams()
|
|
|
|
|
|
|
|
if err := domainParams.SetSort([]domain.EnnoblementSort{domain.EnnoblementSortIDASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := domainParams.SetVillageIDs([]int{villageID}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 09:05:29 +00:00
|
|
|
if params.Sort != nil {
|
2024-03-27 06:39:39 +00:00
|
|
|
if err := domainParams.PrependSortString(
|
|
|
|
*params.Sort,
|
|
|
|
apiEnnoblementSortAllowedValues,
|
|
|
|
apiEnnoblementSortMaxLength,
|
|
|
|
); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := domainParams.PrependSort([]domain.EnnoblementSort{domain.EnnoblementSortCreatedAtASC}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 09:05:29 +00:00
|
|
|
}
|
|
|
|
|
2024-03-11 06:41:32 +00:00
|
|
|
if err := domainParams.SetServerKeys([]string{serverKey}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 09:05:29 +00:00
|
|
|
if params.Before != nil {
|
|
|
|
if err := domainParams.SetBefore(domain.NullTime{
|
|
|
|
V: *params.Before,
|
|
|
|
Valid: true,
|
|
|
|
}); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).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(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 06:41:32 +00:00
|
|
|
if params.Limit != nil {
|
|
|
|
if err := domainParams.SetLimit(*params.Limit); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if params.Cursor != nil {
|
|
|
|
if err := domainParams.SetEncodedCursor(*params.Cursor); err != nil {
|
|
|
|
h.errorRenderer.withErrorPathFormatter(formatListEnnoblementsErrorPath).render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := h.ennoblementSvc.ListWithRelations(r.Context(), domainParams)
|
|
|
|
if err != nil {
|
|
|
|
h.errorRenderer.render(w, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
renderJSON(w, r, http.StatusOK, apimodel.NewListEnnoblementsResponse(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatListEnnoblementsErrorPath(segments []domain.ErrorPathSegment) []string {
|
|
|
|
if segments[0].Model != "ListEnnoblementsParams" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch segments[0].Field {
|
|
|
|
case "cursor":
|
|
|
|
return []string{"$query", "cursor"}
|
|
|
|
case "limit":
|
|
|
|
return []string{"$query", "limit"}
|
2024-03-11 09:05:29 +00:00
|
|
|
case "sort":
|
|
|
|
path := []string{"$query", "sort"}
|
|
|
|
if segments[0].Index >= 0 {
|
|
|
|
path = append(path, strconv.Itoa(segments[0].Index))
|
|
|
|
}
|
|
|
|
return path
|
2024-03-11 06:41:32 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|