1
0

feat: 实现分层架构,包含 domain、service、repository 和 pkg 层

- 新增 domain 层:model、provider、route、stats 实体
- 新增 service 层:models、providers、routing、stats 业务逻辑
- 新增 repository 层:models、providers、stats 数据访问
- 新增 pkg 工具包:errors、logger、validator
- 新增中间件:CORS、logging、recovery、request ID
- 新增数据库迁移:初始 schema 和索引
- 新增单元测试和集成测试
- 新增规范文档:config-management、database-migration、error-handling、layered-architecture、middleware-system、request-validation、structured-logging、test-coverage
- 移除 config 子包和 model_router(已迁移至分层架构)
This commit is contained in:
2026-04-16 00:47:20 +08:00
parent 915b004924
commit f18904af1e
77 changed files with 5727 additions and 1257 deletions

View File

@@ -6,15 +6,20 @@ import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"nex/backend/internal/config"
appErrors "nex/backend/pkg/errors"
"nex/backend/internal/domain"
"nex/backend/internal/service"
)
// ModelHandler 模型管理处理器
type ModelHandler struct{}
type ModelHandler struct {
modelService service.ModelService
}
// NewModelHandler 创建模型处理器
func NewModelHandler() *ModelHandler {
return &ModelHandler{}
func NewModelHandler(modelService service.ModelService) *ModelHandler {
return &ModelHandler{modelService: modelService}
}
// CreateModel 创建模型
@@ -32,26 +37,21 @@ func (h *ModelHandler) CreateModel(c *gin.Context) {
return
}
// 创建模型对象
model := &config.Model{
model := &domain.Model{
ID: req.ID,
ProviderID: req.ProviderID,
ModelName: req.ModelName,
Enabled: true, // 默认启用
}
// 保存到数据库
err := config.CreateModel(model)
err := h.modelService.Create(model)
if err != nil {
if err.Error() == "供应商不存在" {
if err == appErrors.ErrProviderNotFound {
c.JSON(http.StatusBadRequest, gin.H{
"error": "供应商不存在",
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"error": "创建模型失败: " + err.Error(),
})
writeError(c, err)
return
}
@@ -62,11 +62,9 @@ func (h *ModelHandler) CreateModel(c *gin.Context) {
func (h *ModelHandler) ListModels(c *gin.Context) {
providerID := c.Query("provider_id")
models, err := config.ListModels(providerID)
models, err := h.modelService.List(providerID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "查询模型失败: " + err.Error(),
})
writeError(c, err)
return
}
@@ -77,7 +75,7 @@ func (h *ModelHandler) ListModels(c *gin.Context) {
func (h *ModelHandler) GetModel(c *gin.Context) {
id := c.Param("id")
model, err := config.GetModel(id)
model, err := h.modelService.Get(id)
if err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{
@@ -85,9 +83,7 @@ func (h *ModelHandler) GetModel(c *gin.Context) {
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"error": "查询模型失败: " + err.Error(),
})
writeError(c, err)
return
}
@@ -106,8 +102,7 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
return
}
// 更新模型
err := config.UpdateModel(id, req)
err := h.modelService.Update(id, req)
if err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{
@@ -115,24 +110,19 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
})
return
}
if err.Error() == "供应商不存在" {
if err == appErrors.ErrProviderNotFound {
c.JSON(http.StatusBadRequest, gin.H{
"error": "供应商不存在",
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"error": "更新模型失败: " + err.Error(),
})
writeError(c, err)
return
}
// 返回更新后的模型
model, err := config.GetModel(id)
model, err := h.modelService.Get(id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "查询更新后的模型失败: " + err.Error(),
})
writeError(c, err)
return
}
@@ -143,7 +133,7 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
func (h *ModelHandler) DeleteModel(c *gin.Context) {
id := c.Param("id")
err := config.DeleteModel(id)
err := h.modelService.Delete(id)
if err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{
@@ -151,9 +141,7 @@ func (h *ModelHandler) DeleteModel(c *gin.Context) {
})
return
}
c.JSON(http.StatusInternalServerError, gin.H{
"error": "删除模型失败: " + err.Error(),
})
writeError(c, err)
return
}