apskel-pos-backend/internal/repository/game_play_repository.go

171 lines
4.6 KiB
Go
Raw Normal View History

2025-09-17 19:30:17 +07:00
package repository
import (
"context"
"fmt"
2025-09-18 13:39:37 +07:00
"apskel-pos-be/internal/entities"
2025-09-17 19:30:17 +07:00
"github.com/google/uuid"
"gorm.io/gorm"
)
2025-09-18 13:39:37 +07:00
type GamePlayRepository interface {
Create(ctx context.Context, gamePlay *entities.GamePlay) error
GetByID(ctx context.Context, id uuid.UUID) (*entities.GamePlay, error)
List(ctx context.Context, offset, limit int, search string, gameID, customerID, prizeID *uuid.UUID, sortBy, sortOrder string) ([]*entities.GamePlay, int64, error)
GetByCustomerID(ctx context.Context, customerID uuid.UUID, limit int) ([]*entities.GamePlay, error)
GetByGameID(ctx context.Context, gameID uuid.UUID, limit int) ([]*entities.GamePlay, error)
CountByCustomerID(ctx context.Context, customerID uuid.UUID) (int64, error)
CountByGameID(ctx context.Context, gameID uuid.UUID) (int64, error)
}
type gamePlayRepository struct {
2025-09-17 19:30:17 +07:00
db *gorm.DB
}
2025-09-18 13:39:37 +07:00
func NewGamePlayRepository(db *gorm.DB) GamePlayRepository {
return &gamePlayRepository{
db: db,
}
2025-09-17 19:30:17 +07:00
}
2025-09-18 13:39:37 +07:00
func (r *gamePlayRepository) Create(ctx context.Context, gamePlay *entities.GamePlay) error {
if err := r.db.WithContext(ctx).Create(gamePlay).Error; err != nil {
return fmt.Errorf("failed to create game play: %w", err)
}
return nil
2025-09-17 19:30:17 +07:00
}
2025-09-18 13:39:37 +07:00
func (r *gamePlayRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.GamePlay, error) {
2025-09-17 19:30:17 +07:00
var gamePlay entities.GamePlay
2025-09-18 13:39:37 +07:00
err := r.db.WithContext(ctx).
Preload("Game").
Preload("Customer").
Preload("Prize").
First(&gamePlay, "id = ?", id).Error
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 13:39:37 +07:00
if err == gorm.ErrRecordNotFound {
return nil, fmt.Errorf("game play not found")
}
return nil, fmt.Errorf("failed to get game play: %w", err)
2025-09-17 19:30:17 +07:00
}
return &gamePlay, nil
}
2025-09-18 13:39:37 +07:00
func (r *gamePlayRepository) List(ctx context.Context, offset, limit int, search string, gameID, customerID, prizeID *uuid.UUID, sortBy, sortOrder string) ([]*entities.GamePlay, int64, error) {
var gamePlays []*entities.GamePlay
2025-09-17 19:30:17 +07:00
var total int64
2025-09-18 13:39:37 +07:00
query := r.db.WithContext(ctx).Model(&entities.GamePlay{})
2025-09-17 19:30:17 +07:00
2025-09-18 13:39:37 +07:00
// Apply filters
2025-09-17 19:30:17 +07:00
if search != "" {
2025-09-18 13:39:37 +07:00
query = query.Where("random_seed ILIKE ?", "%"+search+"%")
2025-09-17 19:30:17 +07:00
}
if gameID != nil {
query = query.Where("game_id = ?", *gameID)
}
if customerID != nil {
query = query.Where("customer_id = ?", *customerID)
}
if prizeID != nil {
query = query.Where("prize_id = ?", *prizeID)
}
2025-09-18 13:39:37 +07:00
// Count total records
if err := query.Count(&total).Error; err != nil {
return nil, 0, fmt.Errorf("failed to count game plays: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 13:39:37 +07:00
// Apply sorting
2025-09-17 19:30:17 +07:00
if sortBy != "" {
2025-09-18 13:39:37 +07:00
if sortOrder == "desc" {
query = query.Order(fmt.Sprintf("%s DESC", sortBy))
} else {
query = query.Order(fmt.Sprintf("%s ASC", sortBy))
2025-09-17 19:30:17 +07:00
}
} else {
2025-09-18 13:39:37 +07:00
query = query.Order("created_at DESC")
2025-09-17 19:30:17 +07:00
}
2025-09-18 13:39:37 +07:00
// Apply pagination and preload relations
err := query.
Preload("Game").
Preload("Customer").
Preload("Prize").
Offset(offset).
Limit(limit).
Find(&gamePlays).Error
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 13:39:37 +07:00
return nil, 0, fmt.Errorf("failed to list game plays: %w", err)
2025-09-17 19:30:17 +07:00
}
return gamePlays, total, nil
}
2025-09-18 13:39:37 +07:00
func (r *gamePlayRepository) GetByCustomerID(ctx context.Context, customerID uuid.UUID, limit int) ([]*entities.GamePlay, error) {
var gamePlays []*entities.GamePlay
query := r.db.WithContext(ctx).
Where("customer_id = ?", customerID).
Preload("Game").
Preload("Prize").
Order("created_at DESC")
if limit > 0 {
query = query.Limit(limit)
}
2025-09-17 19:30:17 +07:00
2025-09-18 13:39:37 +07:00
err := query.Find(&gamePlays).Error
if err != nil {
return nil, fmt.Errorf("failed to get game plays by customer ID: %w", err)
}
return gamePlays, nil
2025-09-17 19:30:17 +07:00
}
2025-09-18 13:39:37 +07:00
func (r *gamePlayRepository) GetByGameID(ctx context.Context, gameID uuid.UUID, limit int) ([]*entities.GamePlay, error) {
var gamePlays []*entities.GamePlay
query := r.db.WithContext(ctx).
Where("game_id = ?", gameID).
Preload("Customer").
Preload("Prize").
Order("created_at DESC")
if limit > 0 {
query = query.Limit(limit)
}
err := query.Find(&gamePlays).Error
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 13:39:37 +07:00
return nil, fmt.Errorf("failed to get game plays by game ID: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 13:39:37 +07:00
2025-09-17 19:30:17 +07:00
return gamePlays, nil
}
2025-09-18 13:39:37 +07:00
func (r *gamePlayRepository) CountByCustomerID(ctx context.Context, customerID uuid.UUID) (int64, error) {
var count int64
err := r.db.WithContext(ctx).
Model(&entities.GamePlay{}).
Where("customer_id = ?", customerID).
Count(&count).Error
if err != nil {
return 0, fmt.Errorf("failed to count game plays by customer ID: %w", err)
}
return count, nil
}
2025-09-17 19:30:17 +07:00
2025-09-18 13:39:37 +07:00
func (r *gamePlayRepository) CountByGameID(ctx context.Context, gameID uuid.UUID) (int64, error) {
var count int64
err := r.db.WithContext(ctx).
Model(&entities.GamePlay{}).
2025-09-17 19:30:17 +07:00
Where("game_id = ?", gameID).
2025-09-18 13:39:37 +07:00
Count(&count).Error
2025-09-17 19:30:17 +07:00
if err != nil {
2025-09-18 13:39:37 +07:00
return 0, fmt.Errorf("failed to count game plays by game ID: %w", err)
2025-09-17 19:30:17 +07:00
}
2025-09-18 13:39:37 +07:00
return count, nil
2025-09-17 19:30:17 +07:00
}