Some checks failed
CI / Build & Test (push) Failing after 40s
- kernel/plugin: 定义 Plugin 接口(Metadata/FxOption/SetupRoutes/Init) - kernel/plugin: 全局 Registry(Register/All/InitAll/Reset) - modules/example: 完整 4 层示例插件,实现 Plugin 接口 - config: 新增 PluginConfig(enabled 列表) - app.go: 集成插件系统 — fx.Invoke 注册 + plugin.All() 路由 + InitAll 生命周期
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package interfaces
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"mengstack/internal/kernel/response"
|
|
"mengstack/internal/modules/example/application"
|
|
"mengstack/internal/modules/example/domain"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Handler handles HTTP requests for the example plugin.
|
|
type Handler struct {
|
|
svc *application.Service
|
|
}
|
|
|
|
// NewHandler creates a new example handler.
|
|
func NewHandler(svc *application.Service) *Handler {
|
|
return &Handler{svc: svc}
|
|
}
|
|
|
|
// Create handles POST /api/v1/examples
|
|
func (h *Handler) Create(c *gin.Context) {
|
|
var req domain.CreateExampleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid request: "+err.Error()))
|
|
return
|
|
}
|
|
tenantID := c.GetString("tenant_id")
|
|
dto, err := h.svc.Create(c.Request.Context(), tenantID, req)
|
|
if err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, dto)
|
|
}
|
|
|
|
// Get handles GET /api/v1/examples/:id
|
|
func (h *Handler) Get(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid id"))
|
|
return
|
|
}
|
|
tenantID := c.GetString("tenant_id")
|
|
dto, err := h.svc.Get(c.Request.Context(), tenantID, uint(id))
|
|
if err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, dto)
|
|
}
|
|
|
|
// List handles GET /api/v1/examples
|
|
func (h *Handler) List(c *gin.Context) {
|
|
tenantID := c.GetString("tenant_id")
|
|
dtos, err := h.svc.List(c.Request.Context(), tenantID)
|
|
if err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, dtos)
|
|
}
|
|
|
|
// Delete handles DELETE /api/v1/examples/:id
|
|
func (h *Handler) Delete(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
response.Fail(c, response.NewBadRequest("invalid id"))
|
|
return
|
|
}
|
|
tenantID := c.GetString("tenant_id")
|
|
if err := h.svc.Delete(c.Request.Context(), tenantID, uint(id)); err != nil {
|
|
response.HandleError(c, err)
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|