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:
@@ -450,6 +450,54 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/battle-replays/geometries": {
|
||||||
|
"get": {
|
||||||
|
"description": "Get battle replays grouped by geometry IDs",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"BattleReplays"
|
||||||
|
],
|
||||||
|
"summary": "Get battle replays by geometry IDs",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"minItems": 1,
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"collectionFormat": "csv",
|
||||||
|
"name": "geometry_ids",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "Bad Request",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Internal Server Error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/battle-replays/geometry/{geometryId}": {
|
"/battle-replays/geometry/{geometryId}": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Get all battle replays associated with a specific geometry",
|
"description": "Get all battle replays associated with a specific geometry",
|
||||||
|
|||||||
@@ -443,6 +443,54 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/battle-replays/geometries": {
|
||||||
|
"get": {
|
||||||
|
"description": "Get battle replays grouped by geometry IDs",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"BattleReplays"
|
||||||
|
],
|
||||||
|
"summary": "Get battle replays by geometry IDs",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"minItems": 1,
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"collectionFormat": "csv",
|
||||||
|
"name": "geometry_ids",
|
||||||
|
"in": "query",
|
||||||
|
"required": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "OK",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "Bad Request",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"500": {
|
||||||
|
"description": "Internal Server Error",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/definitions/history-api_internal_dtos_response.CommonResponse"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"/battle-replays/geometry/{geometryId}": {
|
"/battle-replays/geometry/{geometryId}": {
|
||||||
"get": {
|
"get": {
|
||||||
"description": "Get all battle replays associated with a specific geometry",
|
"description": "Get all battle replays associated with a specific geometry",
|
||||||
|
|||||||
@@ -1070,6 +1070,38 @@ paths:
|
|||||||
summary: Get battle replay by ID
|
summary: Get battle replay by ID
|
||||||
tags:
|
tags:
|
||||||
- BattleReplays
|
- BattleReplays
|
||||||
|
/battle-replays/geometries:
|
||||||
|
get:
|
||||||
|
consumes:
|
||||||
|
- application/json
|
||||||
|
description: Get battle replays grouped by geometry IDs
|
||||||
|
parameters:
|
||||||
|
- collectionFormat: csv
|
||||||
|
in: query
|
||||||
|
items:
|
||||||
|
type: string
|
||||||
|
minItems: 1
|
||||||
|
name: geometry_ids
|
||||||
|
required: true
|
||||||
|
type: array
|
||||||
|
produces:
|
||||||
|
- application/json
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: OK
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/history-api_internal_dtos_response.CommonResponse'
|
||||||
|
"400":
|
||||||
|
description: Bad Request
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/history-api_internal_dtos_response.CommonResponse'
|
||||||
|
"500":
|
||||||
|
description: Internal Server Error
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/history-api_internal_dtos_response.CommonResponse'
|
||||||
|
summary: Get battle replays by geometry IDs
|
||||||
|
tags:
|
||||||
|
- BattleReplays
|
||||||
/battle-replays/geometry/{geometryId}:
|
/battle-replays/geometry/{geometryId}:
|
||||||
get:
|
get:
|
||||||
consumes:
|
consumes:
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"history-api/internal/dtos/request"
|
||||||
"history-api/internal/dtos/response"
|
"history-api/internal/dtos/response"
|
||||||
"history-api/internal/services"
|
"history-api/internal/services"
|
||||||
|
"history-api/pkg/validator"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
@@ -70,3 +72,40 @@ func (h *BattleReplayController) GetBattleReplaysByGeometryId(c fiber.Ctx) error
|
|||||||
Data: res,
|
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) {
|
func BattleReplayRoutes(router fiber.Router, battleReplayController *controllers.BattleReplayController) {
|
||||||
br := router.Group("/battle-replays")
|
br := router.Group("/battle-replays")
|
||||||
|
br.Get("/geometries", battleReplayController.GetBattleReplaysByGeometryIDs)
|
||||||
br.Get("/geometry/:geometryId", battleReplayController.GetBattleReplaysByGeometryId)
|
br.Get("/geometry/:geometryId", battleReplayController.GetBattleReplaysByGeometryId)
|
||||||
br.Get("/:id", battleReplayController.GetBattleReplayById)
|
br.Get("/:id", battleReplayController.GetBattleReplayById)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package services
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"history-api/internal/dtos/request"
|
||||||
"history-api/internal/dtos/response"
|
"history-api/internal/dtos/response"
|
||||||
"history-api/internal/models"
|
"history-api/internal/models"
|
||||||
"history-api/internal/repositories"
|
"history-api/internal/repositories"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
type BattleReplayService interface {
|
type BattleReplayService interface {
|
||||||
GetByID(ctx context.Context, id string) (*response.BattleReplayResponse, *fiber.Error)
|
GetByID(ctx context.Context, id string) (*response.BattleReplayResponse, *fiber.Error)
|
||||||
GetByGeometryID(ctx context.Context, geometryID 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 {
|
type battleReplayService struct {
|
||||||
@@ -52,3 +54,26 @@ func (s *battleReplayService) GetByGeometryID(ctx context.Context, geometryID st
|
|||||||
|
|
||||||
return models.BattleReplaysEntityToResponse(replays), nil
|
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