diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index ddf3bd6..7f0ae7a 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -2,7 +2,6 @@ package merch import ( "context" - "errors" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "merch-parser-api/internal/interfaces" @@ -45,8 +44,8 @@ func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, ref imagesGroup.DELETE("/:uuid", h.controller.deleteMerchImage) labelsGroup := merchGroup.Group("/labels") - labelsGroup.POST("/", h.controller.createLabel) - labelsGroup.GET("/", h.controller.getLabels) + 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) @@ -431,8 +430,8 @@ func (co *controller) deleteMerchImage(c *gin.Context) { // @Security BearerAuth // @Param payload body LabelDTO true "payload" // @Success 200 -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/labels [post] func (co *controller) createLabel(c *gin.Context) { const logMsg = "Merch | Create label" @@ -464,9 +463,9 @@ func (co *controller) createLabel(c *gin.Context) { // @Description Получить все метки товаров // @Tags Merch labels // @Security BearerAuth -// @Success 200 {array} LabelDTO -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Success 200 {array} LabelsList +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 // @Router /merch/labels [get] func (co *controller) getLabels(c *gin.Context) { const logMsg = "Merch | Get labels" @@ -492,7 +491,7 @@ func (co *controller) getLabels(c *gin.Context) { // @Description Изменить метку // @Tags Merch labels // @Security BearerAuth -// @Param uuid path string true "label uuid" +// @Param uuid path string true "label uuid" // @Param payload body LabelDTO true "payload" // @Success 200 // @Failure 400 {object} responses.ErrorResponse400 @@ -522,14 +521,7 @@ func (co *controller) updateLabel(c *gin.Context) { return } - if labelUuid != payload.LabelUuid { - err = errors.New("label uuid is different") - c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) - log.WithError(err).Error(logMsg) - return - } - - if err = co.service.updateLabel(userUuid, payload); err != nil { + if err = co.service.updateLabel(userUuid, labelUuid, payload); err != nil { c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) log.WithError(err).Error(logMsg) return diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index fc1b077..32e72e4 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -60,10 +60,16 @@ type ImageLink struct { } type LabelDTO struct { + Name string `json:"name"` + Color string `json:"color,omitempty"` + BgColor string `json:"bg_color,omitempty"` +} + +type LabelsList struct { LabelUuid string `json:"label_uuid"` Name string `json:"name"` - Color string `json:"color"` - BgColor string `json:"bg_color"` + Color string `json:"color,omitempty"` + BgColor string `json:"bg_color,omitempty"` } type LabelLink struct { diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index 088cb39..935a194 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -63,8 +63,16 @@ type Label struct { BgColor string `json:"bg_color" gorm:"column:bg_color"` } +func (Label) TableName() string { + return "labels" +} + type CardLabel struct { LabelUuid string `json:"label_uuid"` UserUuid string `json:"user_uuid"` MerchUuid string `json:"merch_uuid"` } + +func (CardLabel) TableName() string { + return "card_labels" +} diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 24ff87b..5b3c308 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -43,7 +43,7 @@ type prices interface { type labels interface { createLabel(label Label) error getLabels(userUuid string) ([]Label, error) - updateLabel(userUuid, labelUuid string, label map[string]string) error + updateLabel(userUuid, labelUuid string, label map[string]any) error deleteLabel(userUuid, labelUuid string) error attachLabel(label CardLabel) error detachLabel(label CardLabel) error @@ -251,22 +251,24 @@ func (r *Repo) upsertOrigin(model any) error { } func (r *Repo) createLabel(label Label) error { - return r.db.Model(&Label{}).Create(label).Error + return r.db.Model(&Label{}).Create(&label).Error } + func (r *Repo) getLabels(userUuid string) ([]Label, error) { - var labels []Label + var labelsList []Label if err := r.db. + Model(&Label{}). Where("user_uuid = ?", userUuid). Where("deleted_at IS NULL"). - Find(labels).Error; err != nil { + Find(&labelsList).Error; err != nil { return nil, err } - return labels, nil + return labelsList, nil } -func (r *Repo) updateLabel(userUuid, labelUuid string, label map[string]string) error { +func (r *Repo) updateLabel(userUuid, labelUuid string, label map[string]any) error { return r.db.Model(&Label{}). Where("user_uuid =? AND label_uuid = ?", userUuid, labelUuid). Updates(label).Error @@ -284,6 +286,6 @@ func (r *Repo) attachLabel(label CardLabel) error { func (r *Repo) detachLabel(label CardLabel) error { return r.db. - Where("userUuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). + Where("user_uuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). Delete(&CardLabel{}).Error } diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index bf49893..c6902ff 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -504,15 +504,15 @@ func (s *service) createLabel(label LabelDTO, userUuid string) error { return s.repo.createLabel(newLabel) } -func (s *service) getLabels(userUuid string) ([]LabelDTO, error) { +func (s *service) getLabels(userUuid string) ([]LabelsList, error) { stored, err := s.repo.getLabels(userUuid) if err != nil { return nil, err } - response := make([]LabelDTO, 0, len(stored)) + response := make([]LabelsList, 0, len(stored)) for _, label := range stored { - response = append(response, LabelDTO{ + response = append(response, LabelsList{ LabelUuid: label.LabelUuid, Name: label.Name, Color: label.Color, @@ -522,8 +522,8 @@ func (s *service) getLabels(userUuid string) ([]LabelDTO, error) { return response, nil } -func (s *service) updateLabel(userUuid string, label LabelDTO) error { - updateMap := make(map[string]string, 3) +func (s *service) updateLabel(userUuid, labelUuid string, label LabelDTO) error { + updateMap := make(map[string]any, 3) if label.Name != "" { updateMap["name"] = label.Name @@ -537,7 +537,7 @@ func (s *service) updateLabel(userUuid string, label LabelDTO) error { updateMap["bgcolor"] = label.BgColor } - return s.repo.updateLabel(userUuid, label.LabelUuid, updateMap) + return s.repo.updateLabel(userUuid, labelUuid, updateMap) } func (s *service) deleteLabel(userUuid, labelUuid string) error {