refactor product domain models

This commit is contained in:
david
2026-07-28 08:44:54 +08:00
parent 7000e90d88
commit 8d71a1fc08
15 changed files with 634 additions and 85 deletions

View File

@@ -27,7 +27,7 @@ func RegisterPlatform(serviceKey string, engine *gin.Engine) {
registerDeliveryRoute(protected)
registerStaffRoute(protected)
registerUserRoute(protected)
registerDeviceRoute(protected)
registerProductRoute(protected)
registerCommerceRoute(protected)
registerFinanceRoute(protected)
registerContentRoute(protected)
@@ -50,13 +50,41 @@ func registerDeliveryRoute(group *gin.RouterGroup) {
registerReadOnlyResource(group, "/delivery/delivery_track_point", &models.DeliveryTrackPoint{})
}
func registerDeviceRoute(group *gin.RouterGroup) {
registerRestrictedWritableResource(group, "/device/dev_smart_cylinder_valve", &models.DevSmartCylinderValve{}, []string{"device_no", "model", "online_status", "owner_identity"})
registerRestrictedWritableResource(group, "/device/dev_device_binding", &models.DevDeviceBinding{}, []string{"effective_at", "expired_at"}, requiredRelation("smart_cylinder_valve_identity", "smart_cylinder_valve_id", &models.DevSmartCylinderValve{}), requiredRelation("user_account_identity", "user_account_id", &models.UserAccount{}))
list, _, get, _ := platform.ResourceHandlers(&models.DevTelemetry{}, nil, nil)
telemetry := group.Group("/device/dev_telemetry")
telemetry.GET("", list)
telemetry.GET("/:identity", get)
func registerProductRoute(group *gin.RouterGroup) {
registerRestrictedNoDeleteResource(group, "/product_type", &models.ProductType{}, []string{"code", "name"})
registerRestrictedNoDeleteResource(group, "/product_warehouse", &models.ProductWarehouse{}, []string{"code", "name", "address", "manager", "phone", "is_enabled"})
infoRelations := []platform.ResourceRelation{
requiredRelation("product_type_identity", "product_type_id", &models.ProductType{}),
optionalRelation("warehouse_identity", "warehouse_id", &models.ProductWarehouse{}),
optionalRelation("gas_basic_identity", "gas_basic_id", &models.GasBasic{}),
optionalRelation("delivery_basic_identity", "delivery_basic_id", &models.DeliveryBasic{}),
optionalRelation("user_account_identity", "user_account_id", &models.UserAccount{}),
}
infoList, infoCreate, infoGet, infoUpdate := platform.ProductInfoHandlers(infoRelations...)
info := group.Group("/product_info")
info.GET("", infoList)
info.POST("", infoCreate)
info.GET("/:identity", infoGet)
info.PUT("/:identity", infoUpdate)
info.PATCH("/:identity/status", platform.UpdateProductInfoStatus)
repairList, repairCreate, repairGet, repairUpdate := platform.ProductRepairHandlers(
requiredRelation("product_info_identity", "product_info_id", &models.ProductInfo{}),
)
registerNoDeleteResource(group, "/product_repair", repairList, repairCreate, repairGet, repairUpdate, &models.ProductRepair{})
ownerList, ownerCreate, ownerGet := platform.ProductOwnerHandlers(
requiredRelation("product_info_identity", "product_info_id", &models.ProductInfo{}),
optionalRelation("warehouse_identity", "warehouse_id", &models.ProductWarehouse{}),
optionalRelation("gas_basic_identity", "gas_basic_id", &models.GasBasic{}),
optionalRelation("delivery_basic_identity", "delivery_basic_id", &models.DeliveryBasic{}),
optionalRelation("user_account_identity", "user_account_id", &models.UserAccount{}),
)
owner := group.Group("/product_owner")
owner.GET("", ownerList)
owner.POST("", ownerCreate)
owner.GET("/:identity", ownerGet)
}
func registerCommerceRoute(group *gin.RouterGroup) {
@@ -138,6 +166,20 @@ func registerRestrictedWritableResource(group *gin.RouterGroup, path string, mod
registerWritableResource(group, path, list, create, get, update, model)
}
func registerNoDeleteResource(group *gin.RouterGroup, path string, list, create, get, update gin.HandlerFunc, model any) {
resource := group.Group(path)
resource.GET("", list)
resource.POST("", create)
resource.GET("/:identity", get)
resource.PUT("/:identity", update)
resource.PATCH("/:identity/status", func(ctx *gin.Context) { platform.UpdateRecordStatus(ctx, model) })
}
func registerRestrictedNoDeleteResource(group *gin.RouterGroup, path string, model any, fields []string, relations ...platform.ResourceRelation) {
list, create, get, update := platform.ResourceHandlers(model, fields, fields, relations...)
registerNoDeleteResource(group, path, list, create, get, update, model)
}
func registerReadOnlyResource(group *gin.RouterGroup, path string, model any) {
list, _, get, _ := platform.ResourceHandlers(model, nil, nil)
resource := group.Group(path)

View File

@@ -29,6 +29,12 @@ func TestEveryContractHasRegisteredRoute(t *testing.T) {
case platform.AppendOnly:
assertRouteMethods(t, routes, path, http.MethodGet, http.MethodPost)
assertNoRouteMethods(t, routes, path, http.MethodPut, http.MethodPatch, http.MethodDelete)
case platform.Editable:
assertRouteMethods(t, routes, path, http.MethodGet, http.MethodPost)
assertRouteMethods(t, routes, path+"/:identity", http.MethodGet, http.MethodPut)
assertRouteMethods(t, routes, path+"/:identity/status", http.MethodPatch)
assertNoRouteMethods(t, routes, path, http.MethodDelete)
assertNoRouteMethods(t, routes, path+"/:identity", http.MethodDelete)
default:
assertRouteMethods(t, routes, path, http.MethodGet, http.MethodPost)
assertRouteMethods(t, routes, path+"/:identity", http.MethodGet, http.MethodPut, http.MethodDelete)
@@ -87,7 +93,7 @@ func TestPlatformOrganizationAndAccountRoutesExposeResourceCRUD(t *testing.T) {
assertRouteMethods(t, routes, "/heqi/platform/v1/platform/platform_role/:identity/menus", http.MethodPut)
}
func TestPlatformDeviceCommerceAndDeliveryRoutesFollowTheirContracts(t *testing.T) {
func TestPlatformProductCommerceAndDeliveryRoutesFollowTheirContracts(t *testing.T) {
engine := gin.New()
RegisterPlatform("heqi", engine)
@@ -100,7 +106,6 @@ func TestPlatformDeviceCommerceAndDeliveryRoutesFollowTheirContracts(t *testing.
}
for _, resource := range []string{
"/device/dev_smart_cylinder_valve", "/device/dev_device_binding",
"/ec/ec_category", "/ec/ec_product", "/ec/ec_product_attribute", "/ec/ec_product_image", "/ec/ec_cart", "/ec/ec_order", "/ec/ec_order_item", "/ec/ec_review",
"/delivery/delivery_task", "/delivery/delivery_track",
} {
@@ -116,14 +121,17 @@ func TestPlatformDeviceCommerceAndDeliveryRoutesFollowTheirContracts(t *testing.
assertNoRouteMethods(t, routes, trackPoint+"/:identity", http.MethodPut, http.MethodDelete)
assertNoRouteMethods(t, routes, trackPoint+"/:identity/status", http.MethodPatch)
telemetry := "/heqi/platform/v1/device/dev_telemetry"
assertRouteMethods(t, routes, telemetry, http.MethodGet)
assertRouteMethods(t, routes, telemetry+"/:identity", http.MethodGet)
for _, method := range []string{http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete} {
if routes[telemetry][method] || routes[telemetry+"/:identity"][method] {
t.Errorf("telemetry unexpectedly permits %s", method)
}
for _, resource := range []string{"/product_type", "/product_warehouse", "/product_info", "/product_repair"} {
path := "/heqi/platform/v1" + resource
assertRouteMethods(t, routes, path, http.MethodGet, http.MethodPost)
assertRouteMethods(t, routes, path+"/:identity", http.MethodGet, http.MethodPut)
assertRouteMethods(t, routes, path+"/:identity/status", http.MethodPatch)
assertNoRouteMethods(t, routes, path+"/:identity", http.MethodDelete)
}
owner := "/heqi/platform/v1/product_owner"
assertRouteMethods(t, routes, owner, http.MethodGet, http.MethodPost)
assertRouteMethods(t, routes, owner+"/:identity", http.MethodGet)
assertNoRouteMethods(t, routes, owner+"/:identity", http.MethodPut, http.MethodPatch, http.MethodDelete)
for _, resource := range []string{
"/safety/safe_rule", "/safety/safe_event", "/safety/safe_inspection",