diff --git a/bin/evening_detective_server b/bin/evening_detective_server index f4932ec..57260e6 100755 Binary files a/bin/evening_detective_server and b/bin/evening_detective_server differ diff --git a/cmd/evening_detective_server/gateway_test.go b/cmd/evening_detective_server/gateway_test.go index 2722ae8..eaa847f 100644 --- a/cmd/evening_detective_server/gateway_test.go +++ b/cmd/evening_detective_server/gateway_test.go @@ -84,7 +84,7 @@ func (s *testStub) DownloadTeamQR(ctx context.Context, req *proto.GetGameReq) (* if req.Id != 7 { return nil, status.Error(codes.NotFound, "Игра не найдена") } - data, err := team_qr_pdf.NewGenerator().Generate([]team_qr_pdf.Card{{Name: "Знатоки", URL: "https://example.org/team-story/1?password=test"}}) + data, err := team_qr_pdf.Generate([]team_qr_pdf.Card{{Name: "Знатоки", URL: "https://example.org/team-story/1?password=test"}}) if err != nil { return nil, err } diff --git a/cmd/evening_detective_server/main.go b/cmd/evening_detective_server/main.go index bfc2f0b..54b2e2d 100644 --- a/cmd/evening_detective_server/main.go +++ b/cmd/evening_detective_server/main.go @@ -12,7 +12,6 @@ import ( "evening_detective_server/internal/modules/processor_jwt" "evening_detective_server/internal/modules/scenario_archive" "evening_detective_server/internal/modules/storytelling" - "evening_detective_server/internal/modules/team_qr_pdf" "evening_detective_server/internal/repos/actions_repo" "evening_detective_server/internal/repos/applications_repo" "evening_detective_server/internal/repos/game_map_repo" @@ -149,7 +148,7 @@ func main() { answerService := answer_service.NewAnswerService(gameRepo, teamRepo, scenarioService, answersRepo, actionsRepo) - teamService := team_service.NewTeamService(gameRepo, teamRepo, passwordGenerator, team_qr_pdf.NewGenerator(), os.Getenv("APP_BASE_URL")) + teamService := team_service.NewTeamService(gameRepo, teamRepo, passwordGenerator, os.Getenv("APP_BASE_URL")) participationService := participation_service.NewParticipationService(teamRepo, os.Getenv("APP_BASE_URL")) go participationService.RunParticipationEmails(context.Background(), emailSender, os.Getenv("PARTICIPATION_SECRET"), os.Getenv("GAME_TIMEZONE")) diff --git a/internal/app/team_qr.go b/internal/app/team_qr.go index f778720..537feea 100644 --- a/internal/app/team_qr.go +++ b/internal/app/team_qr.go @@ -34,7 +34,7 @@ func (s *server) DownloadTeamQR(ctx context.Context, req *proto.GetGameReq) (*ht switch { case errors.Is(err, games_repo.ErrGameNotFound): return nil, status.Error(codes.NotFound, err.Error()) - case errors.Is(err, team_qr_pdf.ErrNoTeams): + case errors.Is(err, team_qr_pdf.ErrNoCards): return nil, status.Error(codes.FailedPrecondition, err.Error()) default: return nil, status.Error(codes.Internal, err.Error()) diff --git a/internal/modules/team_qr_pdf/generator.go b/internal/modules/team_qr_pdf/generator.go index 04728ff..dee04c2 100644 --- a/internal/modules/team_qr_pdf/generator.go +++ b/internal/modules/team_qr_pdf/generator.go @@ -3,6 +3,7 @@ package team_qr_pdf import ( "bytes" _ "embed" + "errors" "fmt" "net/url" "strings" @@ -11,16 +12,19 @@ import ( "github.com/skip2/go-qrcode" ) +var ErrNoCards = errors.New("нет карточек") + +type Card struct { + Name string + URL string +} + //go:embed fonts/DejaVuSansCondensed.ttf var font []byte -type generator struct{} - -func NewGenerator() IGenerator { return &generator{} } - -func (g *generator) Generate(cards []Card) ([]byte, error) { +func Generate(cards []Card) ([]byte, error) { if len(cards) == 0 { - return nil, ErrNoTeams + return nil, ErrNoCards } pdf := fpdf.New("P", "mm", "A4", "") pdf.SetAutoPageBreak(false, 0) diff --git a/internal/modules/team_qr_pdf/generator_test.go b/internal/modules/team_qr_pdf/generator_test.go index dde16e9..dce6013 100644 --- a/internal/modules/team_qr_pdf/generator_test.go +++ b/internal/modules/team_qr_pdf/generator_test.go @@ -10,8 +10,7 @@ import ( ) func TestGenerate(t *testing.T) { - g := NewGenerator() - if _, err := g.Generate(nil); !errors.Is(err, ErrNoTeams) { + if _, err := Generate(nil); !errors.Is(err, ErrNoCards) { t.Fatalf("empty game: %v", err) } for _, count := range []int{1, 12, 13, 24} { @@ -24,7 +23,7 @@ func TestGenerate(t *testing.T) { } } cards[0].Name = strings.Repeat("Ш", 255) - data, err := g.Generate(cards) + data, err := Generate(cards) if err != nil { t.Fatal(err) } @@ -48,7 +47,7 @@ func TestGenerate(t *testing.T) { {Name: "Команда", URL: "https://example.org/" + strings.Repeat("x", 5000)}, {Name: "Команда 🔍", URL: "https://example.org"}, } { - if _, err := g.Generate([]Card{card}); err == nil { + if _, err := Generate([]Card{card}); err == nil { t.Fatal("expected error for unsupported input") } } diff --git a/internal/modules/team_qr_pdf/interface.go b/internal/modules/team_qr_pdf/interface.go deleted file mode 100644 index 6c6de6c..0000000 --- a/internal/modules/team_qr_pdf/interface.go +++ /dev/null @@ -1,14 +0,0 @@ -package team_qr_pdf - -import "errors" - -var ErrNoTeams = errors.New("в игре нет команд") - -type Card struct { - Name string - URL string -} - -type IGenerator interface { - Generate(cards []Card) ([]byte, error) -} diff --git a/internal/services/game_service/lifecycle_test.go b/internal/services/game_service/lifecycle_test.go index 864136e..e7e02af 100644 --- a/internal/services/game_service/lifecycle_test.go +++ b/internal/services/game_service/lifecycle_test.go @@ -11,7 +11,6 @@ import ( "evening_detective_server/internal/modules/cleaner" "evening_detective_server/internal/modules/password_generator" "evening_detective_server/internal/modules/storytelling" - "evening_detective_server/internal/modules/team_qr_pdf" "evening_detective_server/internal/repos/actions_repo" "evening_detective_server/internal/repos/applications_repo" "evening_detective_server/internal/repos/games_repo" @@ -69,7 +68,7 @@ func TestServicesLifecyclePostgres(t *testing.T) { textCleaner := cleaner.NewCleaner() scenarioService := scenarios_service.NewScenarioService(scenarios, textCleaner, "https://files.test/", nil, nil) game := game_service.NewGameService(games, teams, scenarios, scenarioService, "https://files.test/", actions, applications) - team := team_service.NewTeamService(games, teams, password_generator.NewGenerator(12), team_qr_pdf.NewGenerator(), "https://app.test") + team := team_service.NewTeamService(games, teams, password_generator.NewGenerator(12), "https://app.test") play := gameplay_service.NewGameplayService(games, teams, scenarioService, storytelling.NewStory(textCleaner), actions, applications, textCleaner, "https://files.test/") answers := answer_service.NewAnswerService(games, teams, scenarioService, team_answers_repo.NewTeamAnswersRepo(pool), actions) participation := participation_service.NewParticipationService(teams, "https://app.test") diff --git a/internal/services/team_service/service.go b/internal/services/team_service/service.go index a1244c7..91f5c41 100644 --- a/internal/services/team_service/service.go +++ b/internal/services/team_service/service.go @@ -16,7 +16,6 @@ type TeamService struct { gamesRepo *games_repo.GamesRepo teamsRepo *teams_repo.TeamsRepo passwordGenerator password_generator.IPasswordGenerator - teamQRGenerator team_qr_pdf.IGenerator appBaseURL string } @@ -24,14 +23,12 @@ func NewTeamService( gamesRepo *games_repo.GamesRepo, teamsRepo *teams_repo.TeamsRepo, passwordGenerator password_generator.IPasswordGenerator, - teamQRGenerator team_qr_pdf.IGenerator, appBaseURL string, ) *TeamService { return &TeamService{ gamesRepo: gamesRepo, teamsRepo: teamsRepo, passwordGenerator: passwordGenerator, - teamQRGenerator: teamQRGenerator, appBaseURL: appBaseURL, } } @@ -102,12 +99,12 @@ func (s *TeamService) DownloadTeamQR(ctx context.Context, id int) ([]byte, strin return nil, "", err } if len(teams) == 0 { - return nil, "", team_qr_pdf.ErrNoTeams + return nil, "", team_qr_pdf.ErrNoCards } cards, filename, err := teamQRData(s.appBaseURL, game, teams) if err != nil { return nil, "", err } - data, err := s.teamQRGenerator.Generate(cards) + data, err := team_qr_pdf.Generate(cards) return data, filename, err }