From ddc00523e03486ca5c48701643196bac0d125cfb Mon Sep 17 00:00:00 2001 From: Michael Chus Date: Sun, 24 May 2026 19:06:39 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=D1=83=D0=B1=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20categoryRepo=20=D0=B8=D0=B7=20ExportService,=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=80=D1=8F=D0=B4=D0=BE=D0=BA=20=D0=B8=D0=B7=20DefaultCa?= =?UTF-8?q?tegories?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Категория лота приходит из прайслиста — запрашивать её из серверной БД нарушало принцип local-first. Сигнатура NewExportService упрощена, все call-sites обновлены. Co-Authored-By: Claude Sonnet 4.6 --- cmd/qfs/main.go | 2 +- internal/handlers/export_test.go | 12 ++++++------ internal/services/export.go | 21 +++++---------------- internal/services/export_test.go | 22 +++++++++++----------- 4 files changed, 23 insertions(+), 34 deletions(-) diff --git a/cmd/qfs/main.go b/cmd/qfs/main.go index ac31ca2..af0c6e5 100644 --- a/cmd/qfs/main.go +++ b/cmd/qfs/main.go @@ -678,7 +678,7 @@ func setupRouter(cfg *config.Config, local *localdb.LocalDB, connMgr *db.Connect syncService = sync.NewService(connMgr, local) componentService := services.NewComponentService(nil, nil, nil) quoteService := services.NewQuoteService(nil, nil, nil, local, nil) - exportService := services.NewExportService(cfg.Export, nil, local) + exportService := services.NewExportService(cfg.Export, local) // isOnline function for local-first architecture isOnline := func() bool { diff --git a/internal/handlers/export_test.go b/internal/handlers/export_test.go index f6c2ed5..da8cf1d 100644 --- a/internal/handlers/export_test.go +++ b/internal/handlers/export_test.go @@ -30,7 +30,7 @@ func TestExportCSV_Success(t *testing.T) { gin.SetMode(gin.TestMode) // Create handler with mocks - exportSvc := services.NewExportService(config.ExportConfig{}, nil, nil) + exportSvc := services.NewExportService(config.ExportConfig{}, nil) handler := NewExportHandler( exportSvc, &mockConfigService{}, @@ -105,7 +105,7 @@ func TestExportCSV_Success(t *testing.T) { func TestExportCSV_InvalidRequest(t *testing.T) { gin.SetMode(gin.TestMode) - exportSvc := services.NewExportService(config.ExportConfig{}, nil, nil) + exportSvc := services.NewExportService(config.ExportConfig{}, nil) handler := NewExportHandler( exportSvc, &mockConfigService{}, @@ -139,7 +139,7 @@ func TestExportCSV_InvalidRequest(t *testing.T) { func TestExportCSV_EmptyItems(t *testing.T) { gin.SetMode(gin.TestMode) - exportSvc := services.NewExportService(config.ExportConfig{}, nil, nil) + exportSvc := services.NewExportService(config.ExportConfig{}, nil) handler := NewExportHandler( exportSvc, &mockConfigService{}, @@ -181,7 +181,7 @@ func TestExportConfigCSV_Success(t *testing.T) { CreatedAt: time.Now(), } - exportSvc := services.NewExportService(config.ExportConfig{}, nil, nil) + exportSvc := services.NewExportService(config.ExportConfig{}, nil) handler := NewExportHandler( exportSvc, &mockConfigService{config: mockConfig}, @@ -228,7 +228,7 @@ func TestExportConfigCSV_Success(t *testing.T) { func TestExportConfigCSV_NotFound(t *testing.T) { gin.SetMode(gin.TestMode) - exportSvc := services.NewExportService(config.ExportConfig{}, nil, nil) + exportSvc := services.NewExportService(config.ExportConfig{}, nil) handler := NewExportHandler( exportSvc, &mockConfigService{err: errors.New("config not found")}, @@ -271,7 +271,7 @@ func TestExportConfigCSV_EmptyItems(t *testing.T) { CreatedAt: time.Now(), } - exportSvc := services.NewExportService(config.ExportConfig{}, nil, nil) + exportSvc := services.NewExportService(config.ExportConfig{}, nil) handler := NewExportHandler( exportSvc, &mockConfigService{config: mockConfig}, diff --git a/internal/services/export.go b/internal/services/export.go index b0efe0f..7606da9 100644 --- a/internal/services/export.go +++ b/internal/services/export.go @@ -13,20 +13,17 @@ import ( "git.mchus.pro/mchus/quoteforge/internal/config" "git.mchus.pro/mchus/quoteforge/internal/localdb" "git.mchus.pro/mchus/quoteforge/internal/models" - "git.mchus.pro/mchus/quoteforge/internal/repository" ) type ExportService struct { - config config.ExportConfig - categoryRepo *repository.CategoryRepository - localDB *localdb.LocalDB + config config.ExportConfig + localDB *localdb.LocalDB } -func NewExportService(cfg config.ExportConfig, categoryRepo *repository.CategoryRepository, local *localdb.LocalDB) *ExportService { +func NewExportService(cfg config.ExportConfig, local *localdb.LocalDB) *ExportService { return &ExportService{ - config: cfg, - categoryRepo: categoryRepo, - localDB: local, + config: cfg, + localDB: local, } } @@ -127,15 +124,7 @@ func (s *ExportService) ToCSV(w io.Writer, data *ProjectExportData) error { return fmt.Errorf("failed to write header: %w", err) } - // Build category order: start from DefaultCategories, override with live DB values if available. categoryOrder := defaultCategoryOrder() - if s.categoryRepo != nil { - if categories, err := s.categoryRepo.GetAll(); err == nil { - for _, cat := range categories { - categoryOrder[cat.Code] = cat.DisplayOrder - } - } - } for i, block := range data.Configs { lineNo := block.Line diff --git a/internal/services/export_test.go b/internal/services/export_test.go index b7c236e..c53f9ef 100644 --- a/internal/services/export_test.go +++ b/internal/services/export_test.go @@ -33,7 +33,7 @@ func newTestProjectData(items []ExportItem, article string, serverCount int) *Pr } func TestToCSV_UTF8BOM(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := newTestProjectData([]ExportItem{ { @@ -63,7 +63,7 @@ func TestToCSV_UTF8BOM(t *testing.T) { } func TestToCSV_SemicolonDelimiter(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := newTestProjectData([]ExportItem{ { @@ -130,7 +130,7 @@ func TestToCSV_SemicolonDelimiter(t *testing.T) { } func TestToCSV_ServerRow(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := newTestProjectData([]ExportItem{ {LotName: "LOT-001", Category: "CAT", Quantity: 1, UnitPrice: 100.0, TotalPrice: 100.0}, @@ -175,7 +175,7 @@ func TestToCSV_ServerRow(t *testing.T) { } func TestToCSV_CategorySorting(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := newTestProjectData([]ExportItem{ {LotName: "LOT-001", Category: "CAT-A", Quantity: 1, UnitPrice: 100.0, TotalPrice: 100.0}, @@ -214,7 +214,7 @@ func TestToCSV_CategorySorting(t *testing.T) { } func TestToCSV_EmptyData(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := &ProjectExportData{ Configs: []ConfigExportBlock{}, @@ -247,7 +247,7 @@ func TestToCSV_EmptyData(t *testing.T) { } func TestToCSVBytes_BackwardCompat(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := newTestProjectData([]ExportItem{ {LotName: "LOT-001", Category: "CAT", Quantity: 1, UnitPrice: 100.0, TotalPrice: 100.0}, @@ -270,7 +270,7 @@ func TestToCSVBytes_BackwardCompat(t *testing.T) { } func TestToCSV_WriterError(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := newTestProjectData([]ExportItem{ {LotName: "LOT-001", Category: "CAT", Quantity: 1, UnitPrice: 100.0, TotalPrice: 100.0}, @@ -284,7 +284,7 @@ func TestToCSV_WriterError(t *testing.T) { } func TestToCSV_MultipleBlocks(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := &ProjectExportData{ Configs: []ConfigExportBlock{ @@ -359,7 +359,7 @@ func TestToCSV_MultipleBlocks(t *testing.T) { } func TestProjectToExportData_SortsByLine(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) configs := []models.Configuration{ { @@ -445,7 +445,7 @@ func TestFormatPriceComma(t *testing.T) { } func TestToPricingCSV_UsesSelectedColumns(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) data := &ProjectPricingExportData{ Configs: []ProjectPricingExportConfig{ { @@ -519,7 +519,7 @@ func TestToPricingCSV_UsesSelectedColumns(t *testing.T) { } func TestProjectToPricingExportData_UsesCartRowsWithoutBOM(t *testing.T) { - svc := NewExportService(config.ExportConfig{}, nil, nil) + svc := NewExportService(config.ExportConfig{}, nil) configs := []models.Configuration{ { UUID: "cfg-1",