2023-11-01 23:21:12 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-01-06 08:44:26 +03:00
|
|
|
"github.com/google/uuid"
|
2023-11-01 23:21:12 +03:00
|
|
|
"gitrepo.ru/neonxp/nquest/pkg/models"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
DB *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGame returns new Game.
|
|
|
|
func NewGame(db *gorm.DB) *Game {
|
|
|
|
return &Game{
|
|
|
|
DB: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 08:44:26 +03:00
|
|
|
func (gs *Game) GetByID(ctx context.Context, id uuid.UUID) (*models.Game, error) {
|
2023-11-01 23:21:12 +03:00
|
|
|
g := &models.Game{}
|
|
|
|
|
|
|
|
return g, gs.DB.
|
|
|
|
WithContext(ctx).
|
2024-01-06 08:44:26 +03:00
|
|
|
Preload("Tasks", func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Order("tasks.task_order ASC")
|
|
|
|
}).
|
|
|
|
Preload("Tasks.Codes").
|
|
|
|
Preload("Tasks.Solutions").
|
2023-11-01 23:21:12 +03:00
|
|
|
First(g, id).
|
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *Game) List(ctx context.Context) ([]*models.Game, error) {
|
|
|
|
games := make([]*models.Game, 0)
|
|
|
|
|
|
|
|
return games, gs.DB.
|
|
|
|
WithContext(ctx).
|
2024-01-05 03:50:33 +03:00
|
|
|
Order("created_at DESC").
|
2024-01-06 08:44:26 +03:00
|
|
|
Preload("Tasks").
|
|
|
|
Preload("Authors").
|
2024-01-25 01:45:09 +03:00
|
|
|
Find(&games, "visible = true").
|
2023-11-01 23:21:12 +03:00
|
|
|
Error
|
|
|
|
}
|
|
|
|
|
2024-01-06 08:44:26 +03:00
|
|
|
func (gs *Game) GetTaskID(ctx context.Context, id uuid.UUID) (*models.Task, error) {
|
2023-11-01 23:21:12 +03:00
|
|
|
t := &models.Task{}
|
|
|
|
|
2024-01-06 08:44:26 +03:00
|
|
|
return t, gs.DB.WithContext(ctx).First(t, id).Error
|
2023-11-01 23:21:12 +03:00
|
|
|
}
|
|
|
|
|
2023-11-19 22:54:54 +03:00
|
|
|
func (gs *Game) ListByAuthor(ctx context.Context, author *models.User) ([]*models.Game, error) {
|
|
|
|
games := make([]*models.Game, 0)
|
2024-01-25 01:45:09 +03:00
|
|
|
model := gs.DB.
|
2023-11-19 22:54:54 +03:00
|
|
|
WithContext(ctx).
|
2024-01-25 01:45:09 +03:00
|
|
|
Model(&models.Game{})
|
|
|
|
if author.Role == models.RoleCreator {
|
|
|
|
model.Preload("Authors", gs.DB.Where("id = ?", author.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
return games,
|
|
|
|
model.
|
|
|
|
Order("created_at DESC").
|
|
|
|
Find(&games).
|
|
|
|
Error
|
2023-11-19 22:54:54 +03:00
|
|
|
}
|
2024-01-05 03:50:33 +03:00
|
|
|
|
|
|
|
func (gs *Game) CreateGame(ctx context.Context, game *models.Game) (*models.Game, error) {
|
2024-01-06 08:44:26 +03:00
|
|
|
return game, gs.DB.
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}).
|
|
|
|
Create(game).
|
|
|
|
Error
|
2024-01-05 03:50:33 +03:00
|
|
|
}
|
2024-01-24 23:15:19 +03:00
|
|
|
|
|
|
|
func (gs *Game) UpdateGame(ctx context.Context, uid uuid.UUID, game *models.Game) (*models.Game, error) {
|
|
|
|
game.ID = uid
|
|
|
|
|
2024-01-25 01:45:09 +03:00
|
|
|
db := gs.DB
|
|
|
|
|
|
|
|
return game, db.Debug().
|
|
|
|
Session(&gorm.Session{FullSaveAssociations: true}).Omit("created_at").
|
2024-01-24 23:15:19 +03:00
|
|
|
Save(game).
|
|
|
|
Error
|
|
|
|
}
|