This commit is contained in:
parent
b6f7875710
commit
8186d8a46c
4 changed files with 72 additions and 9 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue