package service import ( "context" "github.com/google/uuid" "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, } } func (gs *Game) GetByID(ctx context.Context, id uuid.UUID) (*models.Game, error) { g := &models.Game{} return g, gs.DB. WithContext(ctx). Preload("Tasks", func(db *gorm.DB) *gorm.DB { return db.Order("tasks.task_order ASC") }). Preload("Tasks.Codes"). 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). Order("created_at DESC"). Preload("Tasks"). Preload("Authors"). Find(&games, "visible = true"). Error } func (gs *Game) GetTaskID(ctx context.Context, id uuid.UUID) (*models.Task, error) { t := &models.Task{} return t, gs.DB.WithContext(ctx).First(t, id).Error } func (gs *Game) ListByAuthor(ctx context.Context, author *models.User) ([]*models.Game, error) { games := make([]*models.Game, 0) model := gs.DB. WithContext(ctx). 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 } func (gs *Game) UpsertGame(ctx context.Context, game *models.Game) (*models.Game, error) { return game, gs.DB.Debug(). Session(&gorm.Session{FullSaveAssociations: true}). Save(game). Error }