74 lines
1.9 KiB
Go
74 lines
1.9 KiB
Go
package product
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitlab.guxuan.icu/jinshan_community/internal/config"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/product/api"
|
|
"gitlab.guxuan.icu/jinshan_community/internal/mods/product/schema"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Product struct {
|
|
DB *gorm.DB
|
|
ProductAPI *api.Product
|
|
ProductCategoryAPI *api.ProductCategory
|
|
OrderAPI *api.Order
|
|
ShopAPI *api.Shop
|
|
}
|
|
|
|
func (a *Product) AutoMigrate(ctx context.Context) error {
|
|
return a.DB.AutoMigrate(new(schema.Product), new(schema.ProductCategory), new(schema.Order), new(schema.Shop))
|
|
}
|
|
|
|
func (a *Product) Init(ctx context.Context) error {
|
|
if config.C.Storage.DB.AutoMigrate {
|
|
if err := a.AutoMigrate(ctx); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *Product) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
|
|
product := v1.Group("products")
|
|
{
|
|
product.GET("", a.ProductAPI.Query)
|
|
product.GET(":id", a.ProductAPI.Get)
|
|
product.POST("", a.ProductAPI.Create)
|
|
product.PUT(":id", a.ProductAPI.Update)
|
|
product.DELETE(":id", a.ProductAPI.Delete)
|
|
}
|
|
productCategory := v1.Group("product-categories")
|
|
{
|
|
productCategory.GET("", a.ProductCategoryAPI.Query)
|
|
productCategory.GET(":id", a.ProductCategoryAPI.Get)
|
|
productCategory.POST("", a.ProductCategoryAPI.Create)
|
|
productCategory.PUT(":id", a.ProductCategoryAPI.Update)
|
|
productCategory.DELETE(":id", a.ProductCategoryAPI.Delete)
|
|
}
|
|
order := v1.Group("orders")
|
|
{
|
|
order.GET("", a.OrderAPI.Query)
|
|
order.GET(":id", a.OrderAPI.Get)
|
|
order.POST("", a.OrderAPI.Create)
|
|
order.PUT(":id", a.OrderAPI.Update)
|
|
order.DELETE(":id", a.OrderAPI.Delete)
|
|
}
|
|
shop := v1.Group("shops")
|
|
{
|
|
shop.GET("", a.ShopAPI.Query)
|
|
shop.GET(":id", a.ShopAPI.Get)
|
|
shop.POST("", a.ShopAPI.Create)
|
|
shop.PUT(":id", a.ShopAPI.Update)
|
|
shop.DELETE(":id", a.ShopAPI.Delete)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *Product) Release(ctx context.Context) error {
|
|
return nil
|
|
}
|