some fixes
This commit is contained in:
parent
c1e8922968
commit
9fe21b4b78
3 changed files with 60 additions and 30 deletions
|
|
@ -7,14 +7,14 @@ import (
|
||||||
|
|
||||||
type Labels interface {
|
type Labels interface {
|
||||||
createLabel(ctx context.Context, userId int64, label *Label) error
|
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
|
updateLabel(ctx context.Context, userId int64, label *Label) error
|
||||||
deleteLabel(ctx context.Context, userId int64, labelUuid string, now sql.NullTime) error
|
deleteLabel(ctx context.Context, userId int64, labelUuid string, now sql.NullTime) error
|
||||||
|
|
||||||
attachLabel(ctx context.Context, userId int64, label *CardLabel) error
|
attachLabel(ctx context.Context, userId int64, label *LabelLink) error
|
||||||
detachLabel(ctx context.Context, userId int64, label *CardLabel) 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)
|
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
|
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 := `
|
q := `
|
||||||
SELECT uuid, name, color, bg_color
|
SELECT uuid, name, color, bg_color
|
||||||
FROM labels
|
FROM labels
|
||||||
|
|
@ -44,10 +44,10 @@ func (r *repo) getLabels(ctx context.Context, userId int64) ([]Label, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var labels []Label
|
var labels []LabelsList
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var l Label
|
var l LabelsList
|
||||||
if err = rows.Scan(&l.Uuid, &l.Name, &l.Color, &l.BgColor); err != nil {
|
if err = rows.Scan(&l.LabelUuid, &l.Name, &l.Color, &l.BgColor); err != nil {
|
||||||
rows.Close()
|
rows.Close()
|
||||||
return nil, err
|
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 {
|
func (r *repo) updateLabel(ctx context.Context, userId int64, l *Label) error {
|
||||||
q := `
|
q := `
|
||||||
INSERT INTO labels (created_at, updated_at, deleted_at, user_id, uuid, name, color, bg_color)
|
UPDATE labels
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
SET
|
||||||
ON CONFLICT (uuid) DO UPDATE SET
|
updated_at = $1,
|
||||||
updated_at = $2,
|
name = COALESCE(NULLIF($2, ''), labels.name),
|
||||||
name = COALESCE(NULLIF(EXCLUDED.name, ''), labels.name),
|
color = COALESCE(NULLIF($3, ''), labels.color),
|
||||||
color = COALESCE(NULLIF(EXCLUDED.color, ''), labels.color),
|
bg_color = COALESCE(NULLIF($4, ''), labels.bg_color)
|
||||||
bg_color = COALESCE(NULLIF(EXCLUDED.bg_color, ''), 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -90,7 +91,7 @@ func (r *repo) deleteLabel(ctx context.Context, userId int64, labelUuid string,
|
||||||
return nil
|
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 := `
|
q := `
|
||||||
INSERT INTO card_labels (user_id, label_id, merch_id)
|
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))
|
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
|
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 := `
|
q := `
|
||||||
DELETE FROM card_labels
|
DELETE FROM card_labels
|
||||||
WHERE user_id = $1
|
WHERE user_id = $1
|
||||||
|
|
@ -116,7 +117,7 @@ func (r *repo) detachLabel(ctx context.Context, userId int64, label *CardLabel)
|
||||||
return nil
|
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 := `
|
q := `
|
||||||
SELECT l.uuid, cl.merch_id
|
SELECT l.uuid, cl.merch_id
|
||||||
FROM card_labels AS cl
|
FROM card_labels AS cl
|
||||||
|
|
@ -130,7 +131,7 @@ func (r *repo) getManyAttachedLabelsByList(ctx context.Context, userId int64, li
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cardLabelsMap := make(map[int64]string)
|
cardLabelsMap := make(map[int64][]string)
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var (
|
var (
|
||||||
|
|
@ -142,7 +143,7 @@ func (r *repo) getManyAttachedLabelsByList(ctx context.Context, userId int64, li
|
||||||
rows.Close()
|
rows.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
cardLabelsMap[merchId] = labelUuid
|
cardLabelsMap[merchId] = append(cardLabelsMap[merchId], labelUuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
rows.Close()
|
rows.Close()
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ type MerchRepo interface {
|
||||||
createMerch(ctx context.Context, merch *Merch, extra []ExtraData) error
|
createMerch(ctx context.Context, merch *Merch, extra []ExtraData) error
|
||||||
|
|
||||||
// getMany returns list of only main merch record, without origins extra data
|
// 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)
|
getMerchIdByUuid(ctx context.Context, userId int64, uuid string) (int64, error)
|
||||||
getMerchUuidMap(ctx context.Context, merchUuids []string) (map[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)
|
return tx.Commit(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *repo) getMany(ctx context.Context, userId int64) ([]merchDTO, error) {
|
func (r *repo) getMany(ctx context.Context, userId int64) ([]getMerchInternal, error) {
|
||||||
q := `SELECT created_at, updated_at, merch_uuid, name FROM merch WHERE deleted_at IS NULL AND user_id = $1`
|
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)
|
rows, err := r.db.Query(ctx, q, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -84,10 +84,10 @@ func (r *repo) getMany(ctx context.Context, userId int64) ([]merchDTO, error) {
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
var result []merchDTO
|
var result []getMerchInternal
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var m merchDTO
|
var m getMerchInternal
|
||||||
if err = rows.Scan(&m.CreatedAt, &m.UpdatedAt, &m.MerchUuid, &m.Name); err != nil {
|
if err = rows.Scan(&m.Id, &m.MerchUuid, &m.Name); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
result = append(result, m)
|
result = append(result, m)
|
||||||
|
|
|
||||||
|
|
@ -49,8 +49,37 @@ func (s *service) createMerch(ctx context.Context, userId int64, payload *newMer
|
||||||
return s.repo.createMerch(ctx, newMerch, merchExtra)
|
return s.repo.createMerch(ctx, newMerch, merchExtra)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) getMany(ctx context.Context, userId int64) ([]merchDTO, error) {
|
func (s *service) getMany(ctx context.Context, userId int64) ([]ListResponse, error) {
|
||||||
return s.repo.getMany(ctx, userId)
|
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) {
|
func (s *service) updateMerch(ctx context.Context, userId int64, payload *updateMerchDTO) (*merchDTO, error) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue