add teams operations

This commit is contained in:
2026-07-28 00:51:25 +07:00
parent 5a5b36e44d
commit f6934e9d5b
11 changed files with 199 additions and 35 deletions
+49 -7
View File
@@ -544,8 +544,20 @@ func (s *server) AddTeam(ctx context.Context, req *proto.AddTeamReq) (*proto.Add
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
panic("unimplemented")
err := s.gameService.AddTeam(
ctx,
claims.UserID,
int(req.GameId),
req.Name,
)
if err != nil {
return &proto.AddTeamRsp{
Error: err.Error(),
}, nil
}
return &proto.AddTeamRsp{}, nil
}
func (s *server) UpdateTeam(ctx context.Context, req *proto.UpdateTeamReq) (*proto.UpdateTeamRsp, error) {
@@ -553,8 +565,19 @@ func (s *server) UpdateTeam(ctx context.Context, req *proto.UpdateTeamReq) (*pro
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
panic("unimplemented")
err := s.gameService.UpdateTeam(
ctx,
int(req.Id),
req.Name,
)
if err != nil {
return &proto.UpdateTeamRsp{
Error: err.Error(),
}, nil
}
return &proto.UpdateTeamRsp{}, nil
}
func (s *server) DeleteTeam(ctx context.Context, req *proto.DeleteTeamReq) (*proto.DeleteTeamRsp, error) {
@@ -562,8 +585,18 @@ func (s *server) DeleteTeam(ctx context.Context, req *proto.DeleteTeamReq) (*pro
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
panic("unimplemented")
err := s.gameService.DeleteTeam(
ctx,
int(req.Id),
)
if err != nil {
return &proto.DeleteTeamRsp{
Error: err.Error(),
}, nil
}
return &proto.DeleteTeamRsp{}, nil
}
func (s *server) GiveTeamApplications(ctx context.Context, req *proto.GiveTeamApplicationsReq) (*proto.GiveTeamApplicationsRsp, error) {
@@ -571,7 +604,7 @@ func (s *server) GiveTeamApplications(ctx context.Context, req *proto.GiveTeamAp
if !roles.HasRole(claims, roles.Organizer) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
panic("unimplemented")
}
@@ -582,3 +615,12 @@ func (s *server) GetTeamActions(ctx context.Context, req *proto.GetTeamActionsRe
func (s *server) AddTeamAction(ctx context.Context, req *proto.AddTeamActionReq) (*proto.AddTeamActionRsp, error) {
panic("unimplemented")
}
func (s *server) DeleteLastTeamAction(ctx context.Context, req *proto.DeleteLastTeamActionReq) (*proto.DeleteLastTeamActionRsp, error) {
claims := ctx.Value("claims").(*processor_jwt.JWTClaims)
if !roles.HasRole(claims, roles.Author) {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
panic("unimplemented")
}
+4 -2
View File
@@ -9,8 +9,10 @@ func mapTeam(
o *game_service.Team,
) *proto.Team {
return &proto.Team{
Id: int32(o.ID),
Name: o.Name,
Id: int32(o.ID),
Name: o.Name,
Status: o.Status,
Password: o.Password,
}
}
+1
View File
@@ -3,5 +3,6 @@ package repos
type Team struct {
ID int
Name string
Status string
Password string
}
+74 -14
View File
@@ -2,11 +2,16 @@ package teams_repo
import (
"context"
"errors"
"evening_detective_server/internal/repos"
"github.com/jackc/pgx/v5/pgxpool"
)
var (
ErrTeamNotFound = errors.New("Команда не найдена")
)
type TeamsRepo struct {
pool *pgxpool.Pool
}
@@ -24,20 +29,12 @@ func (r *TeamsRepo) GetTeamsByGameID(
rows, err := r.pool.Query(
ctx,
`SELECT
scenarios.id,
scenarios.name,
scenarios.description,
scenarios.image,
scenarios.scenario,
users.id,
users.username,
scenarios.status,
scenarios.updated_at,
scenarios.created_at,
scenarios.published_at
FROM scenarios
LEFT JOIN users on scenarios.author_id = users.id
WHERE scenarios.author_id = $1 and is_deleted = FALSE
id,
name,
status,
password
FROM teams
WHERE game_id = $1
ORDER BY created_at DESC`,
gameId,
)
@@ -52,6 +49,7 @@ func (r *TeamsRepo) GetTeamsByGameID(
err := rows.Scan(
&team.ID,
&team.Name,
&team.Status,
&team.Password,
)
if err != nil {
@@ -65,3 +63,65 @@ func (r *TeamsRepo) GetTeamsByGameID(
return teams, nil
}
func (r *TeamsRepo) AddTeam(
ctx context.Context,
creatorId int,
gameId int,
name string,
scenarioId int,
password string,
) (int, error) {
id := 0
err := r.pool.QueryRow(
ctx,
`INSERT INTO teams (name, creator_id, game_id, scenario_id, password)
VALUES ($1, $2, $3, $4, $5)
RETURNING id`,
name,
creatorId,
gameId,
scenarioId,
password,
).Scan(&id)
if err != nil {
return 0, err
}
return id, nil
}
func (r *TeamsRepo) UpdateTeam(ctx context.Context, teamId int, name string) error {
tag, err := r.pool.Exec(
ctx,
`UPDATE teams
SET
name = $1
WHERE id = $2`,
name,
teamId,
)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrTeamNotFound
}
return nil
}
func (r *TeamsRepo) DeleteTeam(ctx context.Context, teamId int) error {
tag, err := r.pool.Exec(
ctx,
`DELETE FROM teams
WHERE id = $1`,
teamId,
)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrTeamNotFound
}
return nil
}
+38 -8
View File
@@ -2,6 +2,7 @@ package game_service
import (
"context"
"evening_detective_server/internal/modules/password_generator"
"evening_detective_server/internal/repos/games_repo"
"evening_detective_server/internal/repos/scenarios_repo"
"evening_detective_server/internal/repos/teams_repo"
@@ -9,10 +10,11 @@ import (
)
type GameService struct {
gamesRepo *games_repo.GamesRepo
teamsRepo *teams_repo.TeamsRepo
scenariosRepo *scenarios_repo.ScenariosRepo
domain string
gamesRepo *games_repo.GamesRepo
teamsRepo *teams_repo.TeamsRepo
scenariosRepo *scenarios_repo.ScenariosRepo
domain string
passwordGenerator password_generator.IPasswordGenerator
}
func NewGameService(
@@ -20,12 +22,14 @@ func NewGameService(
teamsRepo *teams_repo.TeamsRepo,
scenariosRepo *scenarios_repo.ScenariosRepo,
domain string,
passwordGenerator password_generator.IPasswordGenerator,
) *GameService {
return &GameService{
gamesRepo: gamesRepo,
teamsRepo: teamsRepo,
scenariosRepo: scenariosRepo,
domain: domain,
gamesRepo: gamesRepo,
teamsRepo: teamsRepo,
scenariosRepo: scenariosRepo,
domain: domain,
passwordGenerator: passwordGenerator,
}
}
@@ -91,3 +95,29 @@ func (s *GameService) UpdateGame(
) error {
return s.gamesRepo.UpdateGame(ctx, id, name, description, startAt, scenarioId)
}
func (s *GameService) AddTeam(
ctx context.Context,
creatorId int,
gameId int,
name string,
) error {
game, err := s.gamesRepo.GetGameByID(ctx, gameId)
if err != nil {
return err
}
password, err := s.passwordGenerator.Generate()
if err != nil {
return err
}
_, err = s.teamsRepo.AddTeam(ctx, creatorId, gameId, name, game.ScenarioId, password)
return err
}
func (s *GameService) UpdateTeam(ctx context.Context, teamId int, name string) error {
return s.teamsRepo.UpdateTeam(ctx, teamId, name)
}
func (s *GameService) DeleteTeam(ctx context.Context, teamId int) error {
return s.teamsRepo.DeleteTeam(ctx, teamId)
}
+1
View File
@@ -3,5 +3,6 @@ package game_service
type Team struct {
ID int
Name string
Status string
Password string
}
@@ -8,6 +8,7 @@ func mapTeam(
return &Team{
ID: o.ID,
Name: o.Name,
Status: o.Status,
Password: o.Password,
}
}