From 9fe21b4b78588ec9047553b20e5e7588b7be39c1 Mon Sep 17 00:00:00 2001 From: nquidox Date: Fri, 13 Mar 2026 16:52:39 +0300 Subject: [PATCH] some fixes --- internal/merch/repository_labels.go | 45 +++++++++++++++-------------- internal/merch/repository_merch.go | 12 ++++---- internal/merch/service_merch.go | 33 +++++++++++++++++++-- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/internal/merch/repository_labels.go b/internal/merch/repository_labels.go index 13752ce..a529968 100644 --- a/internal/merch/repository_labels.go +++ b/internal/merch/repository_labels.go @@ -7,14 +7,14 @@ import ( type Labels interface { createLabel(ctx context.Context, userId int64, label *Label) error - getLabels(ctx context.Context, userId int64) ([]Label, error) + getLabels(ctx context.Context, userId int64) ([]LabelsList, error) updateLabel(ctx context.Context, userId int64, label *Label) error deleteLabel(ctx context.Context, userId int64, labelUuid string, now sql.NullTime) error - attachLabel(ctx context.Context, userId int64, label *CardLabel) error - detachLabel(ctx context.Context, userId int64, label *CardLabel) error + attachLabel(ctx context.Context, userId int64, label *LabelLink) error + detachLabel(ctx context.Context, userId int64, label *LabelLink) error - getManyAttachedLabelsByList(ctx context.Context, userId int64, list []int64) (map[int64]string, error) + getManyAttachedLabelsByList(ctx context.Context, userId int64, list []int64) (map[int64][]string, error) getAttachedLabelsByUuid(ctx context.Context, userId int64, merchUuid string) ([]string, error) } @@ -31,7 +31,7 @@ func (r *repo) createLabel(ctx context.Context, userId int64, l *Label) error { return nil } -func (r *repo) getLabels(ctx context.Context, userId int64) ([]Label, error) { +func (r *repo) getLabels(ctx context.Context, userId int64) ([]LabelsList, error) { q := ` SELECT uuid, name, color, bg_color FROM labels @@ -44,10 +44,10 @@ func (r *repo) getLabels(ctx context.Context, userId int64) ([]Label, error) { return nil, err } - var labels []Label + var labels []LabelsList for rows.Next() { - var l Label - if err = rows.Scan(&l.Uuid, &l.Name, &l.Color, &l.BgColor); err != nil { + var l LabelsList + if err = rows.Scan(&l.LabelUuid, &l.Name, &l.Color, &l.BgColor); err != nil { rows.Close() return nil, err } @@ -64,16 +64,17 @@ func (r *repo) getLabels(ctx context.Context, userId int64) ([]Label, error) { func (r *repo) updateLabel(ctx context.Context, userId int64, l *Label) error { q := ` - INSERT INTO labels (created_at, updated_at, deleted_at, user_id, uuid, name, color, bg_color) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8) - ON CONFLICT (uuid) DO UPDATE SET - updated_at = $2, - name = COALESCE(NULLIF(EXCLUDED.name, ''), labels.name), - color = COALESCE(NULLIF(EXCLUDED.color, ''), labels.color), - bg_color = COALESCE(NULLIF(EXCLUDED.bg_color, ''), labels.bg_color) - ` + UPDATE labels + SET + updated_at = $1, + name = COALESCE(NULLIF($2, ''), labels.name), + color = COALESCE(NULLIF($3, ''), labels.color), + bg_color = COALESCE(NULLIF($4, ''), labels.bg_color) + WHERE uuid = $5 + AND user_id = $6; + ` - _, err := r.db.Exec(ctx, q, l.CreatedAt, l.UpdatedAt, l.DeletedAt, userId, l.Uuid, l.Name, l.Color, l.BgColor) + _, err := r.db.Exec(ctx, q, l.UpdatedAt, l.Name, l.Color, l.BgColor, l.Uuid, userId) if err != nil { return err } @@ -90,7 +91,7 @@ func (r *repo) deleteLabel(ctx context.Context, userId int64, labelUuid string, return nil } -func (r *repo) attachLabel(ctx context.Context, userId int64, label *CardLabel) error { +func (r *repo) attachLabel(ctx context.Context, userId int64, label *LabelLink) error { q := ` INSERT INTO card_labels (user_id, label_id, merch_id) VALUES ($1, (SELECT id FROM labels WHERE uuid = $2), (SELECT id FROM merch WHERE merch_uuid = $3 AND user_id = $1)) @@ -102,7 +103,7 @@ func (r *repo) attachLabel(ctx context.Context, userId int64, label *CardLabel) return nil } -func (r *repo) detachLabel(ctx context.Context, userId int64, label *CardLabel) error { +func (r *repo) detachLabel(ctx context.Context, userId int64, label *LabelLink) error { q := ` DELETE FROM card_labels WHERE user_id = $1 @@ -116,7 +117,7 @@ func (r *repo) detachLabel(ctx context.Context, userId int64, label *CardLabel) return nil } -func (r *repo) getManyAttachedLabelsByList(ctx context.Context, userId int64, list []int64) (map[int64]string, error) { +func (r *repo) getManyAttachedLabelsByList(ctx context.Context, userId int64, list []int64) (map[int64][]string, error) { q := ` SELECT l.uuid, cl.merch_id FROM card_labels AS cl @@ -130,7 +131,7 @@ func (r *repo) getManyAttachedLabelsByList(ctx context.Context, userId int64, li return nil, err } - cardLabelsMap := make(map[int64]string) + cardLabelsMap := make(map[int64][]string) for rows.Next() { var ( @@ -142,7 +143,7 @@ func (r *repo) getManyAttachedLabelsByList(ctx context.Context, userId int64, li rows.Close() return nil, err } - cardLabelsMap[merchId] = labelUuid + cardLabelsMap[merchId] = append(cardLabelsMap[merchId], labelUuid) } rows.Close() diff --git a/internal/merch/repository_merch.go b/internal/merch/repository_merch.go index c29af6d..b407347 100644 --- a/internal/merch/repository_merch.go +++ b/internal/merch/repository_merch.go @@ -14,7 +14,7 @@ type MerchRepo interface { createMerch(ctx context.Context, merch *Merch, extra []ExtraData) error // getMany returns list of only main merch record, without origins extra data - getMany(ctx context.Context, userId int64) ([]merchDTO, error) + getMany(ctx context.Context, userId int64) ([]getMerchInternal, error) getMerchIdByUuid(ctx context.Context, userId int64, uuid string) (int64, error) getMerchUuidMap(ctx context.Context, merchUuids []string) (map[string]int64, error) @@ -75,8 +75,8 @@ func (r *repo) createMerch(ctx context.Context, merch *Merch, extra []ExtraData) return tx.Commit(ctx) } -func (r *repo) getMany(ctx context.Context, userId int64) ([]merchDTO, error) { - q := `SELECT created_at, updated_at, merch_uuid, name FROM merch WHERE deleted_at IS NULL AND user_id = $1` +func (r *repo) getMany(ctx context.Context, userId int64) ([]getMerchInternal, error) { + q := `SELECT id, merch_uuid, name FROM merch WHERE deleted_at IS NULL AND user_id = $1` rows, err := r.db.Query(ctx, q, userId) if err != nil { @@ -84,10 +84,10 @@ func (r *repo) getMany(ctx context.Context, userId int64) ([]merchDTO, error) { } defer rows.Close() - var result []merchDTO + var result []getMerchInternal for rows.Next() { - var m merchDTO - if err = rows.Scan(&m.CreatedAt, &m.UpdatedAt, &m.MerchUuid, &m.Name); err != nil { + var m getMerchInternal + if err = rows.Scan(&m.Id, &m.MerchUuid, &m.Name); err != nil { return nil, err } result = append(result, m) diff --git a/internal/merch/service_merch.go b/internal/merch/service_merch.go index b3a1d2b..2f05823 100644 --- a/internal/merch/service_merch.go +++ b/internal/merch/service_merch.go @@ -49,8 +49,37 @@ func (s *service) createMerch(ctx context.Context, userId int64, payload *newMer return s.repo.createMerch(ctx, newMerch, merchExtra) } -func (s *service) getMany(ctx context.Context, userId int64) ([]merchDTO, error) { - return s.repo.getMany(ctx, userId) +func (s *service) getMany(ctx context.Context, userId int64) ([]ListResponse, error) { + allUserMerch, err := s.repo.getMany(ctx, userId) + if err != nil { + return nil, err + } + + if len(allUserMerch) == 0 || allUserMerch == nil { + logWarn(serviceLogHeader, "User has no merch records") + return nil, nil + } + + ids := make([]int64, 0, len(allUserMerch)) + for _, m := range allUserMerch { + ids = append(ids, m.Id) + } + + cardLabels, err := s.repo.getManyAttachedLabelsByList(ctx, userId, ids) + if err != nil { + return nil, err + } + + var response []ListResponse + for _, m := range allUserMerch { + response = append(response, ListResponse{ + MerchUuid: m.MerchUuid, + Name: m.Name, + Labels: cardLabels[m.Id], + }) + } + + return response, nil } func (s *service) updateMerch(ctx context.Context, userId int64, payload *updateMerchDTO) (*merchDTO, error) {