Browse Source

新增/product/create接口

Ohio-HYF 3 years ago
parent
commit
817bd38518

+ 24 - 3
db/product.go

@@ -1,7 +1,28 @@
 package db
 package db
 
 
-import "context"
-
-func CreateProduct(ctx context.Context, newProduct string) {
+import (
+	"context"
+	"youngee_b_api/model/gorm_model"
+	"youngee_b_api/model/http_model"
+)
 
 
+func CreateProduct(ctx context.Context, newProduct http_model.CreateProductRequest) (*http_model.CreateProductData, error) {
+	db := GetReadDB(ctx)
+	Product := gorm_model.YounggeeProduct{
+		ProductName:  newProduct.ProductName,
+		ProductType:  newProduct.ProductType,
+		ShopAddress:  newProduct.ShopAddress,
+		ProductPrice: newProduct.ProductPrice,
+		ProductUrl:   newProduct.ProductUrl,
+		EnterpriseID: newProduct.EnterpriseID,
+		BrandName:    newProduct.BrandName,
+	}
+	err := db.Create(&Product).Error
+	if err != nil {
+		return nil, err
+	}
+	data := &http_model.CreateProductData{
+		ProductID: Product.ProductID,
+	}
+	return data, nil
 }
 }

+ 25 - 0
db/product_photo.go

@@ -0,0 +1,25 @@
+package db
+
+import (
+	"context"
+	"youngee_b_api/model/gorm_model"
+	"youngee_b_api/model/http_model"
+)
+
+func CreateProductPhoto(ctx context.Context, productID int, newProductPhotos []http_model.CreateProductPhoto) error {
+	db := GetReadDB(ctx)
+	productPhotos := []gorm_model.YounggeeProductPhoto{}
+	for _, newPhoto := range newProductPhotos {
+		productPhoto := gorm_model.YounggeeProductPhoto{
+			PhotoUrl:  newPhoto.PhotoUrl,
+			Symbol:    newPhoto.Symbol,
+			ProductID: productID,
+		}
+		productPhotos = append(productPhotos, productPhoto)
+	}
+	err := db.Create(&productPhotos).Error
+	if err != nil {
+		return err
+	}
+	return nil
+}

+ 1 - 1
handler/enterprice_create.go

@@ -8,7 +8,7 @@ import (
 	"github.com/gin-gonic/gin"
 	"github.com/gin-gonic/gin"
 )
 )
 
 
-func WrapCreateEnterpriceHandler(ctx *gin.Context) {
+func WrapCreateEnterpriseHandler(ctx *gin.Context) {
 	handler := newCreateEnterpriseHandler(ctx)
 	handler := newCreateEnterpriseHandler(ctx)
 	baseRun(handler)
 	baseRun(handler)
 }
 }

+ 52 - 0
handler/product_create.go

@@ -0,0 +1,52 @@
+package handler
+
+import (
+	"fmt"
+	"youngee_b_api/model/http_model"
+	"youngee_b_api/service"
+
+	"github.com/gin-gonic/gin"
+)
+
+func WrapCreateProductHandler(ctx *gin.Context) {
+	handler := newCreateProductHandler(ctx)
+	baseRun(handler)
+}
+
+func newCreateProductHandler(ctx *gin.Context) *CreateProductHandler {
+	return &CreateProductHandler{
+		req:  http_model.NewCreateProductRequest(),
+		resp: http_model.NewCreateProductResponse(),
+		ctx:  ctx,
+	}
+}
+
+type CreateProductHandler struct {
+	req  *http_model.CreateProductRequest
+	resp *http_model.CommonResponse
+	ctx  *gin.Context
+}
+
+func (h *CreateProductHandler) getRequest() interface{} {
+	return h.req
+}
+func (h *CreateProductHandler) getContext() *gin.Context {
+	return h.ctx
+}
+func (h *CreateProductHandler) getResponse() interface{} {
+	return h.resp
+}
+func (h *CreateProductHandler) run() {
+	data := http_model.CreateProductRequest{}
+	data = *h.req
+	res := service.CreateProduct.CreateProduct(h.ctx, data)
+	if res != nil {
+		fmt.Printf("%+v\n", res)
+	} else {
+		fmt.Printf("error\n")
+	}
+	h.resp.Data = res
+}
+func (h *CreateProductHandler) checkParam() error {
+	return nil
+}

+ 12 - 11
model/gorm_model/product.go

@@ -1,3 +1,4 @@
+// Code generated by sql2gorm. DO NOT EDIT.
 package gorm_model
 package gorm_model
 
 
 import (
 import (
@@ -5,18 +6,18 @@ import (
 )
 )
 
 
 type YounggeeProduct struct {
 type YounggeeProduct struct {
-	ProductId    int       `gorm:"column:product_id;type:int(11);primary_key" json:"product_id"` // 商品id
-	ProductName  string    `gorm:"column:product_name;type:varchar(50)" json:"product_name"`     // 商品名称
-	ProductType  int       `gorm:"column:product_type;type:tinyint(4)" json:"product_type"`      // 商品类型
-	ShopAddress  string    `gorm:"column:shop_address;type:varchar(255)" json:"shop_address"`    // 店铺地址,商品类型为线下品牌时需填写
-	ProductPrice int64     `gorm:"column:product_price;type:bigint(20)" json:"product_price"`    // 商品价值
-	ProductUrl   string    `gorm:"column:product_url;type:varchar(1000)" json:"product_url"`     // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
-	EnterpriseId int       `gorm:"column:enterprise_id;type:int(11)" json:"enterprise_id"`       // 所属企业id
-	CreatedAt    time.Time `gorm:"column:created_at;type:datetime;" json:"created_at"`           // 创建时间
-	UpdatedAt    time.Time `gorm:"column:updated_at;type:datetime" json:"updated_at"`            // 更新时间
-	BrandName    string    `gorm:"column:brand_name;type:varchar(50)" json:"brand_name"`
+	ProductID    int       `gorm:"column:product_id;primary_key"` // 商品id
+	ProductName  string    `gorm:"column:product_name"`           // 商品名称
+	ProductType  int       `gorm:"column:product_type"`           // 商品类型
+	ShopAddress  string    `gorm:"column:shop_address"`           // 店铺地址,商品类型为线下品牌时需填写
+	ProductPrice int64     `gorm:"column:product_price"`          // 商品价值
+	ProductUrl   string    `gorm:"column:product_url"`            // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
+	EnterpriseID int       `gorm:"column:enterprise_id"`          // 所属企业id
+	CreatedAt    time.Time `gorm:"column:created_at"`             // 创建时间
+	UpdatedAt    time.Time `gorm:"column:updated_at"`             // 更新时间
+	BrandName    string    `gorm:"column:brand_name"`
 }
 }
 
 
 func (m *YounggeeProduct) TableName() string {
 func (m *YounggeeProduct) TableName() string {
 	return "younggee_product"
 	return "younggee_product"
-}
+}

+ 7 - 5
model/gorm_model/product_photo.go

@@ -1,3 +1,4 @@
+// Code generated by sql2gorm. DO NOT EDIT.
 package gorm_model
 package gorm_model
 
 
 import (
 import (
@@ -5,13 +6,14 @@ import (
 )
 )
 
 
 type YounggeeProductPhoto struct {
 type YounggeeProductPhoto struct {
-	ProductPhotoId int       `gorm:"column:product_photo_id;type:int(11);primary_key" json:"product_photo_id"` // 商品图片id
-	PhotoUrl       string    `gorm:"column:photo_url;type:varchar(1000)" json:"photo_url"`                     // 图片或视频url
-	Symbol         int       `gorm:"column:symbol;type:tinyint(4)" json:"symbol"`                              // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
-	ProductId      int       `gorm:"column:product_id;type:int(11)" json:"product_id"`                         // 所属商品id
-	CreatedAt      time.Time `gorm:"column:created_at;type:datetime" json:"created_at"`                        // 创建时间
+	ProductPhotoID int       `gorm:"column:product_photo_id;primary_key"` // 商品图片id
+	PhotoUrl       string    `gorm:"column:photo_url"`                    // 图片或视频url
+	Symbol         int       `gorm:"column:symbol"`                       // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
+	ProductID      int       `gorm:"column:product_id"`                   // 所属商品id
+	CreatedAt      time.Time `gorm:"column:created_at"`                   // 创建时间
 }
 }
 
 
 func (m *YounggeeProductPhoto) TableName() string {
 func (m *YounggeeProductPhoto) TableName() string {
 	return "younggee_product_photo"
 	return "younggee_product_photo"
 }
 }
+

+ 30 - 0
model/http_model/product_create.go

@@ -0,0 +1,30 @@
+package http_model
+
+type CreateProductPhoto struct {
+	PhotoUrl string `json:"photo_url"` // 图片或视频url
+	Symbol   int    `json:"symbol"`    // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
+}
+
+type CreateProductRequest struct {
+	ProductName   string               `json:"product_name"`   // 商品名称
+	ProductType   int                  `json:"product_type"`   // 商品类型
+	ShopAddress   string               `json:"shop_address"`   // 店铺地址,商品类型为线下品牌时需填写
+	ProductPrice  int64                `json:"product_price"`  // 商品价值
+	ProductPhotos []CreateProductPhoto `json:"product_photos"` // 商品图片列表
+	ProductUrl    string               `json:"product_url"`    // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
+	EnterpriseID  int                  `json:"enterprise_id"`  // 所属企业id
+	BrandName     string               `json:"brand_name"`
+}
+
+type CreateProductData struct {
+	ProductID int `json:"product_id"` // 商品id
+}
+
+func NewCreateProductRequest() *CreateProductRequest {
+	return new(CreateProductRequest)
+}
+func NewCreateProductResponse() *CommonResponse {
+	resp := new(CommonResponse)
+	resp.Data = new(CreateProductData)
+	return resp
+}

+ 2 - 1
route/init.go

@@ -10,7 +10,8 @@ import (
 
 
 func InitRoute(r *gin.Engine) {
 func InitRoute(r *gin.Engine) {
 
 
-	r.POST("/enterprise/create", handler.WrapCreateEnterpriceHandler)
+	r.POST("/enterprise/create", handler.WrapCreateEnterpriseHandler)
+	r.POST("/product/create", handler.WrapCreateProductHandler)
 	r.POST("/login", handler.WrapPasswordLoginHandler)
 	r.POST("/login", handler.WrapPasswordLoginHandler)
 	r.GET("/test/ping", func(c *gin.Context) {
 	r.GET("/test/ping", func(c *gin.Context) {
 		resp := http_model.CommonResponse{
 		resp := http_model.CommonResponse{

+ 25 - 0
service/product_create.go

@@ -0,0 +1,25 @@
+package service
+
+import (
+	"context"
+	"youngee_b_api/db"
+	"youngee_b_api/model/http_model"
+)
+
+var CreateProduct *createProduct
+
+type createProduct struct {
+}
+
+func (*createProduct) CreateProduct(ctx context.Context, newProduct http_model.CreateProductRequest) *http_model.CreateProductData {
+	data, err := db.CreateProduct(ctx, newProduct)
+	if err != nil {
+		return nil
+	} else {
+		err := db.CreateProductPhoto(ctx, data.ProductID, newProduct.ProductPhotos)
+		if err != nil {
+			return nil
+		}
+	}
+	return data
+}