84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package schema
|
|
|
|
import (
|
|
"github.com/google/uuid"
|
|
"gitlab.guxuan.icu/jinshan_community/pkg/util"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Defining the `Product` struct.
|
|
type Product struct {
|
|
util.BaseModel
|
|
CategoryID string `json:"categoryId" gorm:"type:char(36);index;comment:分类id"`
|
|
Title string `json:"title" gorm:"size:128;not null;index;comment:标题"`
|
|
Introduce string `json:"introduce" gorm:"size:1024;comment:介绍"`
|
|
Cover string `json:"cover" gorm:"size:2048;not null;comment:封面"`
|
|
Images *[]string `json:"images" gorm:"serializer:json;comment:详图数组"`
|
|
Content string `json:"content" gorm:"type:text;not null;comment:详情"`
|
|
Num int `json:"num" gorm:"not null;comment:数量"`
|
|
Price int `json:"price" gorm:"not null;comment:价值"`
|
|
ShopID string `json:"shopId" gorm:"type:char(36);index;not null;comment:店铺ID"`
|
|
Status string `json:"status" gorm:"size:20;index;comment:状态"`
|
|
}
|
|
|
|
func (c *Product) BeforeCreate(tx *gorm.DB) (err error) {
|
|
if c.ID == "" {
|
|
c.ID = uuid.New().String()
|
|
}
|
|
return
|
|
}
|
|
|
|
// Defining the query parameters for the `Product` struct.
|
|
type ProductQueryParam struct {
|
|
util.PaginationParam
|
|
}
|
|
|
|
// Defining the query options for the `Product` struct.
|
|
type ProductQueryOptions struct {
|
|
util.QueryOptions
|
|
}
|
|
|
|
// Defining the query result for the `Product` struct.
|
|
type ProductQueryResult struct {
|
|
Data Products
|
|
PageResult *util.PaginationResult
|
|
}
|
|
|
|
// Defining the slice of `Product` struct.
|
|
type Products []*Product
|
|
|
|
// Defining the data structure for creating a `Product` struct.
|
|
type ProductForm struct {
|
|
CategoryID string `json:"categoryId"`
|
|
Title string `json:"title" `
|
|
Introduce string `json:"introduce"`
|
|
Cover string `json:"cover"`
|
|
Images *[]string `json:"images" `
|
|
Content string `json:"content"`
|
|
Num int `json:"num" `
|
|
Price int `json:"price"`
|
|
ShopID string `json:"shopId" `
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// A validation function for the `ProductForm` struct.
|
|
func (a *ProductForm) Validate() error {
|
|
return nil
|
|
}
|
|
|
|
// Convert `ProductForm` to `Product` object.
|
|
func (a *ProductForm) FillTo(product *Product) error {
|
|
product.CategoryID = a.CategoryID
|
|
product.Title = a.Title
|
|
product.Introduce = a.Introduce
|
|
product.Cover = a.Cover
|
|
product.Images = a.Images
|
|
product.Content = a.Content
|
|
product.Num = a.Num
|
|
product.Price = a.Price
|
|
product.ShopID = a.ShopID
|
|
product.Status = a.Status
|
|
|
|
return nil
|
|
}
|