103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"apskel-pos-be/internal/entities"
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type GamePrizeRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewGamePrizeRepository(db *gorm.DB) *GamePrizeRepository {
|
|
return &GamePrizeRepository{db: db}
|
|
}
|
|
|
|
func (r *GamePrizeRepository) Create(ctx context.Context, gamePrize *entities.GamePrize) error {
|
|
return r.db.WithContext(ctx).Create(gamePrize).Error
|
|
}
|
|
|
|
func (r *GamePrizeRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.GamePrize, error) {
|
|
var gamePrize entities.GamePrize
|
|
err := r.db.WithContext(ctx).Preload("Game").Preload("FallbackPrize").Where("id = ?", id).First(&gamePrize).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &gamePrize, nil
|
|
}
|
|
|
|
func (r *GamePrizeRepository) GetByGameID(ctx context.Context, gameID uuid.UUID) ([]entities.GamePrize, error) {
|
|
var gamePrizes []entities.GamePrize
|
|
err := r.db.WithContext(ctx).Preload("Game").Preload("FallbackPrize").Where("game_id = ?", gameID).Find(&gamePrizes).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return gamePrizes, nil
|
|
}
|
|
|
|
func (r *GamePrizeRepository) List(ctx context.Context, offset, limit int, search string, gameID *uuid.UUID, sortBy, sortOrder string) ([]entities.GamePrize, int64, error) {
|
|
var gamePrizes []entities.GamePrize
|
|
var total int64
|
|
|
|
query := r.db.WithContext(ctx).Preload("Game").Preload("FallbackPrize")
|
|
|
|
if search != "" {
|
|
searchTerm := "%" + search + "%"
|
|
query = query.Where("name ILIKE ?", searchTerm)
|
|
}
|
|
|
|
if gameID != nil {
|
|
query = query.Where("game_id = ?", *gameID)
|
|
}
|
|
|
|
if err := query.Model(&entities.GamePrize{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
if sortBy != "" {
|
|
if sortOrder == "" {
|
|
sortOrder = "asc"
|
|
}
|
|
query = query.Order(fmt.Sprintf("game_prizes.%s %s", sortBy, sortOrder))
|
|
} else {
|
|
query = query.Order("game_prizes.weight DESC")
|
|
}
|
|
|
|
err := query.Offset(offset).Limit(limit).Find(&gamePrizes).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return gamePrizes, total, nil
|
|
}
|
|
|
|
func (r *GamePrizeRepository) Update(ctx context.Context, gamePrize *entities.GamePrize) error {
|
|
return r.db.WithContext(ctx).Save(gamePrize).Error
|
|
}
|
|
|
|
func (r *GamePrizeRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|
return r.db.WithContext(ctx).Delete(&entities.GamePrize{}, id).Error
|
|
}
|
|
|
|
func (r *GamePrizeRepository) DecreaseStock(ctx context.Context, id uuid.UUID, amount int) error {
|
|
return r.db.WithContext(ctx).Model(&entities.GamePrize{}).
|
|
Where("id = ? AND stock >= ?", id, amount).
|
|
Update("stock", gorm.Expr("stock - ?", amount)).Error
|
|
}
|
|
|
|
func (r *GamePrizeRepository) GetAvailablePrizes(ctx context.Context, gameID uuid.UUID) ([]entities.GamePrize, error) {
|
|
var gamePrizes []entities.GamePrize
|
|
err := r.db.WithContext(ctx).Preload("Game").Preload("FallbackPrize").
|
|
Where("game_id = ? AND stock > 0", gameID).
|
|
Order("weight DESC").
|
|
Find(&gamePrizes).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return gamePrizes, nil
|
|
}
|