用DDD分项目模块

This commit is contained in:
2025-10-05 01:46:48 +08:00
parent 48461b9c49
commit 28acc5104c
41 changed files with 1138 additions and 17 deletions

96
server/api/article.api Normal file
View File

@@ -0,0 +1,96 @@
// 定义文章相关的结构体(与数据表字段映射)
type (
// 文章基础结构(对应数据表字段)
Article {
Id int64 `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Cover string `json:"cover"`
CreateAt string `json:"create_at"` // 前端通常用字符串承载时间,可根据需求改为 time.Time
UpdateAt string `json:"update_at"`
IsDelete int `json:"is_delete"` // tinyint 映射为 int1=已删除0=未删除
Topic string `json:"topic"`
Excerpt string `json:"excerpt"`
}
// 1. 创建文章:请求体
CreateArticleReq {
Title string `json:"title"` // 文章标题
Content string `json:"content"` // 文章内容
Cover string `json:"cover"` // 封面图
Topic string `json:"topic"` // 话题
}
// 创建文章:响应
CreateArticleResp {
Id int64 `json:"id"` // 新增文章的 ID
Success bool `json:"success"` // 操作是否成功
}
// 2. 文章列表:请求(带分页/筛选)
ListArticleReq {
Page int `json:"page,optional"` // 页码(可选,默认第一页)
Size int `json:"size,optional"` // 每页条数(可选,默认 10 条)
Topic string `json:"topic,optional"` // 按话题筛选(可选)
}
// 文章列表:响应
ListArticleResp {
Total int64 `json:"total"` // 总条数
List []Article `json:"list"` // 文章列表
}
// 3. 文章详情:请求(通过 ID 查询)
DetailArticleReq {
Id int64 `path:"id"` // 从路径参数取 ID如 /api/articles/:id
}
// 文章详情:响应
DetailArticleResp {
Article Article `json:"article"` // 文章详情
Success bool `json:"success"` // 操作是否成功
}
// 4. 更新文章:请求(部分字段可选更新)
UpdateArticleReq {
Id int64 `path:"id"` // 要更新的文章 ID
Title string `json:"title,optional"`
Content string `json:"content,optional"`
Cover string `json:"cover,optional"`
Topic string `json:"topic,optional"`
Excerpt string `json:"excerpt,optional"`
}
// 更新文章:响应
UpdateArticleResp {
Success bool `json:"success"`
}
// 5. 删除文章(软删除:修改 is_delete 为 1请求
DeleteArticleReq {
Id int64 `path:"id"` // 要删除的文章 ID
}
// 删除文章:响应
DeleteArticleResp {
Success bool `json:"success"`
}
)
// 文章服务的 API 分组与路由前缀
@server (
group: article
prefix: /api/articles
)
service article-api {
// 1. 创建文章POST
@handler CreateArticleHandler
post / (CreateArticleReq) returns (CreateArticleResp)
// 2. 查询文章列表GET
@handler ListArticleHandler
get / (ListArticleReq) returns (ListArticleResp)
// 3. 查询文章详情GET
@handler DetailArticleHandler
get /:id (DetailArticleReq) returns (DetailArticleResp)
// 4. 更新文章PUT
@handler UpdateArticleHandler
put /:id (UpdateArticleReq) returns (UpdateArticleResp)
// 5. 删除文章软删除DELETE
@handler DeleteArticleHandler
delete /:id (DeleteArticleReq) returns (DeleteArticleResp)
}

14
server/api/ping.api Normal file
View File

@@ -0,0 +1,14 @@
type PingReq {}
type PingResp {
Msg string `json:"message"`
}
@server (
group: ping
)
service ping-api {
@handler PingHandler
get /ping (PingReq) returns (PingResp)
}

19
server/api/upload.api Normal file
View File

@@ -0,0 +1,19 @@
type (
UploadImageResp {
Code int `json:"code"` // 业务状态码
Message string `json:"message"` // 提示信息
Data {
Url string `json:"url"` // 图片访问地址
} `json:"data"`
}
)
@server (
group: upload
prefix: /api
)
service upload-api {
@handler UploadImageHandler
post /upload/image returns (UploadImageResp)
}