This commit is contained in:
2026-09-17 13:32:05 +07:00
parent 5ba640bfe4
commit dfa4aa9d8b
9 changed files with 19 additions and 35 deletions
+1 -1
View File
@@ -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())
+10 -6
View File
@@ -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)
@@ -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")
}
}
-14
View File
@@ -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)
}
@@ -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")
+2 -5
View File
@@ -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
}