small fixes
This commit is contained in:
parent
844561ef70
commit
f7ec1bce1e
5 changed files with 40 additions and 32 deletions
|
|
@ -2,7 +2,6 @@ package merch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"merch-parser-api/internal/interfaces"
|
"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)
|
imagesGroup.DELETE("/:uuid", h.controller.deleteMerchImage)
|
||||||
|
|
||||||
labelsGroup := merchGroup.Group("/labels")
|
labelsGroup := merchGroup.Group("/labels")
|
||||||
labelsGroup.POST("/", h.controller.createLabel)
|
labelsGroup.POST("", h.controller.createLabel)
|
||||||
labelsGroup.GET("/", h.controller.getLabels)
|
labelsGroup.GET("", h.controller.getLabels)
|
||||||
labelsGroup.PUT("/:uuid", h.controller.updateLabel)
|
labelsGroup.PUT("/:uuid", h.controller.updateLabel)
|
||||||
labelsGroup.DELETE("/:uuid", h.controller.deleteLabel)
|
labelsGroup.DELETE("/:uuid", h.controller.deleteLabel)
|
||||||
labelsGroup.POST("/attach", h.controller.attachLabel)
|
labelsGroup.POST("/attach", h.controller.attachLabel)
|
||||||
|
|
@ -431,8 +430,8 @@ func (co *controller) deleteMerchImage(c *gin.Context) {
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Param payload body LabelDTO true "payload"
|
// @Param payload body LabelDTO true "payload"
|
||||||
// @Success 200
|
// @Success 200
|
||||||
// @Failure 400 {object} responses.ErrorResponse400
|
// @Failure 400 {object} responses.ErrorResponse400
|
||||||
// @Failure 500 {object} responses.ErrorResponse500
|
// @Failure 500 {object} responses.ErrorResponse500
|
||||||
// @Router /merch/labels [post]
|
// @Router /merch/labels [post]
|
||||||
func (co *controller) createLabel(c *gin.Context) {
|
func (co *controller) createLabel(c *gin.Context) {
|
||||||
const logMsg = "Merch | Create label"
|
const logMsg = "Merch | Create label"
|
||||||
|
|
@ -464,9 +463,9 @@ func (co *controller) createLabel(c *gin.Context) {
|
||||||
// @Description Получить все метки товаров
|
// @Description Получить все метки товаров
|
||||||
// @Tags Merch labels
|
// @Tags Merch labels
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Success 200 {array} LabelDTO
|
// @Success 200 {array} LabelsList
|
||||||
// @Failure 400 {object} responses.ErrorResponse400
|
// @Failure 400 {object} responses.ErrorResponse400
|
||||||
// @Failure 500 {object} responses.ErrorResponse500
|
// @Failure 500 {object} responses.ErrorResponse500
|
||||||
// @Router /merch/labels [get]
|
// @Router /merch/labels [get]
|
||||||
func (co *controller) getLabels(c *gin.Context) {
|
func (co *controller) getLabels(c *gin.Context) {
|
||||||
const logMsg = "Merch | Get labels"
|
const logMsg = "Merch | Get labels"
|
||||||
|
|
@ -492,7 +491,7 @@ func (co *controller) getLabels(c *gin.Context) {
|
||||||
// @Description Изменить метку
|
// @Description Изменить метку
|
||||||
// @Tags Merch labels
|
// @Tags Merch labels
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Param uuid path string true "label uuid"
|
// @Param uuid path string true "label uuid"
|
||||||
// @Param payload body LabelDTO true "payload"
|
// @Param payload body LabelDTO true "payload"
|
||||||
// @Success 200
|
// @Success 200
|
||||||
// @Failure 400 {object} responses.ErrorResponse400
|
// @Failure 400 {object} responses.ErrorResponse400
|
||||||
|
|
@ -522,14 +521,7 @@ func (co *controller) updateLabel(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if labelUuid != payload.LabelUuid {
|
if err = co.service.updateLabel(userUuid, labelUuid, payload); err != nil {
|
||||||
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 {
|
|
||||||
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()})
|
||||||
log.WithError(err).Error(logMsg)
|
log.WithError(err).Error(logMsg)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -60,10 +60,16 @@ type ImageLink struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type LabelDTO 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"`
|
LabelUuid string `json:"label_uuid"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color,omitempty"`
|
||||||
BgColor string `json:"bg_color"`
|
BgColor string `json:"bg_color,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LabelLink struct {
|
type LabelLink struct {
|
||||||
|
|
|
||||||
|
|
@ -63,8 +63,16 @@ type Label struct {
|
||||||
BgColor string `json:"bg_color" gorm:"column:bg_color"`
|
BgColor string `json:"bg_color" gorm:"column:bg_color"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (Label) TableName() string {
|
||||||
|
return "labels"
|
||||||
|
}
|
||||||
|
|
||||||
type CardLabel struct {
|
type CardLabel struct {
|
||||||
LabelUuid string `json:"label_uuid"`
|
LabelUuid string `json:"label_uuid"`
|
||||||
UserUuid string `json:"user_uuid"`
|
UserUuid string `json:"user_uuid"`
|
||||||
MerchUuid string `json:"merch_uuid"`
|
MerchUuid string `json:"merch_uuid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (CardLabel) TableName() string {
|
||||||
|
return "card_labels"
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ type prices interface {
|
||||||
type labels interface {
|
type labels interface {
|
||||||
createLabel(label Label) error
|
createLabel(label Label) error
|
||||||
getLabels(userUuid string) ([]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
|
deleteLabel(userUuid, labelUuid string) error
|
||||||
attachLabel(label CardLabel) error
|
attachLabel(label CardLabel) error
|
||||||
detachLabel(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 {
|
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) {
|
func (r *Repo) getLabels(userUuid string) ([]Label, error) {
|
||||||
var labels []Label
|
var labelsList []Label
|
||||||
|
|
||||||
if err := r.db.
|
if err := r.db.
|
||||||
|
Model(&Label{}).
|
||||||
Where("user_uuid = ?", userUuid).
|
Where("user_uuid = ?", userUuid).
|
||||||
Where("deleted_at IS NULL").
|
Where("deleted_at IS NULL").
|
||||||
Find(labels).Error; err != nil {
|
Find(&labelsList).Error; err != nil {
|
||||||
return nil, err
|
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{}).
|
return r.db.Model(&Label{}).
|
||||||
Where("user_uuid =? AND label_uuid = ?", userUuid, labelUuid).
|
Where("user_uuid =? AND label_uuid = ?", userUuid, labelUuid).
|
||||||
Updates(label).Error
|
Updates(label).Error
|
||||||
|
|
@ -284,6 +286,6 @@ func (r *Repo) attachLabel(label CardLabel) error {
|
||||||
|
|
||||||
func (r *Repo) detachLabel(label CardLabel) error {
|
func (r *Repo) detachLabel(label CardLabel) error {
|
||||||
return r.db.
|
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
|
Delete(&CardLabel{}).Error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -504,15 +504,15 @@ func (s *service) createLabel(label LabelDTO, userUuid string) error {
|
||||||
|
|
||||||
return s.repo.createLabel(newLabel)
|
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)
|
stored, err := s.repo.getLabels(userUuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
response := make([]LabelDTO, 0, len(stored))
|
response := make([]LabelsList, 0, len(stored))
|
||||||
for _, label := range stored {
|
for _, label := range stored {
|
||||||
response = append(response, LabelDTO{
|
response = append(response, LabelsList{
|
||||||
LabelUuid: label.LabelUuid,
|
LabelUuid: label.LabelUuid,
|
||||||
Name: label.Name,
|
Name: label.Name,
|
||||||
Color: label.Color,
|
Color: label.Color,
|
||||||
|
|
@ -522,8 +522,8 @@ func (s *service) getLabels(userUuid string) ([]LabelDTO, error) {
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
func (s *service) updateLabel(userUuid string, label LabelDTO) error {
|
func (s *service) updateLabel(userUuid, labelUuid string, label LabelDTO) error {
|
||||||
updateMap := make(map[string]string, 3)
|
updateMap := make(map[string]any, 3)
|
||||||
|
|
||||||
if label.Name != "" {
|
if label.Name != "" {
|
||||||
updateMap["name"] = label.Name
|
updateMap["name"] = label.Name
|
||||||
|
|
@ -537,7 +537,7 @@ func (s *service) updateLabel(userUuid string, label LabelDTO) error {
|
||||||
updateMap["bgcolor"] = label.BgColor
|
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 {
|
func (s *service) deleteLabel(userUuid, labelUuid string) error {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue