add registration

This commit is contained in:
2026-09-13 01:42:22 +07:00
parent 0723644d3f
commit c12f81a7c4
20 changed files with 517 additions and 33 deletions
@@ -0,0 +1,42 @@
package game_service
import (
"context"
"errors"
"net/mail"
"strings"
"unicode/utf8"
)
var ErrInvalidRegistration = errors.New("Укажите название команды (до 255 символов), почту капитана и положительное количество участников")
type Registration struct {
Name string `json:"name"`
Email string `json:"email"`
ParticipantsCount int32 `json:"participantsCount"`
}
func (r *Registration) Validate() error {
r.Name = strings.TrimSpace(r.Name)
r.Email = strings.TrimSpace(r.Email)
address, err := mail.ParseAddress(r.Email)
if r.Name == "" || !utf8.ValidString(r.Name) || strings.ContainsRune(r.Name, 0) || utf8.RuneCountInString(r.Name) > 255 ||
err != nil || address.Address != r.Email || len(r.Email) > 254 || r.ParticipantsCount <= 0 {
return ErrInvalidRegistration
}
return nil
}
func (s *GameService) RegisterTeam(ctx context.Context, gameID int, deliveryID string, registration Registration) (int, error) {
if err := registration.Validate(); err != nil {
return 0, err
}
if gameID <= 0 || deliveryID == "" || len(deliveryID) > 255 || strings.ContainsRune(deliveryID, 0) {
return 0, ErrInvalidRegistration
}
password, err := s.passwordGenerator.Generate()
if err != nil {
return 0, err
}
return s.teamsRepo.RegisterTeam(ctx, gameID, deliveryID, registration.Name, registration.Email, registration.ParticipantsCount, password)
}
@@ -0,0 +1,32 @@
package game_service
import (
"strings"
"testing"
)
func TestRegistrationValidation(t *testing.T) {
for _, tc := range []struct {
name, email string
count int32
valid bool
}{
{" Команда ", " captain@example.org ", 5, true},
{strings.Repeat("я", 255), "captain@example.org", 1, true},
{"", "captain@example.org", 5, false},
{strings.Repeat("я", 256), "captain@example.org", 5, false},
{"team\x00", "captain@example.org", 5, false},
{"team", "Captain <captain@example.org>", 5, false},
{"team", "not-an-email", 5, false},
{"team", "captain@example.org", 0, false},
{"team", "captain@example.org", -1, false},
} {
r := Registration{Name: tc.name, Email: tc.email, ParticipantsCount: tc.count}
if err := r.Validate(); (err == nil) != tc.valid {
t.Fatalf("validation %q %q %d: %v", tc.name, tc.email, tc.count, err)
}
if tc.valid && (r.Name != strings.TrimSpace(tc.name) || r.Email != strings.TrimSpace(tc.email)) {
t.Fatal("spaces not trimmed")
}
}
}
+8 -6
View File
@@ -3,10 +3,12 @@ package game_service
import "evening_detective_server/internal/modules/storytelling"
type Team struct {
ID int
Name string
Status string
Password string
ActionsCount int
Applications []*storytelling.Application
CaptainEmail string
ParticipantsCount int32
ID int
Name string
Status string
Password string
ActionsCount int
Applications []*storytelling.Application
}
@@ -6,10 +6,12 @@ func mapTeam(
o *repos.Team,
) *Team {
return &Team{
ID: o.ID,
Name: o.Name,
Status: o.Status,
Password: o.Password,
ID: o.ID,
CaptainEmail: o.CaptainEmail,
ParticipantsCount: o.ParticipantsCount,
Name: o.Name,
Status: o.Status,
Password: o.Password,
}
}