From 844561ef70ac62c9a952d7ecda7b5592a4e0e362 Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 20:28:40 +0300 Subject: [PATCH 1/4] update --- migrations.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations.sql b/migrations.sql index bb270bc..b5fd81c 100644 --- a/migrations.sql +++ b/migrations.sql @@ -69,7 +69,7 @@ CREATE TABLE labels( bg_color VARCHAR(32) ); -CREATE TABLE card_label ( +CREATE TABLE card_labels ( user_uuid VARCHAR(36) NOT NULL, label_uuid VARCHAR(36) NOT NULL, merch_uuid VARCHAR(36) NOT NULL From f7ec1bce1e38563e20a9cf5b552c405689894b56 Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 20:29:14 +0300 Subject: [PATCH 2/4] small fixes --- internal/api/merch/controller.go | 26 +++++++++----------------- internal/api/merch/dto.go | 10 ++++++++-- internal/api/merch/model.go | 8 ++++++++ internal/api/merch/repository.go | 16 +++++++++------- internal/api/merch/service.go | 12 ++++++------ 5 files changed, 40 insertions(+), 32 deletions(-) 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 { From 565e019a67004ff2324585adf71df928741dd51d Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 20:30:05 +0300 Subject: [PATCH 3/4] swagger docs update --- docs/docs.go | 290 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 290 ++++++++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 182 +++++++++++++++++++++++++++++ 3 files changed, 762 insertions(+) diff --git a/docs/docs.go b/docs/docs.go index 8e556d2..0111bc4 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -279,6 +279,254 @@ 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": [ @@ -781,6 +1029,48 @@ 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 ea5dbf2..7b17bd7 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -271,6 +271,254 @@ } } }, + "/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": [ @@ -773,6 +1021,48 @@ } } }, + "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 1afef07..3dadf18 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -14,6 +14,33 @@ 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: @@ -368,6 +395,161 @@ 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: Получить цены мерча за период From 8ac753f63293092318f4967cfef813eb9f94f93d Mon Sep 17 00:00:00 2001 From: nquidox Date: Tue, 28 Oct 2025 21:46:40 +0300 Subject: [PATCH 4/4] labels added to dto --- internal/api/merch/dto.go | 5 +++-- internal/api/merch/repository.go | 11 +++++++++++ internal/api/merch/service.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index 32e72e4..ad63c26 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -27,8 +27,9 @@ type SingleMerchResponse struct { } type ListResponse struct { - MerchUuid string `json:"merch_uuid"` - Name string `json:"name"` + MerchUuid string `json:"merch_uuid"` + Name string `json:"name"` + Labels []string `json:"labels,omitempty" gorm:"-"` } type PriceEntry struct { diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index 5b3c308..d1567de 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -47,6 +47,7 @@ type labels interface { 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 { @@ -289,3 +290,13 @@ func (r *Repo) detachLabel(label CardLabel) error { Where("user_uuid = ? 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 c6902ff..1632329 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -101,7 +101,34 @@ func (s *service) getSingleMerch(userUuid, merchUuid string) (MerchDTO, error) { } func (s *service) getAllMerch(userUuid string) ([]ListResponse, error) { - return s.repo.getAllMerch(userUuid) + 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 } func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error {