generated from VLADIMIR/template
up
This commit is contained in:
@@ -27,6 +27,7 @@ func (s *server) AddTeam(ctx context.Context, req *proto.AddTeamReq) (*proto.Add
|
||||
int(req.GameID),
|
||||
req.Name,
|
||||
req.CaptainEmail,
|
||||
req.ParticipantsCount,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, teamErrorStatus(err, "не удалось добавить команду")
|
||||
@@ -85,6 +86,8 @@ func teamErrorStatus(err error, internalMessage string) error {
|
||||
return status.Error(codes.NotFound, err.Error())
|
||||
case errors.Is(err, team_service.ErrInvalidCaptainEmail):
|
||||
return status.Error(codes.InvalidArgument, err.Error())
|
||||
case errors.Is(err, team_service.ErrInvalidParticipantsCount):
|
||||
return status.Error(codes.InvalidArgument, err.Error())
|
||||
default:
|
||||
return status.Error(codes.Internal, internalMessage)
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ func TestRegistrationPostgres(t *testing.T) {
|
||||
t.Fatalf("other game: %d %v", otherID, err)
|
||||
}
|
||||
for _, email := range []string{"", "manual@example.org"} {
|
||||
manualID, err := repo.AddTeamWithParticipation(ctx, 1, 2, "Manual", "password", email, false)
|
||||
manualID, err := repo.AddTeamWithParticipation(ctx, 1, 2, "Manual", "password", email, 0, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -121,11 +121,11 @@ func (r *TeamsRepo) AddTeam(
|
||||
name string,
|
||||
password string,
|
||||
) (int, error) {
|
||||
return r.AddTeamWithParticipation(ctx, creatorID, gameID, name, password, "", false)
|
||||
return r.AddTeamWithParticipation(ctx, creatorID, gameID, name, password, "", 0, false)
|
||||
}
|
||||
|
||||
// AddTeamWithParticipation creates a team with participation data.
|
||||
func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorID, gameID int, name, password, captainEmail string, autoConfirm bool) (int, error) {
|
||||
func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorID, gameID int, name, password, captainEmail string, participantsCount int32, autoConfirm bool) (int, error) {
|
||||
id := 0
|
||||
status, source := "pending", ""
|
||||
if autoConfirm {
|
||||
@@ -133,8 +133,8 @@ func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorID, gam
|
||||
}
|
||||
err := r.pool.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO teams (name, creator_id, game_id, password, participation_status, participation_source, participation_answered_at, captain_email)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, CASE WHEN $5 = 'confirmed' THEN NOW() ELSE NULL END, $7)
|
||||
`INSERT INTO teams (name, creator_id, game_id, password, participation_status, participation_source, participation_answered_at, captain_email, participants_count)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, CASE WHEN $5 = 'confirmed' THEN NOW() ELSE NULL END, $7, $8)
|
||||
RETURNING id`,
|
||||
name,
|
||||
creatorID,
|
||||
@@ -143,6 +143,7 @@ func (r *TeamsRepo) AddTeamWithParticipation(ctx context.Context, creatorID, gam
|
||||
status,
|
||||
source,
|
||||
captainEmail,
|
||||
participantsCount,
|
||||
).Scan(&id)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -15,8 +15,14 @@ func TestManualTeamEmail(t *testing.T) {
|
||||
}
|
||||
for _, email := range []string{"invalid", "Captain <captain@example.org>", "a@example.org\r\nBcc: other@example.org", strings.Repeat("a", 250) + "@example.org"} {
|
||||
service := &TeamService{}
|
||||
if _, err := service.AddTeam(context.Background(), 1, 1, "Team", email); err == nil {
|
||||
if _, err := service.AddTeam(context.Background(), 1, 1, "Team", email, 0); err == nil {
|
||||
t.Errorf("accepted invalid email %q", email)
|
||||
}
|
||||
}
|
||||
for _, count := range []int32{-1, 9} {
|
||||
service := &TeamService{}
|
||||
if _, err := service.AddTeam(context.Background(), 1, 1, "Team", "", count); err != ErrInvalidParticipantsCount {
|
||||
t.Errorf("accepted invalid participants count %d: %v", count, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidCaptainEmail = errors.New("укажите корректную почту капитана")
|
||||
ErrTeamNotFound = errors.New("команда не найдена")
|
||||
ErrInvalidRegistration = errors.New("укажите название команды (до 255 символов), почту капитана и положительное количество участников")
|
||||
ErrRegistrationClosed = errors.New("игра не найдена или регистрация уже закрыта")
|
||||
ErrInvalidCaptainEmail = errors.New("укажите корректную почту капитана")
|
||||
ErrInvalidParticipantsCount = errors.New("укажите от 1 до 8 участников")
|
||||
ErrTeamNotFound = errors.New("команда не найдена")
|
||||
ErrInvalidRegistration = errors.New("укажите название команды (до 255 символов), почту капитана и положительное количество участников")
|
||||
ErrRegistrationClosed = errors.New("игра не найдена или регистрация уже закрыта")
|
||||
)
|
||||
|
||||
// Registration contains data required to register a team.
|
||||
|
||||
@@ -50,11 +50,15 @@ func (s *TeamService) AddTeam(
|
||||
gameID int,
|
||||
name string,
|
||||
captainEmail string,
|
||||
participantsCount int32,
|
||||
) (*game_model.Team, error) {
|
||||
captainEmail = strings.TrimSpace(captainEmail)
|
||||
if captainEmail != "" && !validCaptainEmail(captainEmail) {
|
||||
return nil, ErrInvalidCaptainEmail
|
||||
}
|
||||
if participantsCount < 0 || participantsCount > 8 {
|
||||
return nil, ErrInvalidParticipantsCount
|
||||
}
|
||||
password, err := s.passwordGenerator.Generate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -66,7 +70,7 @@ func (s *TeamService) AddTeam(
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorID, gameID, name, password, captainEmail, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
|
||||
id, err := s.teamsRepo.AddTeamWithParticipation(ctx, creatorID, gameID, name, password, captainEmail, participantsCount, !time.Now().Before(game.StartAt.Add(-24*time.Hour)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user