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

View file

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

View file

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