作者 tangxvhui

通用的获取评论列表

... ... @@ -95,10 +95,12 @@ type (
// 小程序获取文章的评论列表
type (
MiniListArticleCommentRequest {
Page int64 `json:"page"`
Size int64 `json:"size"`
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
SectionId int64 `json:"sectionId"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
SectionId int64 `json:"sectionId,optional"`
}
MiniListArticleCommentResponse {
Total int64 `json:"total"`
... ...
... ... @@ -22,6 +22,7 @@ func MiniGetArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := comment.NewMiniGetArticleCommentLogic(r.Context(), svcCtx)
token := contextdata.GetUserTokenFromCtx(r.Context())
req.CompanyId = token.CompanyId
req.UserId = token.UserId
resp, err := l.MiniGetArticleComment(&req)
result.HttpResult(r, w, resp, err)
}
... ...
... ... @@ -21,6 +21,7 @@ func MiniListArticleCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc
l := comment.NewMiniListArticleCommentLogic(r.Context(), svcCtx)
token := contextdata.GetUserTokenFromCtx(r.Context())
req.CompanyId = token.CompanyId
req.UserId = token.UserId
resp, err := l.MiniListArticleComment(&req)
result.HttpResult(r, w, resp, err)
}
... ...
... ... @@ -11,6 +11,7 @@ import (
"text/template"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/logx"
)
... ... @@ -49,13 +50,12 @@ func (l *MiniCreateArticleCommentLogic) MiniCreateArticleComment(req *types.Mini
return nil, xerr.NewErrMsg("没有评论权限")
}
//查看评论权限,
//TODO 临时注释
// if len(articleInfo.WhoReview) > 0 {
// ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)
// if ok < 0 {
// return nil, xerr.NewErrMsg("没有评论权限")
// }
// }
if len(articleInfo.WhoReview) > 0 {
ok := lo.IndexOf(articleInfo.WhoReview, req.FromUserId)
if ok < 0 {
return nil, xerr.NewErrMsg("没有评论权限")
}
}
// 对段落进行评论
var selctionInfo *domain.ArticleSection
if req.SectionId > 0 {
... ...
... ... @@ -5,6 +5,8 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
... ... @@ -24,7 +26,166 @@ func NewMiniListArticleCommentLogic(ctx context.Context, svcCtx *svc.ServiceCont
}
func (l *MiniListArticleCommentLogic) MiniListArticleComment(req *types.MiniListArticleCommentRequest) (resp *types.MiniListArticleCommentResponse, err error) {
// todo: add your logic here and delete this line
// 先获取最顶层的评论
var conn = l.svcCtx.DefaultDBConn()
if req.Page > 40 {
req.Page = 40
}
queryOption := domain.NewQueryOptions().
WithOffsetLimit(req.Page, req.Size).
MustWithKV("topId", 0).
MustWithKV("articleId", req.ArticleId).
MustWithKV("sectionId", req.SectionId).
MustWithKV("show", domain.CommentShowEnable).
MustWithKV("companyId", req.CompanyId)
cnt, commentList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
}
if cnt == 0 {
resp = &types.MiniListArticleCommentResponse{
Total: 0,
List: make([]types.ArticleCommentAndReply, 0),
}
return
}
queryOption = domain.NewQueryOptions().WithFindOnly().
MustWithKV("articleId", req.ArticleId).
MustWithKV("userId", req.UserId)
// 获取我点赞的评论
_, userFlagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("获取评论信息失败", err)
}
// 我点赞的
flagMap := map[int64]struct{}{}
for _, val := range userFlagList {
flagMap[val.CommentId] = struct{}{}
}
resp = &types.MiniListArticleCommentResponse{
Total: cnt,
List: make([]types.ArticleCommentAndReply, len(commentList)),
}
// 获取回复的评论
for i, val := range commentList {
item := types.ArticleCommentAndReply{
Comment: types.ArticleCommentItem{
Id: val.Id,
Pid: val.Pid,
TopId: val.TopId,
ArtitcleId: val.ArticleId,
SectionId: val.SectionId,
FromUserId: val.FromUserId,
FromUser: types.CommentAuthor{
Id: val.FromUser.Id,
Name: val.FromUser.Name,
Avatar: val.FromUser.Avatar,
Position: val.FromUser.Position,
Company: val.FromUser.Company,
},
ToUserId: val.ToUserId,
ToUser: types.CommentAuthor{
Id: val.ToUser.Id,
Name: val.ToUser.Name,
Avatar: val.ToUser.Avatar,
Position: val.ToUser.Position,
Company: val.ToUser.Company,
},
SectionContent: val.SectionContent,
CountReply: val.CountReply,
CountUserLove: val.CountUserLove,
CountAdminLove: val.CountAdminLove,
AtWho: []types.CommentAtWho{},
CreatedAt: val.CreatedAt,
MeLoveFlag: 0,
},
Reply: []types.ArticleCommentItem{},
TotalReply: int64(val.CountReply),
}
if _, ok := flagMap[val.Id]; ok {
item.Comment.MeLoveFlag = 1
}
for _, val2 := range val.AtWho {
item.Comment.AtWho = append(item.Comment.AtWho, types.CommentAtWho{
Id: val2.Id,
Name: val2.Name,
})
}
return
//获取回复的评论
cntReply, reply := l.listCommentReply(item.Comment.Id, flagMap)
resp.List[i] = item
resp.List[i].Reply = reply
resp.List[i].TotalReply = cntReply
}
return resp, nil
}
// listCommentReply
func (l *MiniListArticleCommentLogic) listCommentReply(commentId int64, loveFlagMap map[int64]struct{}) (cnt int64, replyList []types.ArticleCommentItem) {
var conn = l.svcCtx.DefaultDBConn()
queryOption := domain.NewQueryOptions().
WithOffsetLimit(1, 2).
MustWithKV("topId", commentId).
MustWithKV("show", domain.CommentShowEnable)
cnt, commentList, err := l.svcCtx.ArticleCommentRepository.Find(l.ctx, conn, queryOption)
if err != nil {
return cnt, []types.ArticleCommentItem{}
}
if cnt == 0 {
replyList = []types.ArticleCommentItem{}
return cnt, replyList
}
for _, val := range commentList {
item := types.ArticleCommentItem{
Id: val.Id,
Pid: val.Pid,
TopId: val.TopId,
ArtitcleId: val.ArticleId,
SectionId: val.SectionId,
FromUserId: val.FromUserId,
FromUser: types.CommentAuthor{
Id: val.FromUser.Id,
Name: val.FromUser.Name,
Avatar: val.FromUser.Avatar,
Position: val.FromUser.Position,
Company: val.FromUser.Company,
},
ToUserId: val.ToUserId,
ToUser: types.CommentAuthor{
Id: val.ToUser.Id,
Name: val.ToUser.Name,
Avatar: val.ToUser.Avatar,
Position: val.ToUser.Position,
Company: val.ToUser.Company,
},
SectionContent: val.SectionContent,
CountReply: val.CountReply,
CountUserLove: val.CountUserLove,
CountAdminLove: val.CountAdminLove,
AtWho: []types.CommentAtWho{},
CreatedAt: val.CreatedAt,
MeLoveFlag: 0,
}
if _, ok := loveFlagMap[val.Id]; ok {
item.MeLoveFlag = 1
}
for _, val2 := range val.AtWho {
item.AtWho = append(item.AtWho, types.CommentAtWho{
Id: val2.Id,
Name: val2.Name,
})
}
replyList = append(replyList, item)
}
return cnt, replyList
}
... ...
... ... @@ -50,10 +50,12 @@ type MiniCreateArticleCommentResponse struct {
}
type MiniListArticleCommentRequest struct {
Page int64 `json:"page"`
Size int64 `json:"size"`
Page int `json:"page"`
Size int `json:"size"`
CompanyId int64 `json:",optional"`
SectionId int64 `json:"sectionId"`
UserId int64 `json:",optional"`
ArticleId int64 `json:"articleId"`
SectionId int64 `json:"sectionId,optional"`
}
type MiniListArticleCommentResponse struct {
... ...
... ... @@ -125,11 +125,23 @@ func (repository *ArticleCommentRepository) Find(ctx context.Context, conn trans
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if v, ok := queryOptions["topId"]; ok {
tx = tx.Where("top_id", v)
tx = tx.Where("top_id=?", v)
}
if v, ok := queryOptions["show"]; ok {
tx = tx.Where("show", v)
tx = tx.Where("show=?", v)
}
if v, ok := queryOptions["articleId"]; ok {
tx = tx.Where("article_id=?", v)
}
if v, ok := queryOptions["sectionId"]; ok {
tx = tx.Where("section_id=?", v)
}
if v, ok := queryOptions["companyId"]; ok {
tx = tx.Where("company_id=?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
... ...