diff --git a/internal/api/merch/controller.go b/internal/api/merch/controller.go index 0c8f458..a88b3fd 100644 --- a/internal/api/merch/controller.go +++ b/internal/api/merch/controller.go @@ -1,11 +1,77 @@ package merch +import ( + "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + "merch-parser-api/internal/interfaces" + "merch-parser-api/pkg/responses" + "net/http" +) + type controller struct { service *service + utils interfaces.Utils } -func newController(service *service) *controller { +func newController(service *service, utils interfaces.Utils) *controller { return &controller{ service: service, + utils: utils, } } + +func (h *Handler) RegisterRoutes(r *gin.RouterGroup, authMW gin.HandlerFunc, refreshMW gin.HandlerFunc) { + merchGroup := r.Group("/merch", authMW) + + merchGroup.POST("/", h.controller.addMerch) + merchGroup.GET("/", h.controller.getMerch) + merchGroup.PUT("/", h.controller.updateMerch) + merchGroup.DELETE("/", h.controller.deleteMerch) + +} + +// @Summary Добавить новый мерч +// @Description Добавить новый мерч +// @Tags Merch +// @Security BearerAuth +// @Accept json +// @Param body body MerchDTO true "новый мерч" +// @Success 200 +// @Failure 400 {object} responses.ErrorResponse400 +// @Failure 500 {object} responses.ErrorResponse500 +// @Router /merch [post] +func (co *controller) addMerch(c *gin.Context) { + var payload MerchDTO + if err := c.ShouldBind(&payload); err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()}) + log.WithError(err).Error("Merch | Failed to bind JSON on add merch") + return + } + + userUuid, err := co.utils.GetUserUuidFromContext(c) + if err != nil { + c.JSON(http.StatusInternalServerError, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error("Merch | Failed to get user uuid from context") + return + } + + err = co.service.addMerch(payload, userUuid) + if err != nil { + c.JSON(http.StatusBadRequest, responses.ErrorResponse500{Error: err.Error()}) + log.WithError(err).Error("Merch | Failed to add merch") + return + } + + c.Status(http.StatusOK) +} +func (co *controller) getMerch(c *gin.Context) { + c.JSON(200, gin.H{"msg": "placeholder"}) +} + +func (co *controller) updateMerch(c *gin.Context) { + c.JSON(200, gin.H{"msg": "placeholder"}) +} + +func (co *controller) deleteMerch(c *gin.Context) { + c.JSON(200, gin.H{"msg": "placeholder"}) +} diff --git a/internal/api/merch/dto.go b/internal/api/merch/dto.go index bac27e3..7b116ec 100644 --- a/internal/api/merch/dto.go +++ b/internal/api/merch/dto.go @@ -1,17 +1,17 @@ package merch type MerchDTO struct { - MerchUuid string `json:"merch_uuid"` - Name string `json:"name"` - OriginSurugaya OriginSurugaya `json:"origin_surugaya"` - OriginMandarake OriginMandarake `json:"origin_mandarake"` + MerchUuid string `json:"merch_uuid"` + Name string `json:"name"` + OriginSurugaya SurugayaDTO `json:"origin_surugaya"` + OriginMandarake MandarakeDTO `json:"origin_mandarake"` } -type OriginSurugaya struct { +type SurugayaDTO struct { Link string `json:"link"` CookieValues string `json:"cookie_values"` } -type OriginMandarake struct { +type MandarakeDTO struct { Link string `json:"link"` } diff --git a/internal/api/merch/handler.go b/internal/api/merch/handler.go index addd913..274ccdf 100644 --- a/internal/api/merch/handler.go +++ b/internal/api/merch/handler.go @@ -2,6 +2,7 @@ package merch import ( "gorm.io/gorm" + "merch-parser-api/internal/interfaces" ) type Handler struct { @@ -11,13 +12,14 @@ type Handler struct { } type Deps struct { - DB *gorm.DB + DB *gorm.DB + Utils interfaces.Utils } func NewHandler(deps Deps) *Handler { r := NewRepo(deps.DB) s := newService(r) - c := newController(s) + c := newController(s, deps.Utils) return &Handler{ repo: r, diff --git a/internal/api/merch/model.go b/internal/api/merch/model.go index 8b28ae8..207ce3f 100644 --- a/internal/api/merch/model.go +++ b/internal/api/merch/model.go @@ -1,19 +1,18 @@ package merch import ( - "github.com/google/uuid" + "database/sql" "time" ) type Merch struct { - Id uint `json:"id" gorm:"primary_key"` - CreatedAt time.Time `json:"created_at" gorm:"created_at"` - UpdatedAt time.Time `json:"updated_at" gorm:"updated_at"` - DeletedAt time.Time `json:"deleted_at" gorm:"deleted_at"` - MerchUuid string `json:"merch_uuid" gorm:"type:varchar(36);unique_index"` - UserUuid string `json:"user_uuid" gorm:"type:varchar(36)"` - Name string `json:"name" gorm:"column:name"` - Origin string `json:"origin" gorm:"column:origin"` + Id uint `json:"id" gorm:"primary_key"` + CreatedAt time.Time `json:"created_at" gorm:"created_at"` + UpdatedAt sql.NullTime `json:"updated_at" gorm:"updated_at"` + DeletedAt sql.NullTime `json:"deleted_at" gorm:"deleted_at"` + MerchUuid string `json:"merch_uuid" gorm:"type:varchar(36);unique_index"` + UserUuid string `json:"user_uuid" gorm:"type:varchar(36)"` + Name string `json:"name" gorm:"column:name"` } func (Merch) TableName() string { @@ -21,14 +20,11 @@ func (Merch) TableName() string { } type Surugaya struct { - Id uint `gorm:"primary_key" json:"-"` - DeletedAt time.Time `gorm:"index" json:"-"` - MerchUuid uuid.UUID `gorm:"index" json:"-"` - Link string `json:"link"` - ParseTag string `json:"parse_tag"` - ParseSubstring string `json:"parse_substring"` - CookieValues string `json:"cookie_values"` - Separator string `json:"separator"` + Id uint `gorm:"primary_key" json:"-"` + DeletedAt sql.NullTime `gorm:"index" json:"-"` + MerchUuid string `gorm:"index" json:"-"` + Link string `json:"link"` + CookieValues string `json:"cookie_values"` } func (Surugaya) TableName() string { @@ -36,10 +32,10 @@ func (Surugaya) TableName() string { } type Mandarake struct { - Id uint `gorm:"primary_key" json:"-"` - DeletedAt time.Time `gorm:"index" json:"-"` - MerchUuid uuid.UUID `gorm:"index" json:"-"` - Link string `json:"link"` + Id uint `gorm:"primary_key" json:"-"` + DeletedAt sql.NullTime `gorm:"index" json:"-"` + MerchUuid string `gorm:"index" json:"-"` + Link string `json:"link"` } func (Mandarake) TableName() string { diff --git a/internal/api/merch/repository.go b/internal/api/merch/repository.go index f19ec39..9a3e92e 100644 --- a/internal/api/merch/repository.go +++ b/internal/api/merch/repository.go @@ -12,4 +12,22 @@ func NewRepo(db *gorm.DB) *Repo { } } -type repository interface{} +type repository interface { + addMerch(merch Merch, surugaya Surugaya, mandarake Mandarake) error +} + +func (r *Repo) addMerch(merch Merch, surugaya Surugaya, mandarake Mandarake) error { + if err := r.db.Create(merch).Error; err != nil { + return err + } + + if err := r.db.Create(surugaya).Error; err != nil { + return err + } + + if err := r.db.Create(mandarake).Error; err != nil { + return err + } + + return nil +} diff --git a/internal/api/merch/service.go b/internal/api/merch/service.go index 55d8cf9..7f06939 100644 --- a/internal/api/merch/service.go +++ b/internal/api/merch/service.go @@ -1,5 +1,11 @@ package merch +import ( + "database/sql" + "github.com/google/uuid" + "time" +) + type service struct { repo repository } @@ -9,3 +15,42 @@ func newService(repo repository) *service { repo: repo, } } + +func (s *service) addMerch(payload MerchDTO, userUuid string) error { + merchUuid := uuid.NewString() + + merch := Merch{ + CreatedAt: time.Time{}, + UpdatedAt: sql.NullTime{Valid: false}, + DeletedAt: sql.NullTime{Valid: false}, + MerchUuid: merchUuid, + UserUuid: userUuid, + Name: payload.Name, + } + + surugaya := Surugaya{ + DeletedAt: sql.NullTime{}, + MerchUuid: merchUuid, + Link: payload.OriginSurugaya.Link, + CookieValues: payload.OriginSurugaya.CookieValues, + } + + mandarake := Mandarake{ + DeletedAt: sql.NullTime{}, + MerchUuid: merchUuid, + Link: payload.OriginMandarake.Link, + } + + return s.repo.addMerch(merch, surugaya, mandarake) +} +func (s *service) getMerch() error { + return nil +} + +func (s *service) updateMerch() error { + return nil +} + +func (s *service) deleteMerch() error { + return nil +}