把 B 档(此前尚无仓库的对象)的 api 裸查询收进仓库,api 层裸 store.DB 从 162 降到 96(口径:internal/ 下非测试 .go,不含 internal/repository/ 自身)。 新增 7 个仓库:TaskRecordRepo / TaskArtifactRepo / TaskRunRepo / ProjectRepo / StudyNoteRepo / SpecialistRepo / ActionDefinitionRepo。 按对象补齐的方法: - TaskRecordRepo:GetByID / GetByIDForOwners / ListBySpecialistKey / ListByOwners / ListByProject / CountBySpecialistKey / ClearProject / DeleteCascade - ProjectRepo:GetByIDForOwners / ListByOwners - StudyNoteRepo:ListByUser / GetByID - SpecialistRepo:GetByKey / GetByID / Query / List / CountByKey - TaskArtifactRepo / TaskRunRepo:GetByID / ListByTask - ActionDefinitionRepo:List / GetByID / GetByKey 几条口径改由仓库单点持有,避免各处手写漂移: - 归属过滤抽成 ownerScope:owners 为空时套恒假条件(空集),绝不退化成全表。 specialists/runtime 的 MyTaskQuery / ProjectQuery 随之删除,调用方改用 MyTaskOwners + 仓库方法 —— 任务与项目共用同一套归属口径。 - 「任务 + 交付物 + 运行记录三张表同事务级联删除」从 handler 收进 DeleteCascade, 不留没有任务的孤儿交付物;删项目只解除其下任务的 project_id 归属(置 NULL, 不是 0),不删任务本身 —— 任务是「做过的事」,删一个分组不该把它一起抹掉。 - 专员目录的 state 档位(默认 active / all 仅管理员 / system / 其它精确匹配, 且除显式要 system 外一律排除 system 记录)收进 SpecialistRepo.List。 - GetByKey 有意不过滤 state:调用方口径不同(建任务时要能查到,对话取 prompt 时要拒绝 inactive),口径留在调用方,仓库只负责取数。顺带把「按 key 找专员」 从 4 个文件里各写一遍收敛成一处。 - ActionDefinitionRepo.List 不替调用方定 state 默认值 ——「不传 state 就只看 active」是列表接口的契约,由 handler 解析 query 后传入。 - StudyNoteRepo.GetByID 不判归属,越权检查留在 handler(那里能把「不存在」与 「不是你的」分别回成 404 / 403)。 保留未动:skills/api/office_handlers.go 里两处 store.DB.Transaction —— 运行记录 与交付物要在同一个事务里落库,QueryBuilder 不带事务,维持原样并就地注明。 验证:go build ./... 与 go vet ./... 干净,go test ./... 6 个包全绿。 另用独立验证程序走真实路由 + 真实 HTTP,对数据库副本跑 148 项断言全绿 (覆盖跨用户越权 404、空 owners 退化成空集、级联删除、解除归属置 NULL、 笔记按用户隔离、专员 key 唯一性排除自身、管理员 state=all 仍排除 system 等)。 另对其中 10 条关键语义做了变异测试:逐条注入反向实现,确认断言确实会失败, 捕获 10 / 漏掉 0。变异测试同时暴露并修掉了验证体系自身的两个漏洞: - 「state=all 排除 system」这条规则此前没有任何断言能观察到 —— 默认档被 state=active 挡着、system 档被 state=system 挡着,删掉实现也不会红; - 变异驱动只跑 HTTP 断言、不跑 go test,导致针对单元测试注入的变异 (拒绝已下线专员的 prompt)永远逮不到。 验证程序为一次性脚手架,验证完成后已删除(tmp_vfy_b/)。 原始 data/eai_agentplatform.db 全程未触碰(mtime 仍为 2026-09-17 15:14)。 Co-Authored-By: Claude Code <noreply@anthropic.com>
140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
package api
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"eai_agentplatform/backend/internal/middleware"
|
|
"eai_agentplatform/backend/internal/model"
|
|
"eai_agentplatform/backend/internal/repository"
|
|
"eai_agentplatform/backend/internal/web"
|
|
)
|
|
|
|
// noteRepo 学习笔记仓库(便于测试时覆写),包内共享。
|
|
var noteRepo repository.StudyNoteRepo
|
|
|
|
func init() {
|
|
noteRepo = repository.StudyNoteRepo{}
|
|
}
|
|
|
|
var validNoteItemTypes = map[string]bool{"company": true, "product": true, "course": true}
|
|
|
|
// ListNotes GET /api/notes?item_type=&item_id= —— 我的学习笔记
|
|
func ListNotes(c *gin.Context) {
|
|
u := middleware.CurrentUser(c)
|
|
if u == nil {
|
|
web.Fail(c, web.NewAuthError("未登录"))
|
|
return
|
|
}
|
|
// item_id 解析失败(不是数字)就当没传 —— 不给一个错参数回 400,
|
|
// 笔记列表本来就是按内容可筛可不筛。
|
|
var itemID *uint
|
|
if s := c.Query("item_id"); s != "" {
|
|
if n, err := strconv.ParseUint(s, 10, 64); err == nil {
|
|
id := uint(n)
|
|
itemID = &id
|
|
}
|
|
}
|
|
web.OK(c, noteRepo.ListByUser(u.ID, c.Query("item_type"), itemID))
|
|
}
|
|
|
|
// CreateNote POST /api/notes —— 新增学习笔记
|
|
func CreateNote(c *gin.Context) {
|
|
u := middleware.CurrentUser(c)
|
|
if u == nil {
|
|
web.Fail(c, web.NewAuthError("未登录"))
|
|
return
|
|
}
|
|
var req struct {
|
|
ItemType string `json:"item_type"`
|
|
ItemID uint `json:"item_id"`
|
|
Content string `json:"content"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
web.Fail(c, web.NewBadRequest("请求参数错误"))
|
|
return
|
|
}
|
|
if !validNoteItemTypes[req.ItemType] {
|
|
web.Fail(c, web.NewBadRequest("item_type 必须为 company/product/course"))
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.Content) == "" {
|
|
web.Fail(c, web.NewBadRequest("笔记内容不能为空"))
|
|
return
|
|
}
|
|
n := model.StudyNote{UserID: u.ID, ItemType: req.ItemType, ItemID: req.ItemID, Content: strings.TrimSpace(req.Content)}
|
|
if !noteRepo.Insert(&n) {
|
|
web.Fail(c, web.NewBadRequest("保存笔记失败"))
|
|
return
|
|
}
|
|
web.OK(c, n)
|
|
}
|
|
|
|
// UpdateNote PUT /api/notes/{id} —— 编辑学习笔记
|
|
func UpdateNote(c *gin.Context) {
|
|
u := middleware.CurrentUser(c)
|
|
if u == nil {
|
|
web.Fail(c, web.NewAuthError("未登录"))
|
|
return
|
|
}
|
|
id, ok := parseID(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
n, found := noteRepo.GetByID(id)
|
|
if !found {
|
|
web.Fail(c, web.NewNotFoundError("笔记不存在"))
|
|
return
|
|
}
|
|
if n.UserID != u.ID {
|
|
web.Fail(c, web.NewForbiddenError("无权操作他人笔记"))
|
|
return
|
|
}
|
|
var req struct {
|
|
Content string `json:"content"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
web.Fail(c, web.NewBadRequest("请求参数错误"))
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.Content) == "" {
|
|
web.Fail(c, web.NewBadRequest("笔记内容不能为空"))
|
|
return
|
|
}
|
|
n.Content = strings.TrimSpace(req.Content)
|
|
if !noteRepo.Update(&n) {
|
|
web.Fail(c, web.NewBadRequest("更新笔记失败"))
|
|
return
|
|
}
|
|
web.OK(c, n)
|
|
}
|
|
|
|
// DeleteNote DELETE /api/notes/{id} —— 删除学习笔记
|
|
func DeleteNote(c *gin.Context) {
|
|
u := middleware.CurrentUser(c)
|
|
if u == nil {
|
|
web.Fail(c, web.NewAuthError("未登录"))
|
|
return
|
|
}
|
|
id, ok := parseID(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
n, found := noteRepo.GetByID(id)
|
|
if !found {
|
|
web.Fail(c, web.NewNotFoundError("笔记不存在"))
|
|
return
|
|
}
|
|
if n.UserID != u.ID {
|
|
web.Fail(c, web.NewForbiddenError("无权操作他人笔记"))
|
|
return
|
|
}
|
|
if !noteRepo.Delete(&n) {
|
|
web.Fail(c, web.NewBadRequest("删除笔记失败"))
|
|
return
|
|
}
|
|
web.OK(c, gin.H{"id": id})
|
|
}
|