diff --git a/docs/docs.go b/docs/docs.go index 0111bc4..8e556d2 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -279,254 +279,6 @@ const docTemplate = `{ } } }, - "/merch/labels": { - "get": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Получить все метки товаров", - "tags": [ - "Merch labels" - ], - "summary": "Получить все метки товаров", - "responses": { - "200": { - "description": "OK", - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/merch.LabelsList" - } - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - }, - "post": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Создать новую метку для товара", - "tags": [ - "Merch labels" - ], - "summary": "Создать новую метку для товара", - "parameters": [ - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelDTO" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, - "/merch/labels/attach": { - "post": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Прикрепить метку к товару", - "tags": [ - "Merch labels" - ], - "summary": "Прикрепить метку к товару", - "parameters": [ - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelLink" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, - "/merch/labels/detach": { - "post": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Удалить привязку метки к товару", - "tags": [ - "Merch labels" - ], - "summary": "Удалить привязку метки к товару", - "parameters": [ - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelLink" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, - "/merch/labels/{uuid}": { - "put": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Изменить метку", - "tags": [ - "Merch labels" - ], - "summary": "Изменить метку", - "parameters": [ - { - "type": "string", - "description": "label uuid", - "name": "uuid", - "in": "path", - "required": true - }, - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelDTO" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - }, - "delete": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Пометить метку как удаленную", - "tags": [ - "Merch labels" - ], - "summary": "Пометить метку как удаленную", - "parameters": [ - { - "type": "string", - "description": "label uuid", - "name": "uuid", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, "/merch/{uuid}": { "get": { "security": [ @@ -1029,48 +781,6 @@ const docTemplate = `{ } } }, - "merch.LabelDTO": { - "type": "object", - "properties": { - "bg_color": { - "type": "string" - }, - "color": { - "type": "string" - }, - "name": { - "type": "string" - } - } - }, - "merch.LabelLink": { - "type": "object", - "properties": { - "label_uuid": { - "type": "string" - }, - "merch_uuid": { - "type": "string" - } - } - }, - "merch.LabelsList": { - "type": "object", - "properties": { - "bg_color": { - "type": "string" - }, - "color": { - "type": "string" - }, - "label_uuid": { - "type": "string" - }, - "name": { - "type": "string" - } - } - }, "merch.ListResponse": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 7b17bd7..ea5dbf2 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -271,254 +271,6 @@ } } }, - "/merch/labels": { - "get": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Получить все метки товаров", - "tags": [ - "Merch labels" - ], - "summary": "Получить все метки товаров", - "responses": { - "200": { - "description": "OK", - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/merch.LabelsList" - } - } - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - }, - "post": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Создать новую метку для товара", - "tags": [ - "Merch labels" - ], - "summary": "Создать новую метку для товара", - "parameters": [ - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelDTO" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, - "/merch/labels/attach": { - "post": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Прикрепить метку к товару", - "tags": [ - "Merch labels" - ], - "summary": "Прикрепить метку к товару", - "parameters": [ - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelLink" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, - "/merch/labels/detach": { - "post": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Удалить привязку метки к товару", - "tags": [ - "Merch labels" - ], - "summary": "Удалить привязку метки к товару", - "parameters": [ - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelLink" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, - "/merch/labels/{uuid}": { - "put": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Изменить метку", - "tags": [ - "Merch labels" - ], - "summary": "Изменить метку", - "parameters": [ - { - "type": "string", - "description": "label uuid", - "name": "uuid", - "in": "path", - "required": true - }, - { - "description": "payload", - "name": "payload", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/merch.LabelDTO" - } - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - }, - "delete": { - "security": [ - { - "BearerAuth": [] - } - ], - "description": "Пометить метку как удаленную", - "tags": [ - "Merch labels" - ], - "summary": "Пометить метку как удаленную", - "parameters": [ - { - "type": "string", - "description": "label uuid", - "name": "uuid", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "OK" - }, - "400": { - "description": "Bad Request", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse400" - } - }, - "500": { - "description": "Internal Server Error", - "schema": { - "$ref": "#/definitions/responses.ErrorResponse500" - } - } - } - } - }, "/merch/{uuid}": { "get": { "security": [ @@ -1021,48 +773,6 @@ } } }, - "merch.LabelDTO": { - "type": "object", - "properties": { - "bg_color": { - "type": "string" - }, - "color": { - "type": "string" - }, - "name": { - "type": "string" - } - } - }, - "merch.LabelLink": { - "type": "object", - "properties": { - "label_uuid": { - "type": "string" - }, - "merch_uuid": { - "type": "string" - } - } - }, - "merch.LabelsList": { - "type": "object", - "properties": { - "bg_color": { - "type": "string" - }, - "color": { - "type": "string" - }, - "label_uuid": { - "type": "string" - }, - "name": { - "type": "string" - } - } - }, "merch.ListResponse": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 3dadf18..1afef07 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -14,33 +14,6 @@ definitions: link: type: string type: object - merch.LabelDTO: - properties: - bg_color: - type: string - color: - type: string - name: - type: string - type: object - merch.LabelLink: - properties: - label_uuid: - type: string - merch_uuid: - type: string - type: object - merch.LabelsList: - properties: - bg_color: - type: string - color: - type: string - label_uuid: - type: string - name: - type: string - type: object merch.ListResponse: properties: merch_uuid: @@ -395,161 +368,6 @@ paths: summary: Загрузить картинку по merch_uuid tags: - Merch images - /merch/labels: - get: - description: Получить все метки товаров - responses: - "200": - description: OK - schema: - items: - $ref: '#/definitions/merch.LabelsList' - type: array - "400": - description: Bad Request - schema: - $ref: '#/definitions/responses.ErrorResponse400' - "500": - description: Internal Server Error - schema: - $ref: '#/definitions/responses.ErrorResponse500' - security: - - BearerAuth: [] - summary: Получить все метки товаров - tags: - - Merch labels - post: - description: Создать новую метку для товара - parameters: - - description: payload - in: body - name: payload - required: true - schema: - $ref: '#/definitions/merch.LabelDTO' - responses: - "200": - description: OK - "400": - description: Bad Request - schema: - $ref: '#/definitions/responses.ErrorResponse400' - "500": - description: Internal Server Error - schema: - $ref: '#/definitions/responses.ErrorResponse500' - security: - - BearerAuth: [] - summary: Создать новую метку для товара - tags: - - Merch labels - /merch/labels/{uuid}: - delete: - description: Пометить метку как удаленную - parameters: - - description: label uuid - in: path - name: uuid - required: true - type: string - responses: - "200": - description: OK - "400": - description: Bad Request - schema: - $ref: '#/definitions/responses.ErrorResponse400' - "500": - description: Internal Server Error - schema: - $ref: '#/definitions/responses.ErrorResponse500' - security: - - BearerAuth: [] - summary: Пометить метку как удаленную - tags: - - Merch labels - put: - description: Изменить метку - parameters: - - description: label uuid - in: path - name: uuid - required: true - type: string - - description: payload - in: body - name: payload - required: true - schema: - $ref: '#/definitions/merch.LabelDTO' - responses: - "200": - description: OK - "400": - description: Bad Request - schema: - $ref: '#/definitions/responses.ErrorResponse400' - "500": - description: Internal Server Error - schema: - $ref: '#/definitions/responses.ErrorResponse500' - security: - - BearerAuth: [] - summary: Изменить метку - tags: - - Merch labels - /merch/labels/attach: - post: - description: Прикрепить метку к товару - parameters: - - description: payload - in: body - name: payload - required: true - schema: - $ref: '#/definitions/merch.LabelLink' - responses: - "200": - description: OK - "400": - description: Bad Request - schema: - $ref: '#/definitions/responses.ErrorResponse400' - "500": - description: Internal Server Error - schema: - $ref: '#/definitions/responses.ErrorResponse500' - security: - - BearerAuth: [] - summary: Прикрепить метку к товару - tags: - - Merch labels - /merch/labels/detach: - post: - description: Удалить привязку метки к товару - parameters: - - description: payload - in: body - name: payload - required: true - schema: - $ref: '#/definitions/merch.LabelLink' - responses: - "200": - description: OK - "400": - description: Bad Request - schema: - $ref: '#/definitions/responses.ErrorResponse400' - "500": - description: Internal Server Error - schema: - $ref: '#/definitions/responses.ErrorResponse500' - security: - - BearerAuth: [] - summary: Удалить привязку метки к товару - tags: - - Merch labels /prices: get: description: Получить цены мерча за период diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 7f0ae7a..ddf3bd6 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -2,6 +2,7 @@ package merch import ( "context" + "errors" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" "merch-parser-api/internal/interfaces" @@ -44,8 +45,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) @@ -430,8 +431,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" @@ -463,9 +464,9 @@ func (co *controller) createLabel(c *gin.Context) { // @Description Получить все метки товаров // @Tags Merch labels // @Security BearerAuth -// @Success 200 {array} LabelsList -// @Failure 400 {object} responses.ErrorResponse400 -// @Failure 500 {object} responses.ErrorResponse500 +// @Success 200 {array} LabelDTO +// @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" @@ -491,7 +492,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 @@ -521,7 +522,14 @@ func (co *controller) updateLabel(c *gin.Context) { return } - if err = co.service.updateLabel(userUuid, labelUuid, payload); err != nil { + 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 { 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 ad63c26..fc1b077 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -27,9 +27,8 @@ type SingleMerchResponse struct { } type ListResponse struct { - MerchUuid string `json:"merch_uuid"` - Name string `json:"name"` - Labels []string `json:"labels,omitempty" gorm:"-"` + MerchUuid string `json:"merch_uuid"` + Name string `json:"name"` } type PriceEntry struct { @@ -61,16 +60,10 @@ 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,omitempty"` - BgColor string `json:"bg_color,omitempty"` + Color string `json:"color"` + BgColor string `json:"bg_color"` } type LabelLink struct { diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index 935a194..088cb39 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -63,16 +63,8 @@ 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 d1567de..24ff87b 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -43,11 +43,10 @@ type prices interface { type labels interface { createLabel(label Label) error getLabels(userUuid string) ([]Label, error) - updateLabel(userUuid, labelUuid string, label map[string]any) error + updateLabel(userUuid, labelUuid string, label map[string]string) error deleteLabel(userUuid, labelUuid string) error attachLabel(label CardLabel) error detachLabel(label CardLabel) error - getAttachedLabelsByList(list []string) ([]CardLabel, error) } func (r *Repo) addMerch(bundle merchBundle) error { @@ -252,24 +251,22 @@ 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 labelsList []Label + var labels []Label if err := r.db. - Model(&Label{}). Where("user_uuid = ?", userUuid). Where("deleted_at IS NULL"). - Find(&labelsList).Error; err != nil { + Find(labels).Error; err != nil { return nil, err } - return labelsList, nil + return labels, nil } -func (r *Repo) updateLabel(userUuid, labelUuid string, label map[string]any) error { +func (r *Repo) updateLabel(userUuid, labelUuid string, label map[string]string) error { return r.db.Model(&Label{}). Where("user_uuid =? AND label_uuid = ?", userUuid, labelUuid). Updates(label).Error @@ -287,16 +284,6 @@ func (r *Repo) attachLabel(label CardLabel) error { func (r *Repo) detachLabel(label CardLabel) error { return r.db. - Where("user_uuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). + Where("userUuid = ? AND label_uuid = ? AND merch_uuid = ?", label.UserUuid, label.LabelUuid, label.MerchUuid). Delete(&CardLabel{}).Error } - -func (r *Repo) getAttachedLabelsByList(list []string) ([]CardLabel, error) { - var labelsList []CardLabel - - if err := r.db.Model(&CardLabel{}).Where("merch_uuid IN ?", list).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..bf49893 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -101,34 +101,7 @@ func (s *service) getSingleMerch(userUuid, merchUuid string) (MerchDTO, error) { } func (s *service) getAllMerch(userUuid string) ([]ListResponse, error) { - const logMsg = "Merch service | Get all merch" - - allMerch, err := s.repo.getAllMerch(userUuid) - if err != nil { - return nil, err - } - - ids := make([]string, 0, len(allMerch)) - for _, m := range allMerch { - ids = append(ids, m.MerchUuid) - } - - cardLabels, err := s.repo.getAttachedLabelsByList(ids) - if err != nil { - return nil, err - } - log.WithField("content", cardLabels).Debug(logMsg) - - clMap := make(map[string][]string) - for _, cl := range cardLabels { - clMap[cl.MerchUuid] = append(clMap[cl.MerchUuid], cl.LabelUuid) - } - - for item := range allMerch { - allMerch[item].Labels = clMap[allMerch[item].MerchUuid] - } - - return allMerch, nil + return s.repo.getAllMerch(userUuid) } func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error { @@ -531,15 +504,15 @@ func (s *service) createLabel(label LabelDTO, userUuid string) error { return s.repo.createLabel(newLabel) } -func (s *service) getLabels(userUuid string) ([]LabelsList, error) { +func (s *service) getLabels(userUuid string) ([]LabelDTO, error) { stored, err := s.repo.getLabels(userUuid) if err != nil { return nil, err } - response := make([]LabelsList, 0, len(stored)) + response := make([]LabelDTO, 0, len(stored)) for _, label := range stored { - response = append(response, LabelsList{ + response = append(response, LabelDTO{ LabelUuid: label.LabelUuid, Name: label.Name, Color: label.Color, @@ -549,8 +522,8 @@ func (s *service) getLabels(userUuid string) ([]LabelsList, error) { return response, nil } -func (s *service) updateLabel(userUuid, labelUuid string, label LabelDTO) error { - updateMap := make(map[string]any, 3) +func (s *service) updateLabel(userUuid string, label LabelDTO) error { + updateMap := make(map[string]string, 3) if label.Name != "" { updateMap["name"] = label.Name @@ -564,7 +537,7 @@ func (s *service) updateLabel(userUuid, labelUuid string, label LabelDTO) error updateMap["bgcolor"] = label.BgColor } - return s.repo.updateLabel(userUuid, labelUuid, updateMap) + return s.repo.updateLabel(userUuid, label.LabelUuid, updateMap) } func (s *service) deleteLabel(userUuid, labelUuid string) error { diff --git a/migrations.sql b/migrations.sql index b5fd81c..bb270bc 100644 --- a/migrations.sql +++ b/migrations.sql @@ -69,7 +69,7 @@ CREATE TABLE labels( bg_color VARCHAR(32) ); -CREATE TABLE card_labels ( +CREATE TABLE card_label ( user_uuid VARCHAR(36) NOT NULL, label_uuid VARCHAR(36) NOT NULL, merch_uuid VARCHAR(36) NOT NULL