mini_article_delete_logic.go
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package article
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type MiniArticleDeleteLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
conn transaction.Conn
}
func NewMiniArticleDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniArticleDeleteLogic {
return &MiniArticleDeleteLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
conn: svcCtx.DefaultDBConn(),
}
}
func (l *MiniArticleDeleteLogic) MiniArticleDelete(req *types.MiniArticleDeleteRequest) (resp *types.MiniArticleDeleteResponse, err error) {
// 文章数据
article, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, l.conn, req.Id)
if err != nil {
return nil, xerr.NewErrMsgErr("帖子不存在", err)
}
userToken := contextdata.GetUserTokenFromCtx(l.ctx)
if userToken.UserId != article.AuthorId || userToken.CompanyId != article.CompanyId {
return nil, xerr.NewErrMsg("您没有权限删除")
}
// 检查发布人
author, err := l.svcCtx.UserRepository.FindOne(l.ctx, l.conn, article.AuthorId)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
}
companyInfo, err := l.svcCtx.CompanyRepository.FindOne(l.ctx, l.conn, author.CompanyId)
if err != nil {
return nil, xerr.NewErrMsgErr("创建文章内容失败", err)
}
articleAuthor := domain.UserSimple{
Id: author.Id,
Name: author.Name,
Avatar: author.Avatar,
Position: author.Position,
Company: companyInfo.Name,
CompanyId: author.CompanyId,
}
article.DeletedType = domain.ArticleDeletedTypeUser
//文章内容详情
queryOption := domain.NewQueryOptions().WithFindOnly().MustWithKV("articleId", req.Id)
_, sectionList, err := l.svcCtx.ArticleSectionRepository.Find(l.ctx, l.conn, queryOption)
if err != nil {
return nil, xerr.NewErrMsgErr("帖子不存在", err)
}
var backup domain.ArticleBackup
backup.MakeBackup(articleAuthor, article, sectionList, "删除")
err = transaction.UseTrans(l.ctx, l.conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
//更新删除类型
_, err = l.svcCtx.ArticleRepository.Update(l.ctx, conn, article)
if err != nil {
return err
}
//删除
_, err = l.svcCtx.ArticleRepository.Delete(l.ctx, conn, article)
if err != nil {
return err
}
//备份
_, err = l.svcCtx.ArticleBackupRepository.Insert(ctx, conn, &backup)
if err != nil {
return xerr.NewErrMsgErr("删除失败", err)
}
return nil
}, true)
if err != nil {
return nil, xerr.NewErrMsgErr("删除失败", err)
}
resp = &types.MiniArticleDeleteResponse{
Id: article.Id,
Title: article.Title,
}
return
}