feat: implement battle replay controller, service, and routes with Swagger documentation
Build and Release / release (push) Successful in 3m2s
Build and Release / release (push) Successful in 3m2s
This commit is contained in:
@@ -2,8 +2,10 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"history-api/internal/dtos/request"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/services"
|
||||
"history-api/pkg/validator"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
@@ -70,3 +72,40 @@ func (h *BattleReplayController) GetBattleReplaysByGeometryId(c fiber.Ctx) error
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
// GetBattleReplaysByGeometryIDs handles fetching battle replays by a list of geometry IDs.
|
||||
// @Summary Get battle replays by geometry IDs
|
||||
// @Description Get battle replays grouped by geometry IDs
|
||||
// @Tags BattleReplays
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param query query request.GetBattleReplaysByGeometryIDsDto true "Query Parameters"
|
||||
// @Success 200 {object} response.CommonResponse
|
||||
// @Failure 400 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /battle-replays/geometries [get]
|
||||
func (h *BattleReplayController) GetBattleReplaysByGeometryIDs(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
dto := &request.GetBattleReplaysByGeometryIDsDto{}
|
||||
if err := validator.ValidateQueryDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
|
||||
res, err := h.service.GetByGeometryIDs(ctx, dto)
|
||||
if err != nil {
|
||||
return c.Status(err.Code).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Message,
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package request
|
||||
|
||||
type GetBattleReplaysByGeometryIDsDto struct {
|
||||
GeometryIDs []string `json:"geometry_ids" query:"geometry_ids" validate:"required,min=1,dive,uuid"`
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
func BattleReplayRoutes(router fiber.Router, battleReplayController *controllers.BattleReplayController) {
|
||||
br := router.Group("/battle-replays")
|
||||
br.Get("/geometries", battleReplayController.GetBattleReplaysByGeometryIDs)
|
||||
br.Get("/geometry/:geometryId", battleReplayController.GetBattleReplaysByGeometryId)
|
||||
br.Get("/:id", battleReplayController.GetBattleReplayById)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"history-api/internal/dtos/request"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/models"
|
||||
"history-api/internal/repositories"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
type BattleReplayService interface {
|
||||
GetByID(ctx context.Context, id string) (*response.BattleReplayResponse, *fiber.Error)
|
||||
GetByGeometryID(ctx context.Context, geometryID string) ([]*response.BattleReplayResponse, *fiber.Error)
|
||||
GetByGeometryIDs(ctx context.Context, req *request.GetBattleReplaysByGeometryIDsDto) (map[string][]*response.BattleReplayResponse, *fiber.Error)
|
||||
}
|
||||
|
||||
type battleReplayService struct {
|
||||
@@ -52,3 +54,26 @@ func (s *battleReplayService) GetByGeometryID(ctx context.Context, geometryID st
|
||||
|
||||
return models.BattleReplaysEntityToResponse(replays), nil
|
||||
}
|
||||
|
||||
func (s *battleReplayService) GetByGeometryIDs(ctx context.Context, req *request.GetBattleReplaysByGeometryIDsDto) (map[string][]*response.BattleReplayResponse, *fiber.Error) {
|
||||
replays, err := s.battleReplayRepo.GetByGeometryIDs(ctx, req.GeometryIDs)
|
||||
if err != nil {
|
||||
return nil, fiber.NewError(fiber.StatusInternalServerError, "Failed to get battle replays")
|
||||
}
|
||||
|
||||
result := make(map[string][]*response.BattleReplayResponse)
|
||||
for _, idStr := range req.GeometryIDs {
|
||||
result[idStr] = make([]*response.BattleReplayResponse, 0)
|
||||
}
|
||||
|
||||
for _, replay := range replays {
|
||||
if replay != nil {
|
||||
geomID := replay.GeometryID
|
||||
if _, exists := result[geomID]; exists {
|
||||
result[geomID] = append(result[geomID], replay.ToResponse())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user