refactor: Controller 不直接读 Session Exp,新增 CanCreatePost 方法
- 新增 userExpReader 最小接口(ISP),PostService 通过它读取用户经验值
- PostService 新增 CanCreatePost(userID uint) bool,封装 LV0 投稿权限判断
- StudioController 替换内联 c.Get("exp") 检查为调用 ctrl.postService.CanCreatePost(uid)
- 更新 DI 注入链:NewPostService 增加 userRepo 参数
This commit is contained in:
@ -40,7 +40,7 @@ type postUseCase interface {
|
||||
IsPostAccessible(userID uint, role string, postUserID uint) bool
|
||||
}
|
||||
|
||||
// studioUseCase StudioController 对 PostService 的最小依赖(ISP:8 个方法)
|
||||
// studioUseCase StudioController 对 PostService 的最小依赖(ISP:9 个方法)
|
||||
type studioUseCase interface {
|
||||
Create(userID uint, title, body string) (*model.Post, error)
|
||||
GetByID(id uint) (*model.Post, error)
|
||||
@ -50,6 +50,7 @@ type studioUseCase interface {
|
||||
ListByUser(userID uint, status string, page, pageSize int) ([]model.Post, int64, error)
|
||||
GetOverview(userID uint) (*service.StudioOverview, error)
|
||||
IsPostAccessible(userID uint, role string, postUserID uint) bool
|
||||
CanCreatePost(userID uint) bool
|
||||
}
|
||||
|
||||
// spaceUseCase SpaceController 对 SpaceService 的最小依赖(ISP:5 个方法)
|
||||
|
||||
@ -19,11 +19,9 @@ func (ctrl *StudioController) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
// LV0 用户不允许投稿
|
||||
if expVal, exists := c.Get("exp"); exists {
|
||||
if exp, ok := expVal.(int); ok && exp < 1 {
|
||||
common.Error(c, http.StatusForbidden, "经验不足,Lv1 解锁投稿")
|
||||
return
|
||||
}
|
||||
if !ctrl.postService.CanCreatePost(uid) {
|
||||
common.Error(c, http.StatusForbidden, "经验不足,Lv1 解锁投稿")
|
||||
return
|
||||
}
|
||||
|
||||
var req model.PostCreateRequest
|
||||
|
||||
Reference in New Issue
Block a user