add games operations

This commit is contained in:
2026-07-26 22:38:33 +07:00
parent cf32a6114f
commit 6fc19833d3
24 changed files with 2430 additions and 96 deletions
+16
View File
@@ -0,0 +1,16 @@
package game_service
import (
"evening_detective_server/internal/services/scenarios_service"
"time"
)
type Game struct {
ID int
Name string
Description string
StartAt time.Time
Status string
Scenario *scenarios_service.Scenario
Teams []*Team
}
@@ -0,0 +1,32 @@
package game_service
import (
"evening_detective_server/internal/repos"
"evening_detective_server/internal/services/scenarios_service"
)
func mapGame(
o *repos.Game,
domain string,
) *Game {
return &Game{
ID: o.ID,
Name: o.Name,
Description: o.Description,
StartAt: o.StartAt,
Status: o.Status,
Scenario: scenarios_service.MapScenarioLight(o.Scenario, domain),
Teams: mapTeams(o.Teams),
}
}
func mapGames(
o []*repos.Game,
domain string,
) []*Game {
res := make([]*Game, 0, len(o))
for _, item := range o {
res = append(res, mapGame(item, domain))
}
return res
}
+93
View File
@@ -0,0 +1,93 @@
package game_service
import (
"context"
"evening_detective_server/internal/repos/games_repo"
"evening_detective_server/internal/repos/scenarios_repo"
"evening_detective_server/internal/repos/teams_repo"
"time"
)
type GameService struct {
gamesRepo *games_repo.GamesRepo
teamsRepo *teams_repo.TeamsRepo
scenariosRepo *scenarios_repo.ScenariosRepo
domain string
}
func NewGameService(
gamesRepo *games_repo.GamesRepo,
teamsRepo *teams_repo.TeamsRepo,
scenariosRepo *scenarios_repo.ScenariosRepo,
domain string,
) *GameService {
return &GameService{
gamesRepo: gamesRepo,
teamsRepo: teamsRepo,
scenariosRepo: scenariosRepo,
domain: domain,
}
}
func (s *GameService) AddGame(
ctx context.Context,
name string,
description string,
startAt time.Time,
scenarioId int32,
) (int, error) {
id, err := s.gamesRepo.AddGame(ctx, name, description, startAt, scenarioId)
if err != nil {
return 0, err
}
return id, nil
}
func (s *GameService) GetGame(
ctx context.Context,
id int,
) (*Game, error) {
game, err := s.gamesRepo.GetGameByID(ctx, id)
if err != nil {
return nil, err
}
scenario, err := s.scenariosRepo.GetScenarioByID(ctx, game.ScenarioId)
if err != nil {
return nil, err
}
game.Scenario = scenario
teams, err := s.teamsRepo.GetTeamsByGameID(ctx, id)
if err != nil {
return nil, err
}
game.Teams = teams
return mapGame(game, s.domain), nil
}
func (s *GameService) DeleteGame(
ctx context.Context,
id int,
) error {
return s.gamesRepo.DeleteGameByID(ctx, id)
}
func (s *GameService) GetGames(
ctx context.Context,
) ([]*Game, error) {
games, err := s.gamesRepo.GetGames(ctx)
if err != nil {
return nil, err
}
return mapGames(games, s.domain), nil
}
func (s *GameService) UpdateGame(
ctx context.Context,
id int,
name string,
description string,
startAt time.Time,
scenarioId int32,
) error {
return s.gamesRepo.UpdateGame(ctx, id, name, description, startAt, scenarioId)
}
+7
View File
@@ -0,0 +1,7 @@
package game_service
type Team struct {
ID int
Name string
Password string
}
@@ -0,0 +1,23 @@
package game_service
import "evening_detective_server/internal/repos"
func mapTeam(
o *repos.Team,
) *Team {
return &Team{
ID: o.ID,
Name: o.Name,
Password: o.Password,
}
}
func mapTeams(
o []*repos.Team,
) []*Team {
res := make([]*Team, 0, len(o))
for _, item := range o {
res = append(res, mapTeam(item))
}
return res
}
@@ -12,7 +12,7 @@ func mapScenariosLight(
) []*Scenario {
res := make([]*Scenario, 0, len(o))
for _, item := range o {
res = append(res, mapScenarioLight(item, domain))
res = append(res, MapScenarioLight(item, domain))
}
return res
}
@@ -32,10 +32,13 @@ func mapScenarios(
return res, nil
}
func mapScenarioLight(
func MapScenarioLight(
o *repos.Scenario,
domain string,
) *Scenario {
if o == nil {
return nil
}
return &Scenario{
ID: o.ID,
Name: o.Name,
@@ -55,7 +58,7 @@ func mapScenario(
o *repos.Scenario,
domain string,
) (*Scenario, error) {
scenario := mapScenarioLight(o, domain)
scenario := MapScenarioLight(o, domain)
story, err := mapStory(o.Scenario, domain)
if err != nil {
return nil, err
@@ -75,7 +75,7 @@ func (s *ScenarioService) GetScenarioByID(
if err != nil {
return nil, err
}
return mapScenarioLight(scenario, s.domain), nil
return MapScenarioLight(scenario, s.domain), nil
}
func (s *ScenarioService) UpdateScenarioByID(