From b6f787571026bdb758c807eb507b207dfc161cbc Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 29 Oct 2025 20:55:51 +0300 Subject: [PATCH 1/2] update --- migrations.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/migrations.sql b/migrations.sql index b5fd81c..1ffe7e5 100644 --- a/migrations.sql +++ b/migrations.sql @@ -70,7 +70,12 @@ CREATE TABLE labels( ); CREATE TABLE card_labels ( + id BIGSERIAL PRIMARY KEY, user_uuid VARCHAR(36) NOT NULL, label_uuid VARCHAR(36) NOT NULL, merch_uuid VARCHAR(36) NOT NULL ); + +ALTER TABLE card_labels + ADD CONSTRAINT card_labels_unique_user_label_merch + UNIQUE (user_uuid, label_uuid, merch_uuid); \ No newline at end of file From 8186d8a46cbfeddef48bb142fcccc6292a2bbc14 Mon Sep 17 00:00:00 2001 From: nquidox Date: Wed, 29 Oct 2025 20:56:26 +0300 Subject: [PATCH 2/2] getMerchLabels + fixes --- internal/api/merch/controller.go | 38 +++++++++++++++++++++++++++++++- internal/api/merch/dto.go | 1 + internal/api/merch/repository.go | 11 +++++++++ internal/api/merch/service.go | 31 +++++++++++++++++++------- 4 files changed, 72 insertions(+), 9 deletions(-) diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 7f0ae7a..41a6983 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -43,13 +43,14 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref imagesGroup.GET("/:uuid", h.controller.getMerchImage) imagesGroup.DELETE("/:uuid", h.controller.deleteMerchImage) - labelsGroup := merchGroup.Group("/labels") + labelsGroup := merchGroup.Group("/labels", authMW) labelsGroup.POST("", h.controller.createLabel) labelsGroup.GET("", h.controller.getLabels) labelsGroup.PUT("/:uuid", h.controller.updateLabel) labelsGroup.DELETE("/:uuid", h.controller.deleteLabel) labelsGroup.POST("/attach", h.controller.attachLabel) labelsGroup.POST("/detach", h.controller.detachLabel) + labelsGroup.GET("/:uuid", h.controller.getMerchLabels) } // @Summary Добавить новый мерч @@ -630,3 +631,38 @@ func (co *controller) detachLabel(c *gin.Context) { } c.Status(http.StatusOK) } + +// @Summary Получить метки товара по его uuid +// @Description Получить метки товара по его uuid +// @Tags Merch labels +// @Security BearerAuth +// @Param uuid path string true "label uuid" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch/labels/{uuid} [get] +func (co *controller) getMerchLabels(c *gin.Context) { + const logMsg = "Merch | Get merch labels" + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + + merchUuid := c.Param("uuid") + if merchUuid == "" { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: "label uuid is empty"}) + log.WithError(err).Error(logMsg) + return + } + + response, err := co.service.getMerchLabels(userUuid, merchUuid) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error(logMsg) + return + } + c.JSON(http.StatusOK, response) +} diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index ad63c26..2f267b1 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -10,6 +10,7 @@ type MerchDTO struct { Name string `json:"name"` OriginSurugaya SurugayaDTO `json:"origin_surugaya"` OriginMandarake MandarakeDTO `json:"origin_mandarake"` + Labels []string `json:"labels,omitempty" gorm:"-"` } type SurugayaDTO struct { diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index d1567de..b5ecf39 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -48,6 +48,7 @@ type labels interface { attachLabel(label CardLabel) error detachLabel(label CardLabel) error getAttachedLabelsByList(list []string) ([]CardLabel, error) + getAttachedLabelsByUuid(userUuid, merchUuid string) ([]CardLabel, error) } func (r *Repo) addMerch(bundle merchBundle) error { @@ -300,3 +301,13 @@ func (r *Repo) getAttachedLabelsByList(list []string) ([]CardLabel, error) { return labelsList, nil } + +func (r *Repo) getAttachedLabelsByUuid(userUuid, merchUuid string) ([]CardLabel, error) { + var labelsList []CardLabel + + if err := r.db.Model(&CardLabel{}).Where("user_uuid = ? AND merch_uuid = ?", userUuid, merchUuid).Find(&labelsList).Error; err != nil { + return nil, err + } + + return labelsList, nil +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 1632329..4689453 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -411,13 +411,14 @@ func (s *service) deleteMerchImage(ctx context.Context, userUuid, merchUuid stri return fmt.Errorf("no merch found for user %s with uuid %s", userUuid, merchUuid) } - if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid)); err != nil { - return err - } - - if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid)); err != nil { - return err - } + //uncomment for MinIO + //if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/thumbnail.jpg", userUuid, merchUuid)); err != nil { + // return err + //} + // + //if err = s.media.Delete(ctx, s.bucketName, fmt.Sprintf("%s/merch/%s/full.jpg", userUuid, merchUuid)); err != nil { + // return err + //} return nil } @@ -561,7 +562,7 @@ func (s *service) updateLabel(userUuid, labelUuid string, label LabelDTO) error } if label.BgColor != "" { - updateMap["bgcolor"] = label.BgColor + updateMap["bg_color"] = label.BgColor } return s.repo.updateLabel(userUuid, labelUuid, updateMap) @@ -597,3 +598,17 @@ func (s *service) detachLabel(userUuid string, label LabelLink) error { } return s.repo.detachLabel(detach) } + +func (s *service) getMerchLabels(userUuid, merchUuid string) ([]string, error) { + getLabels, err := s.repo.getAttachedLabelsByUuid(userUuid, merchUuid) + if err != nil { + return nil, err + } + + response := make([]string, 0, len(getLabels)) + for _, label := range getLabels { + response = append(response, label.LabelUuid) + } + + return response, nil +}