Add Tiers and Game Prize
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CustomerPointsRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewCustomerPointsRepository(db *gorm.DB) *CustomerPointsRepository {
|
||||
return &CustomerPointsRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) Create(ctx context.Context, customerPoints *entities.CustomerPoints) error {
|
||||
return r.db.WithContext(ctx).Create(customerPoints).Error
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.CustomerPoints, error) {
|
||||
var customerPoints entities.CustomerPoints
|
||||
err := r.db.WithContext(ctx).Preload("Customer").Where("id = ?", id).First(&customerPoints).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &customerPoints, nil
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) GetByCustomerID(ctx context.Context, customerID uuid.UUID) (*entities.CustomerPoints, error) {
|
||||
var customerPoints entities.CustomerPoints
|
||||
err := r.db.WithContext(ctx).Preload("Customer").Where("customer_id = ?", customerID).First(&customerPoints).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &customerPoints, nil
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) List(ctx context.Context, offset, limit int, search string, sortBy, sortOrder string) ([]entities.CustomerPoints, int64, error) {
|
||||
var customerPoints []entities.CustomerPoints
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Preload("Customer")
|
||||
|
||||
if search != "" {
|
||||
searchTerm := "%" + search + "%"
|
||||
query = query.Joins("JOIN customers ON customer_points.customer_id = customers.id").
|
||||
Where("customers.name ILIKE ? OR customers.email ILIKE ?", searchTerm, searchTerm)
|
||||
}
|
||||
|
||||
if err := query.Model(&entities.CustomerPoints{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sortBy != "" {
|
||||
if sortOrder == "" {
|
||||
sortOrder = "asc"
|
||||
}
|
||||
query = query.Order(fmt.Sprintf("customer_points.%s %s", sortBy, sortOrder))
|
||||
} else {
|
||||
query = query.Order("customer_points.created_at DESC")
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(limit).Find(&customerPoints).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return customerPoints, total, nil
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) Update(ctx context.Context, customerPoints *entities.CustomerPoints) error {
|
||||
return r.db.WithContext(ctx).Save(customerPoints).Error
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.CustomerPoints{}, id).Error
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) AddPoints(ctx context.Context, customerID uuid.UUID, points int64) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.CustomerPoints{}).
|
||||
Where("customer_id = ?", customerID).
|
||||
Update("balance", gorm.Expr("balance + ?", points)).Error
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) DeductPoints(ctx context.Context, customerID uuid.UUID, points int64) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.CustomerPoints{}).
|
||||
Where("customer_id = ? AND balance >= ?", customerID, points).
|
||||
Update("balance", gorm.Expr("balance - ?", points)).Error
|
||||
}
|
||||
|
||||
func (r *CustomerPointsRepository) EnsureCustomerPoints(ctx context.Context, customerID uuid.UUID) (*entities.CustomerPoints, error) {
|
||||
customerPoints, err := r.GetByCustomerID(ctx, customerID)
|
||||
if err == nil {
|
||||
return customerPoints, nil
|
||||
}
|
||||
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create new customer points record
|
||||
newCustomerPoints := &entities.CustomerPoints{
|
||||
CustomerID: customerID,
|
||||
Balance: 0,
|
||||
}
|
||||
|
||||
err = r.Create(ctx, newCustomerPoints)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newCustomerPoints, nil
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CustomerTokensRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewCustomerTokensRepository(db *gorm.DB) *CustomerTokensRepository {
|
||||
return &CustomerTokensRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) Create(ctx context.Context, customerTokens *entities.CustomerTokens) error {
|
||||
return r.db.WithContext(ctx).Create(customerTokens).Error
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.CustomerTokens, error) {
|
||||
var customerTokens entities.CustomerTokens
|
||||
err := r.db.WithContext(ctx).Preload("Customer").Where("id = ?", id).First(&customerTokens).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &customerTokens, nil
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) GetByCustomerIDAndType(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType) (*entities.CustomerTokens, error) {
|
||||
var customerTokens entities.CustomerTokens
|
||||
err := r.db.WithContext(ctx).Preload("Customer").Where("customer_id = ? AND token_type = ?", customerID, tokenType).First(&customerTokens).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &customerTokens, nil
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) GetByCustomerID(ctx context.Context, customerID uuid.UUID) ([]entities.CustomerTokens, error) {
|
||||
var customerTokens []entities.CustomerTokens
|
||||
err := r.db.WithContext(ctx).Preload("Customer").Where("customer_id = ?", customerID).Find(&customerTokens).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return customerTokens, nil
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) List(ctx context.Context, offset, limit int, search, tokenType string, sortBy, sortOrder string) ([]entities.CustomerTokens, int64, error) {
|
||||
var customerTokens []entities.CustomerTokens
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Preload("Customer")
|
||||
|
||||
if search != "" {
|
||||
searchTerm := "%" + search + "%"
|
||||
query = query.Joins("JOIN customers ON customer_tokens.customer_id = customers.id").
|
||||
Where("customers.name ILIKE ? OR customers.email ILIKE ?", searchTerm, searchTerm)
|
||||
}
|
||||
|
||||
if tokenType != "" {
|
||||
query = query.Where("token_type = ?", tokenType)
|
||||
}
|
||||
|
||||
if err := query.Model(&entities.CustomerTokens{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sortBy != "" {
|
||||
if sortOrder == "" {
|
||||
sortOrder = "asc"
|
||||
}
|
||||
query = query.Order(fmt.Sprintf("customer_tokens.%s %s", sortBy, sortOrder))
|
||||
} else {
|
||||
query = query.Order("customer_tokens.created_at DESC")
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(limit).Find(&customerTokens).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return customerTokens, total, nil
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) Update(ctx context.Context, customerTokens *entities.CustomerTokens) error {
|
||||
return r.db.WithContext(ctx).Save(customerTokens).Error
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.CustomerTokens{}, id).Error
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) AddTokens(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType, tokens int64) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.CustomerTokens{}).
|
||||
Where("customer_id = ? AND token_type = ?", customerID, tokenType).
|
||||
Update("balance", gorm.Expr("balance + ?", tokens)).Error
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) DeductTokens(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType, tokens int64) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.CustomerTokens{}).
|
||||
Where("customer_id = ? AND token_type = ? AND balance >= ?", customerID, tokenType, tokens).
|
||||
Update("balance", gorm.Expr("balance - ?", tokens)).Error
|
||||
}
|
||||
|
||||
func (r *CustomerTokensRepository) EnsureCustomerTokens(ctx context.Context, customerID uuid.UUID, tokenType entities.TokenType) (*entities.CustomerTokens, error) {
|
||||
customerTokens, err := r.GetByCustomerIDAndType(ctx, customerID, tokenType)
|
||||
if err == nil {
|
||||
return customerTokens, nil
|
||||
}
|
||||
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create new customer tokens record
|
||||
newCustomerTokens := &entities.CustomerTokens{
|
||||
CustomerID: customerID,
|
||||
TokenType: tokenType,
|
||||
Balance: 0,
|
||||
}
|
||||
|
||||
err = r.Create(ctx, newCustomerTokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newCustomerTokens, nil
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GamePlayRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewGamePlayRepository(db *gorm.DB) *GamePlayRepository {
|
||||
return &GamePlayRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *GamePlayRepository) Create(ctx context.Context, gamePlay *entities.GamePlay) error {
|
||||
return r.db.WithContext(ctx).Create(gamePlay).Error
|
||||
}
|
||||
|
||||
func (r *GamePlayRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.GamePlay, error) {
|
||||
var gamePlay entities.GamePlay
|
||||
err := r.db.WithContext(ctx).Preload("Game").Preload("Customer").Preload("Prize").Where("id = ?", id).First(&gamePlay).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &gamePlay, nil
|
||||
}
|
||||
|
||||
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
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Preload("Game").Preload("Customer").Preload("Prize")
|
||||
|
||||
if search != "" {
|
||||
searchTerm := "%" + search + "%"
|
||||
query = query.Joins("JOIN customers ON game_plays.customer_id = customers.id").
|
||||
Where("customers.name ILIKE ? OR customers.email ILIKE ?", searchTerm, searchTerm)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
if err := query.Model(&entities.GamePlay{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sortBy != "" {
|
||||
if sortOrder == "" {
|
||||
sortOrder = "asc"
|
||||
}
|
||||
query = query.Order(fmt.Sprintf("game_plays.%s %s", sortBy, sortOrder))
|
||||
} else {
|
||||
query = query.Order("game_plays.created_at DESC")
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(limit).Find(&gamePlays).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return gamePlays, total, nil
|
||||
}
|
||||
|
||||
func (r *GamePlayRepository) Update(ctx context.Context, gamePlay *entities.GamePlay) error {
|
||||
return r.db.WithContext(ctx).Save(gamePlay).Error
|
||||
}
|
||||
|
||||
func (r *GamePlayRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.GamePlay{}, id).Error
|
||||
}
|
||||
|
||||
func (r *GamePlayRepository) GetCustomerPlays(ctx context.Context, customerID uuid.UUID, limit int) ([]entities.GamePlay, error) {
|
||||
var gamePlays []entities.GamePlay
|
||||
err := r.db.WithContext(ctx).Preload("Game").Preload("Prize").
|
||||
Where("customer_id = ?", customerID).
|
||||
Order("created_at DESC").
|
||||
Limit(limit).
|
||||
Find(&gamePlays).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gamePlays, nil
|
||||
}
|
||||
|
||||
func (r *GamePlayRepository) GetGameStats(ctx context.Context, gameID uuid.UUID) (map[string]interface{}, error) {
|
||||
var stats map[string]interface{}
|
||||
|
||||
err := r.db.WithContext(ctx).Model(&entities.GamePlay{}).
|
||||
Select("COUNT(*) as total_plays, SUM(token_used) as total_tokens_used").
|
||||
Where("game_id = ?", gameID).
|
||||
Scan(&stats).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type GameRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewGameRepository(db *gorm.DB) *GameRepository {
|
||||
return &GameRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *GameRepository) Create(ctx context.Context, game *entities.Game) error {
|
||||
return r.db.WithContext(ctx).Create(game).Error
|
||||
}
|
||||
|
||||
func (r *GameRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.Game, error) {
|
||||
var game entities.Game
|
||||
err := r.db.WithContext(ctx).Preload("Prizes").Where("id = ?", id).First(&game).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &game, nil
|
||||
}
|
||||
|
||||
func (r *GameRepository) List(ctx context.Context, offset, limit int, search, gameType string, isActive *bool, sortBy, sortOrder string) ([]entities.Game, int64, error) {
|
||||
var games []entities.Game
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Preload("Prizes")
|
||||
|
||||
if search != "" {
|
||||
searchTerm := "%" + search + "%"
|
||||
query = query.Where("name ILIKE ?", searchTerm)
|
||||
}
|
||||
|
||||
if gameType != "" {
|
||||
query = query.Where("type = ?", gameType)
|
||||
}
|
||||
|
||||
if isActive != nil {
|
||||
query = query.Where("is_active = ?", *isActive)
|
||||
}
|
||||
|
||||
if err := query.Model(&entities.Game{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sortBy != "" {
|
||||
if sortOrder == "" {
|
||||
sortOrder = "asc"
|
||||
}
|
||||
query = query.Order(fmt.Sprintf("%s %s", sortBy, sortOrder))
|
||||
} else {
|
||||
query = query.Order("created_at DESC")
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(limit).Find(&games).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return games, total, nil
|
||||
}
|
||||
|
||||
func (r *GameRepository) Update(ctx context.Context, game *entities.Game) error {
|
||||
return r.db.WithContext(ctx).Save(game).Error
|
||||
}
|
||||
|
||||
func (r *GameRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.Game{}, id).Error
|
||||
}
|
||||
|
||||
func (r *GameRepository) GetActiveGames(ctx context.Context) ([]entities.Game, error) {
|
||||
var games []entities.Game
|
||||
err := r.db.WithContext(ctx).Preload("Prizes").Where("is_active = ?", true).Find(&games).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return games, nil
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type OmsetTrackerRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewOmsetTrackerRepository(db *gorm.DB) *OmsetTrackerRepository {
|
||||
return &OmsetTrackerRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) Create(ctx context.Context, omsetTracker *entities.OmsetTracker) error {
|
||||
return r.db.WithContext(ctx).Create(omsetTracker).Error
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.OmsetTracker, error) {
|
||||
var omsetTracker entities.OmsetTracker
|
||||
err := r.db.WithContext(ctx).Preload("Game").Where("id = ?", id).First(&omsetTracker).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &omsetTracker, nil
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) List(ctx context.Context, offset, limit int, search, periodType string, gameID *uuid.UUID, from, to *time.Time, sortBy, sortOrder string) ([]entities.OmsetTracker, int64, error) {
|
||||
var omsetTrackers []entities.OmsetTracker
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Preload("Game")
|
||||
|
||||
if search != "" {
|
||||
searchTerm := "%" + search + "%"
|
||||
query = query.Joins("LEFT JOIN games ON omset_tracker.game_id = games.id").
|
||||
Where("games.name ILIKE ?", searchTerm)
|
||||
}
|
||||
|
||||
if periodType != "" {
|
||||
query = query.Where("period_type = ?", periodType)
|
||||
}
|
||||
|
||||
if gameID != nil {
|
||||
query = query.Where("game_id = ?", *gameID)
|
||||
}
|
||||
|
||||
if from != nil {
|
||||
query = query.Where("period_start >= ?", *from)
|
||||
}
|
||||
|
||||
if to != nil {
|
||||
query = query.Where("period_end <= ?", *to)
|
||||
}
|
||||
|
||||
if err := query.Model(&entities.OmsetTracker{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sortBy != "" {
|
||||
if sortOrder == "" {
|
||||
sortOrder = "asc"
|
||||
}
|
||||
query = query.Order(fmt.Sprintf("omset_tracker.%s %s", sortBy, sortOrder))
|
||||
} else {
|
||||
query = query.Order("omset_tracker.period_start DESC")
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(limit).Find(&omsetTrackers).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return omsetTrackers, total, nil
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) Update(ctx context.Context, omsetTracker *entities.OmsetTracker) error {
|
||||
return r.db.WithContext(ctx).Save(omsetTracker).Error
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.OmsetTracker{}, id).Error
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) AddOmset(ctx context.Context, periodType entities.PeriodType, periodStart, periodEnd time.Time, amount int64, gameID *uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Model(&entities.OmsetTracker{}).
|
||||
Where("period_type = ? AND period_start = ? AND period_end = ? AND game_id = ?", periodType, periodStart, periodEnd, gameID).
|
||||
Update("total", gorm.Expr("total + ?", amount)).Error
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) GetOrCreatePeriod(ctx context.Context, periodType entities.PeriodType, periodStart, periodEnd time.Time, gameID *uuid.UUID) (*entities.OmsetTracker, error) {
|
||||
var omsetTracker entities.OmsetTracker
|
||||
|
||||
err := r.db.WithContext(ctx).Preload("Game").
|
||||
Where("period_type = ? AND period_start = ? AND period_end = ? AND game_id = ?", periodType, periodStart, periodEnd, gameID).
|
||||
First(&omsetTracker).Error
|
||||
|
||||
if err == nil {
|
||||
return &omsetTracker, nil
|
||||
}
|
||||
|
||||
if err != gorm.ErrRecordNotFound {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create new period
|
||||
newOmsetTracker := &entities.OmsetTracker{
|
||||
PeriodType: periodType,
|
||||
PeriodStart: periodStart,
|
||||
PeriodEnd: periodEnd,
|
||||
Total: 0,
|
||||
GameID: gameID,
|
||||
}
|
||||
|
||||
err = r.Create(ctx, newOmsetTracker)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return newOmsetTracker, nil
|
||||
}
|
||||
|
||||
func (r *OmsetTrackerRepository) GetPeriodSummary(ctx context.Context, periodType entities.PeriodType, from, to time.Time) (map[string]interface{}, error) {
|
||||
var summary map[string]interface{}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&entities.OmsetTracker{}).
|
||||
Select("SUM(total) as total_omset, COUNT(*) as period_count").
|
||||
Where("period_type = ?", periodType)
|
||||
|
||||
if !from.IsZero() {
|
||||
query = query.Where("period_start >= ?", from)
|
||||
}
|
||||
|
||||
if !to.IsZero() {
|
||||
query = query.Where("period_end <= ?", to)
|
||||
}
|
||||
|
||||
err := query.Scan(&summary).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return summary, nil
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"apskel-pos-be/internal/entities"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type TierRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewTierRepository(db *gorm.DB) *TierRepository {
|
||||
return &TierRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *TierRepository) Create(ctx context.Context, tier *entities.Tier) error {
|
||||
return r.db.WithContext(ctx).Create(tier).Error
|
||||
}
|
||||
|
||||
func (r *TierRepository) GetByID(ctx context.Context, id uuid.UUID) (*entities.Tier, error) {
|
||||
var tier entities.Tier
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&tier).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tier, nil
|
||||
}
|
||||
|
||||
func (r *TierRepository) GetByName(ctx context.Context, name string) (*entities.Tier, error) {
|
||||
var tier entities.Tier
|
||||
err := r.db.WithContext(ctx).Where("name = ?", name).First(&tier).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tier, nil
|
||||
}
|
||||
|
||||
func (r *TierRepository) List(ctx context.Context, offset, limit int, search string, sortBy, sortOrder string) ([]entities.Tier, int64, error) {
|
||||
var tiers []entities.Tier
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx)
|
||||
|
||||
if search != "" {
|
||||
searchTerm := "%" + search + "%"
|
||||
query = query.Where("name ILIKE ?", searchTerm)
|
||||
}
|
||||
|
||||
if err := query.Model(&entities.Tier{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if sortBy != "" {
|
||||
if sortOrder == "" {
|
||||
sortOrder = "asc"
|
||||
}
|
||||
query = query.Order(fmt.Sprintf("%s %s", sortBy, sortOrder))
|
||||
} else {
|
||||
query = query.Order("min_points ASC")
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(limit).Find(&tiers).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return tiers, total, nil
|
||||
}
|
||||
|
||||
func (r *TierRepository) Update(ctx context.Context, tier *entities.Tier) error {
|
||||
return r.db.WithContext(ctx).Save(tier).Error
|
||||
}
|
||||
|
||||
func (r *TierRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.Tier{}, id).Error
|
||||
}
|
||||
|
||||
func (r *TierRepository) GetTierByPoints(ctx context.Context, points int64) (*entities.Tier, error) {
|
||||
var tier entities.Tier
|
||||
err := r.db.WithContext(ctx).Where("min_points <= ?", points).Order("min_points DESC").First(&tier).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tier, nil
|
||||
}
|
||||
Reference in New Issue
Block a user