some fixes

This commit is contained in:
nquidox 2026-03-13 16:52:39 +03:00
parent c1e8922968
commit 9fe21b4b78
3 changed files with 60 additions and 30 deletions

View file

@ -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()