refactor platform logic and add gasorder domain
This commit is contained in:
101
backend/api/internal/logic/platform/resource_contract.go
Normal file
101
backend/api/internal/logic/platform/resource_contract.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ResourceMode describes the operations a platform-admin resource supports.
|
||||
type ResourceMode string
|
||||
|
||||
const (
|
||||
Writable ResourceMode = "writable"
|
||||
ReadOnly ResourceMode = "readonly"
|
||||
AppendOnly ResourceMode = "append_only"
|
||||
Editable ResourceMode = "editable"
|
||||
Managed ResourceMode = "managed"
|
||||
)
|
||||
|
||||
// ResourceContract is the expected cross-layer representation of one resource.
|
||||
type ResourceContract struct {
|
||||
Domain string
|
||||
Name string
|
||||
Path string
|
||||
PageKind string
|
||||
Mode ResourceMode
|
||||
}
|
||||
|
||||
// ResourceDefinition describes a route resource and the fields it may change.
|
||||
type ResourceDefinition struct {
|
||||
Domain string
|
||||
Name string
|
||||
Model func() any
|
||||
Mode ResourceMode
|
||||
CreateFields []string
|
||||
UpdateFields []string
|
||||
}
|
||||
|
||||
// Allows reports whether a resource mode supports an HTTP method.
|
||||
func (d ResourceDefinition) Allows(method string) bool {
|
||||
switch d.Mode {
|
||||
case ReadOnly:
|
||||
return method == http.MethodGet
|
||||
case AppendOnly:
|
||||
return method == http.MethodGet || method == http.MethodPost
|
||||
case Editable:
|
||||
return method == http.MethodGet || method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch
|
||||
case Managed:
|
||||
return method == http.MethodGet || method == http.MethodPost || method == http.MethodPut
|
||||
case Writable:
|
||||
return method == http.MethodGet || method == http.MethodPost || method == http.MethodPut || method == http.MethodPatch || method == http.MethodDelete
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func archiveValues() gin.H {
|
||||
return gin.H{"status": "archived"}
|
||||
}
|
||||
|
||||
func filterFields(values map[string]any, allowedFields []string) gin.H {
|
||||
allowed := make(map[string]struct{}, len(allowedFields))
|
||||
for _, field := range allowedFields {
|
||||
allowed[field] = struct{}{}
|
||||
}
|
||||
|
||||
filtered := make(gin.H, len(allowed))
|
||||
for field, value := range values {
|
||||
if _, ok := allowed[field]; ok {
|
||||
filtered[field] = value
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// ExpectedResources returns the complete platform-admin resource catalogue.
|
||||
func ExpectedResources() []ResourceContract {
|
||||
return []ResourceContract{
|
||||
resourceContract("gas", "gas_basic", Writable, "list"), resourceContract("gas", "gas_account", Writable, "list"),
|
||||
resourceContract("delivery", "delivery_basic", Writable, "list"), resourceContract("delivery", "delivery_account", Writable, "list"),
|
||||
resourceContract("staff", "staff_account", Writable, "list"), resourceContract("staff", "staff_credential", Writable, "list"),
|
||||
resourceContract("user", "user_account", Writable, "list"), resourceContract("user", "user_address", Writable, "list"), resourceContract("user", "user_service_relation", Writable, "list"),
|
||||
resourceContract("product", "product_type", Editable, "list"), resourceContract("product", "product_warehouse", Editable, "list"), resourceContract("product", "product_info", Editable, "list"), resourceContract("product", "product_repair", Editable, "list"), resourceContract("product", "product_owner", AppendOnly, "list"),
|
||||
resourceContract("ec", "ec_category", Writable, "list"), resourceContract("ec", "ec_product", Writable, "list"), resourceContract("ec", "ec_product_attribute", Writable, "list"), resourceContract("ec", "ec_product_image", Writable, "list"), resourceContract("ec", "ec_cart", Writable, "list"), resourceContract("ec", "ec_order", Writable, "list"), resourceContract("ec", "ec_order_item", Writable, "list"), resourceContract("ec", "ec_review", Writable, "list"),
|
||||
resourceContract("gasorder", "gasorder_contract", Managed, "list"), resourceContract("gasorder", "gasorder_contract_product", AppendOnly, "list"), resourceContract("gasorder", "gasorder_contract_revision", ReadOnly, "list"),
|
||||
resourceContract("gasorder", "gasorder_basic", AppendOnly, "list"), resourceContract("gasorder", "gasorder_item", ReadOnly, "list"), resourceContract("gasorder", "gasorder_assign", ReadOnly, "list"), resourceContract("gasorder", "gasorder_status", ReadOnly, "list"),
|
||||
resourceContract("gasorder", "gasorder_track", ReadOnly, "list"), resourceContract("gasorder", "gasorder_track_point", ReadOnly, "list"), resourceContract("gasorder", "gasorder_confirm", ReadOnly, "list"), resourceContract("gasorder", "gasorder_payment", ReadOnly, "list"),
|
||||
resourceContract("finance", "fin_payment", Writable, "list"), resourceContract("finance", "fin_settlement", Writable, "list"), resourceContract("finance", "fin_reconciliation", Writable, "list"),
|
||||
resourceContract("content", "cms_content", Writable, "list"), resourceContract("customer_service", "cs_ticket", Writable, "list"),
|
||||
resourceContract("platform", "platfrom_account", Writable, "list"), resourceContract("platform", "platform_role", Writable, "list"), resourceContract("platform", "platform_menu", Writable, "tree"),
|
||||
resourceContract("wallet", "wallet_basic", ReadOnly, "list"), resourceContract("wallet", "wallet_bank", ReadOnly, "list"), resourceContract("wallet", "wallet_payment", ReadOnly, "list"), resourceContract("wallet", "wallet_record", ReadOnly, "list"), resourceContract("wallet", "wallet_refund", ReadOnly, "list"), resourceContract("wallet", "wallet_apply_cash", ReadOnly, "list"),
|
||||
}
|
||||
}
|
||||
|
||||
func resourceContract(domain, name string, mode ResourceMode, pageKind string) ResourceContract {
|
||||
return ResourceContract{Domain: domain, Name: name, Path: resourcePath(domain, name), Mode: mode, PageKind: pageKind}
|
||||
}
|
||||
|
||||
func resourcePath(domain, name string) string {
|
||||
return "/" + name
|
||||
}
|
||||
Reference in New Issue
Block a user