Add Tiers and Game Prize
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/mappers"
|
||||
"apskel-pos-be/internal/models"
|
||||
"apskel-pos-be/internal/repository"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type GameProcessor struct {
|
||||
gameRepo *repository.GameRepository
|
||||
}
|
||||
|
||||
func NewGameProcessor(gameRepo *repository.GameRepository) *GameProcessor {
|
||||
return &GameProcessor{
|
||||
gameRepo: gameRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateGame creates a new game
|
||||
func (p *GameProcessor) CreateGame(ctx context.Context, req *models.CreateGameRequest) (*models.GameResponse, error) {
|
||||
// Convert request to entity
|
||||
game := mappers.ToGameEntity(req)
|
||||
|
||||
// Create game
|
||||
err := p.gameRepo.Create(ctx, game)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create game: %w", err)
|
||||
}
|
||||
|
||||
return mappers.ToGameResponse(game), nil
|
||||
}
|
||||
|
||||
// GetGame retrieves a game by ID
|
||||
func (p *GameProcessor) GetGame(ctx context.Context, id uuid.UUID) (*models.GameResponse, error) {
|
||||
game, err := p.gameRepo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("game not found: %w", err)
|
||||
}
|
||||
|
||||
return mappers.ToGameResponse(game), nil
|
||||
}
|
||||
|
||||
// ListGames retrieves games with pagination and filtering
|
||||
func (p *GameProcessor) ListGames(ctx context.Context, query *models.ListGamesQuery) (*models.PaginatedResponse[models.GameResponse], error) {
|
||||
// Set default values
|
||||
if query.Page <= 0 {
|
||||
query.Page = 1
|
||||
}
|
||||
if query.Limit <= 0 {
|
||||
query.Limit = 10
|
||||
}
|
||||
if query.Limit > 100 {
|
||||
query.Limit = 100
|
||||
}
|
||||
|
||||
offset := (query.Page - 1) * query.Limit
|
||||
|
||||
// Get games from repository
|
||||
games, total, err := p.gameRepo.List(
|
||||
ctx,
|
||||
offset,
|
||||
query.Limit,
|
||||
query.Search,
|
||||
query.Type,
|
||||
query.IsActive,
|
||||
query.SortBy,
|
||||
query.SortOrder,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list games: %w", err)
|
||||
}
|
||||
|
||||
// Convert to responses
|
||||
responses := mappers.ToGameResponses(games)
|
||||
|
||||
// Calculate pagination info
|
||||
totalPages := int((total + int64(query.Limit) - 1) / int64(query.Limit))
|
||||
|
||||
return &models.PaginatedResponse[models.GameResponse]{
|
||||
Data: responses,
|
||||
Pagination: models.Pagination{
|
||||
Page: query.Page,
|
||||
Limit: query.Limit,
|
||||
Total: total,
|
||||
TotalPages: totalPages,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdateGame updates an existing game
|
||||
func (p *GameProcessor) UpdateGame(ctx context.Context, id uuid.UUID, req *models.UpdateGameRequest) (*models.GameResponse, error) {
|
||||
// Get existing game
|
||||
game, err := p.gameRepo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("game not found: %w", err)
|
||||
}
|
||||
|
||||
// Update game fields
|
||||
mappers.UpdateGameEntity(game, req)
|
||||
|
||||
// Save updated game
|
||||
err = p.gameRepo.Update(ctx, game)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to update game: %w", err)
|
||||
}
|
||||
|
||||
return mappers.ToGameResponse(game), nil
|
||||
}
|
||||
|
||||
// DeleteGame deletes a game
|
||||
func (p *GameProcessor) DeleteGame(ctx context.Context, id uuid.UUID) error {
|
||||
// Get existing game
|
||||
_, err := p.gameRepo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("game not found: %w", err)
|
||||
}
|
||||
|
||||
// Delete game
|
||||
err = p.gameRepo.Delete(ctx, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete game: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetActiveGames gets all active games
|
||||
func (p *GameProcessor) GetActiveGames(ctx context.Context) ([]models.GameResponse, error) {
|
||||
games, err := p.gameRepo.GetActiveGames(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get active games: %w", err)
|
||||
}
|
||||
|
||||
return mappers.ToGameResponses(games), nil
|
||||
}
|
||||
Reference in New Issue
Block a user