merch update method refactor
This commit is contained in:
parent
7dd4a6830e
commit
a4097706de
5 changed files with 44 additions and 32 deletions
|
|
@ -134,16 +134,16 @@ func (co *controller) getAllMerch(c *gin.Context) {
|
||||||
// @Description Обновить информацию про мерч по его uuid в json-е
|
// @Description Обновить информацию про мерч по его uuid в json-е
|
||||||
// @Tags Merch
|
// @Tags Merch
|
||||||
// @Security BearerAuth
|
// @Security BearerAuth
|
||||||
// @Param body body MerchDTO true "merch_uuid"
|
// @Param body body UpdateMerchDTO true "merch_uuid"
|
||||||
// @Success 200 {object} MerchDTO
|
// @Success 200
|
||||||
// @Failure 400 {object} responses.ErrorResponse400
|
// @Failure 400 {object} responses.ErrorResponse400
|
||||||
// @Failure 500 {object} responses.ErrorResponse500
|
// @Failure 500 {object} responses.ErrorResponse500
|
||||||
// @Router /merch/{uuid} [put]
|
// @Router /merch/ [put]
|
||||||
func (co *controller) updateMerch(c *gin.Context) {
|
func (co *controller) updateMerch(c *gin.Context) {
|
||||||
var payload MerchDTO
|
var payload UpdateMerchDTO
|
||||||
if err := c.ShouldBind(&payload); err != nil {
|
if err := c.ShouldBind(&payload); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
c.JSON(http.StatusBadRequest, responses.ErrorResponse400{Error: err.Error()})
|
||||||
log.WithError(err).Error("Merch | Failed to bind JSON on add merch")
|
log.WithError(err).Error("Merch | Failed to bind JSON on update merch")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,3 +46,10 @@ type PricesResponse struct {
|
||||||
MerchUuid string `json:"merch_uuid"`
|
MerchUuid string `json:"merch_uuid"`
|
||||||
Origins []OriginWithPrices `json:"origins"`
|
Origins []OriginWithPrices `json:"origins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateMerchDTO struct {
|
||||||
|
MerchUuid string `json:"merch_uuid"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Origin string `json:"origin"`
|
||||||
|
Link string `json:"link"`
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,10 @@ func (Merch) TableName() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Surugaya struct {
|
type Surugaya struct {
|
||||||
Id uint `gorm:"primary_key" json:"-"`
|
Id uint `gorm:"primary_key" json:"-"`
|
||||||
DeletedAt sql.NullTime `json:"-"`
|
DeletedAt sql.NullTime `json:"-"`
|
||||||
MerchUuid string `json:"-"`
|
MerchUuid string `json:"-"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
CookieValues string `json:"cookie_values"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Surugaya) TableName() string {
|
func (Surugaya) TableName() string {
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ type repository interface {
|
||||||
getSingleMerch(userUuid, merchUuid string) (merchBundle, error)
|
getSingleMerch(userUuid, merchUuid string) (merchBundle, error)
|
||||||
getAllMerch(userUuid string) ([]ListResponse, error)
|
getAllMerch(userUuid string) ([]ListResponse, error)
|
||||||
|
|
||||||
updateMerch(payload MerchDTO, userUuid string) error
|
updateMerch(payload UpdateMerchDTO, userUuid string) error
|
||||||
|
|
||||||
deleteMerch(userUuid, merchUuid string) error
|
deleteMerch(userUuid, merchUuid string) error
|
||||||
|
|
||||||
|
|
@ -99,7 +99,7 @@ func (r *Repo) getAllMerch(userUuid string) ([]ListResponse, error) {
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Repo) updateMerch(payload MerchDTO, userUuid string) error {
|
func (r *Repo) updateMerch(payload UpdateMerchDTO, userUuid string) error {
|
||||||
m := make(map[string]any)
|
m := make(map[string]any)
|
||||||
m["name"] = payload.Name
|
m["name"] = payload.Name
|
||||||
m["updated_at"] = sql.NullTime{
|
m["updated_at"] = sql.NullTime{
|
||||||
|
|
@ -115,27 +115,24 @@ func (r *Repo) updateMerch(payload MerchDTO, userUuid string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// surugaya
|
switch payload.Origin {
|
||||||
fields := make(map[string]any, 2)
|
case "surugaya":
|
||||||
if payload.OriginSurugaya.Link != "" {
|
var recordSurugaya Surugaya
|
||||||
fields["link"] = payload.OriginSurugaya.Link
|
err := r.db.Where("merch_uuid = ?", payload.MerchUuid).FirstOrCreate(&recordSurugaya, Surugaya{
|
||||||
}
|
MerchUuid: payload.MerchUuid,
|
||||||
|
Link: payload.Link,
|
||||||
if len(fields) > 0 {
|
}).Error
|
||||||
if err := r.db.
|
if err != nil {
|
||||||
Model(&Surugaya{}).
|
|
||||||
Where("merch_uuid = ?", payload.MerchUuid).
|
|
||||||
Updates(fields).Error; err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// mandarake
|
case "mandarake":
|
||||||
if payload.OriginMandarake.Link != "" {
|
var recordMandarake Mandarake
|
||||||
if err := r.db.
|
err := r.db.Where("merch_uuid = ?", payload.MerchUuid).FirstOrCreate(&recordMandarake, Mandarake{
|
||||||
Model(&Mandarake{}).
|
MerchUuid: payload.MerchUuid,
|
||||||
Where("merch_uuid = ?", payload.MerchUuid).
|
Link: payload.Link,
|
||||||
Update("link", payload.OriginMandarake.Link).Error; err != nil {
|
}).Error
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,10 +67,19 @@ func (s *service) getAllMerch(userUuid string) ([]ListResponse, error) {
|
||||||
return s.repo.getAllMerch(userUuid)
|
return s.repo.getAllMerch(userUuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) updateMerch(payload MerchDTO, userUuid string) error {
|
func (s *service) updateMerch(payload UpdateMerchDTO, userUuid string) error {
|
||||||
if payload.MerchUuid == "" {
|
if payload.MerchUuid == "" {
|
||||||
return errors.New("no MerchUuid or empty payload")
|
return errors.New("no merch uuid provided")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if payload.Origin == "" {
|
||||||
|
return errors.New("no origin provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
if payload.Link == "" {
|
||||||
|
return errors.New("no link provided")
|
||||||
|
}
|
||||||
|
|
||||||
return s.repo.updateMerch(payload, userUuid)
|
return s.repo.updateMerch(payload, userUuid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue