fix version 1
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
@@ -32,12 +31,16 @@ func AddComment(ctx context.Context, in *pb.CommentItem) (*pb.StatusReply, error
|
||||
Cms: in.Cms,
|
||||
}
|
||||
authName := map[string]any{}
|
||||
err = impl.DBService.Table("mall_staff").Take(&authName, "identity=?", auth.Identity).Error
|
||||
if err != nil {
|
||||
if err = impl.DBService.Table("mall_staff").Take(&authName, "identity=?", auth.Identity).Error; err != nil {
|
||||
// 非 mall_staff 用户(普通会员等)查不到记录时不阻断评论,仅记录日志
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
data.OwnerName = authName["name"].(string)
|
||||
// 安全类型断言:未命中或 name 为 NULL 时取不到名字,避免 panic
|
||||
if v, ok := authName["name"].(string); ok {
|
||||
data.OwnerName = v
|
||||
} else {
|
||||
data.OwnerName = auth.Identity
|
||||
}
|
||||
err = models.AddComment(&data, auth.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
// 删除文章
|
||||
func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -26,7 +26,15 @@ func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, er
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = models.DeletePost(in.Identity, "") // Todo:
|
||||
// 校验归属:仅文章作者/所有者可删除
|
||||
post, err := models.GetPostForAuth(in.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if post.AuthorIdentity != auth.Identity && post.OwnerIdentity != auth.Identity {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
err = models.DeletePost(in.Identity, auth.Identity)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
// DeleteComment 删除评论
|
||||
func DeleteComment(ctx context.Context, in *pb.DeleteCommentRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -21,6 +21,14 @@ func DeleteComment(ctx context.Context, in *pb.DeleteCommentRequest) (*pb.Status
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 校验归属:仅评论作者可删除
|
||||
comment, err := models.GetCommentForAuth(in.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if comment.OwnerIdentity != auth.Identity {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
err = models.DeleteComment(in.Identity)
|
||||
if err != nil {
|
||||
fmt.Println()
|
||||
|
||||
@@ -13,13 +13,21 @@ import (
|
||||
|
||||
// 修改文章
|
||||
func Modify(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.GetSiteIdentity() == "" || in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 校验归属:仅文章作者/所有者可修改
|
||||
post, err := models.GetPostForAuth(in.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if post.AuthorIdentity != auth.Identity && post.OwnerIdentity != auth.Identity {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
var data = &models.CmsPost{
|
||||
SiteIdentity: in.SiteIdentity,
|
||||
Title: in.Title,
|
||||
|
||||
@@ -14,13 +14,21 @@ import (
|
||||
|
||||
// 修改评论
|
||||
func ModifyComment(ctx context.Context, in *pb.CommentItem) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
// 校验归属:仅评论作者可修改
|
||||
comment, err := models.GetCommentForAuth(in.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if comment.OwnerIdentity != auth.Identity {
|
||||
return nil, errcode.ErrPermissionDenied
|
||||
}
|
||||
|
||||
var data = models.CmsComment{
|
||||
Std_Identity: types.Std_Identity{Identity: in.Identity},
|
||||
|
||||
@@ -26,12 +26,15 @@ func Search(ctx context.Context, in *pb.CmsSearchRequest) (reply *pb.PostListRep
|
||||
// vlidate request page_no,page_size.
|
||||
if pageNo < 1 {
|
||||
pageNo = 1
|
||||
offset = 0
|
||||
}
|
||||
if pageSize < 50 {
|
||||
pageSize = 50
|
||||
offset = (pageNo - 1) * pageSize
|
||||
// 归一化分页:默认 20 条,最大 100 条,避免一次拉取全表
|
||||
if pageSize < 20 {
|
||||
pageSize = 20
|
||||
}
|
||||
if pageSize > 100 {
|
||||
pageSize = 100
|
||||
}
|
||||
offset = (pageNo - 1) * pageSize
|
||||
|
||||
var data []*models.CmsPost
|
||||
tx := impl.DBService.Model(&models.CmsPost{}).Where("title like ? or Cms like ?", "%"+in.Keyword+"%", "%"+in.Keyword+"%")
|
||||
|
||||
@@ -4,12 +4,12 @@ import (
|
||||
"context"
|
||||
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 添加站点
|
||||
// 说明:站点逻辑尚未实现,显式返回未实现错误,避免调用方误判为成功
|
||||
func Create(ctx context.Context, in *pb.SiteItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
@@ -17,13 +17,5 @@ func Create(ctx context.Context, in *pb.SiteItem) (reply *pb.StatusReply, err er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: valid code
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
return nil, errcode.ErrUnimplemented
|
||||
}
|
||||
|
||||
@@ -3,15 +3,13 @@ package site
|
||||
import (
|
||||
"context"
|
||||
|
||||
"time"
|
||||
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 删除站点
|
||||
// 说明:站点逻辑尚未实现,显式返回未实现错误,避免调用方误判为成功
|
||||
func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
@@ -24,11 +22,5 @@ func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, er
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
return nil, errcode.ErrUnimplemented
|
||||
}
|
||||
|
||||
@@ -4,10 +4,12 @@ import (
|
||||
"context"
|
||||
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 站点列表
|
||||
// 说明:站点逻辑尚未实现,显式返回未实现错误,避免返回空的成功响应
|
||||
func Fetch(ctx context.Context, in *pb.Empty) (reply *pb.SiteListReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
@@ -15,9 +17,5 @@ func Fetch(ctx context.Context, in *pb.Empty) (reply *pb.SiteListReply, err erro
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: valid code
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return
|
||||
return nil, errcode.ErrUnimplemented
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
)
|
||||
|
||||
// 站点详情
|
||||
// 说明:站点逻辑尚未实现,显式返回未实现错误,避免返回 nil 的成功响应
|
||||
func Get(ctx context.Context, in *pb.IdentRequest) (reply *pb.SiteItem, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
@@ -21,7 +22,5 @@ func Get(ctx context.Context, in *pb.IdentRequest) (reply *pb.SiteItem, err erro
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return
|
||||
return nil, errcode.ErrUnimplemented
|
||||
}
|
||||
|
||||
@@ -3,14 +3,13 @@ package site
|
||||
import (
|
||||
"context"
|
||||
|
||||
"time"
|
||||
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 修改站点
|
||||
// 说明:站点逻辑尚未实现,显式返回未实现错误,避免调用方误判为成功
|
||||
func Modify(ctx context.Context, in *pb.SiteItem) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
@@ -18,13 +17,5 @@ func Modify(ctx context.Context, in *pb.SiteItem) (reply *pb.StatusReply, err er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: valid code
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
return nil, errcode.ErrUnimplemented
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
@@ -203,6 +204,30 @@ func ModifyPost(identity string, post *CmsPost, accessory, category, tags []stri
|
||||
})
|
||||
}
|
||||
|
||||
// GetPostForAuth 按 identity 查询文章(不更新点击量),用于写操作的归属校验
|
||||
func GetPostForAuth(identity string) (*CmsPost, error) {
|
||||
post := new(CmsPost)
|
||||
if err := impl.DBService.Where("identity = ?", identity).Take(post).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
|
||||
// GetCommentForAuth 按 identity 查询评论,用于写操作的归属校验
|
||||
func GetCommentForAuth(identity string) (*CmsComment, error) {
|
||||
comment := new(CmsComment)
|
||||
if err := impl.DBService.Where("identity = ?", identity).Take(comment).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errcode.ErrRecordNotFound
|
||||
}
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return comment, nil
|
||||
}
|
||||
|
||||
// DeletePost 删除文章
|
||||
func DeletePost(identity, authorIdentity string) (err error) {
|
||||
return impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
@@ -265,10 +290,13 @@ func GetPost(key, val string) (*CmsPost, error) {
|
||||
// - error: 错误信息
|
||||
func IncrOrDescPostField(identity, column string, desc bool) (err error) {
|
||||
expr := "%s + ?"
|
||||
tx := impl.DBService.Model(&CmsPost{}).Where("identity = ?", identity)
|
||||
if desc {
|
||||
expr = "%s - ?"
|
||||
// 减少时限定计数大于 0,避免出现负值
|
||||
tx = tx.Where(column+" > ?", 0)
|
||||
}
|
||||
if err := impl.DBService.Model(&CmsPost{}).Where("identity = ?", identity).UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
if err := tx.UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -308,12 +336,17 @@ func DeleteComment(identity string) (err error) {
|
||||
return err
|
||||
}
|
||||
err = impl.DBService.Transaction(func(tx *gorm.DB) error {
|
||||
// 删除评论,更新文章评论量
|
||||
// 统计将被级联删除的子评论数量,保证文章评论计数与实际评论数一致
|
||||
var childCnt int64
|
||||
if err := tx.Model(&CmsComment{}).Where("parent_id = ?", comment.ID).Count(&childCnt).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除评论,更新文章评论量(减去本评论及其被级联删除的子评论总数)
|
||||
if err := tx.Where("identity = ?", identity).Delete(&CmsComment{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Model(&CmsPost{}).Where("identity = ?", comment.PostIdentity).
|
||||
UpdateColumn("comment_hits", gorm.Expr("comment_hits - ?", 1)).Error; err != nil {
|
||||
UpdateColumn("comment_hits", gorm.Expr("comment_hits - ?", childCnt+1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if comment.ParentId != 0 {
|
||||
@@ -343,13 +376,17 @@ func DeleteComment(identity string) (err error) {
|
||||
// 返回:
|
||||
// - error: 错误信息
|
||||
//
|
||||
// TODO: 记录点赞对象避免重复点赞
|
||||
// 说明:现有表结构未记录「谁点过赞」,无法做完整去重;
|
||||
// 完整去重需新增「点赞记录表/字段」,此处仅在计数层做下限保护,避免被刷成负数。
|
||||
func IncrOrDescCommentField(identity, column string, desc bool) (err error) {
|
||||
expr := "%s + ?"
|
||||
tx := impl.DBService.Model(&CmsComment{}).Where("identity = ?", identity)
|
||||
if desc {
|
||||
expr = "%s - ?"
|
||||
// 减少时限定计数大于 0,避免出现负值
|
||||
tx = tx.Where(column+" > ?", 0)
|
||||
}
|
||||
if err = impl.DBService.Model(&CmsComment{}).Where("identity = ?", identity).UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
if err = tx.UpdateColumn(column, gorm.Expr(fmt.Sprintf(expr, column), 1)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user