Merge branch 'dev-tangxvhui' into dev
正在显示
12 个修改的文件
包含
615 行增加
和
16 行删除
@@ -8,6 +8,30 @@ info( | @@ -8,6 +8,30 @@ info( | ||
8 | version: "v1" | 8 | version: "v1" |
9 | ) | 9 | ) |
10 | 10 | ||
11 | +// 小程序接口 | ||
12 | +@server( | ||
13 | + prefix: v1/mini | ||
14 | + group: article | ||
15 | + jwt: MiniAuth | ||
16 | +) | ||
17 | +service Core { | ||
18 | + @doc "小程序创建发布内容" | ||
19 | + @handler MiniCreateArticle | ||
20 | + post /article (MiniArticleCreateRequest) returns (MiniArticleCreateResponse) | ||
21 | + @doc "小程序获取我发布的文章" | ||
22 | + @handler MiniArticleSearchMe | ||
23 | + post /article/search/me (MiniArticleSearchMeRequest) returns (MiniArticleSearchMeResponse) | ||
24 | + @doc "小程序获取文章内容详情" | ||
25 | + @handler MiniGetArticle | ||
26 | + get /article/:id (MiniArticleGetRequest) returns (MiniArticleGetResponse) | ||
27 | + @doc "小程序获取文章的点赞人员列表" | ||
28 | + @handler MiniUserLikeArticle | ||
29 | + post /article/user_like/list (MiniUserLikeArticleRequest) returns (MiniUserLikeArticleResponse) | ||
30 | + @doc "小程序人员操作点赞文章/评论" | ||
31 | + @handler MiniSetUserLike | ||
32 | + post /article/user_like/set (MiniSetUserLikeRequset) returns (MiniSetUserLikeResponse) | ||
33 | +} | ||
34 | + | ||
11 | // 坐标地点描述 | 35 | // 坐标地点描述 |
12 | type Location { | 36 | type Location { |
13 | Longitude float64 `json:"longitude,optional"` //经度 | 37 | Longitude float64 `json:"longitude,optional"` //经度 |
@@ -96,20 +120,38 @@ type ( | @@ -96,20 +120,38 @@ type ( | ||
96 | } | 120 | } |
97 | ) | 121 | ) |
98 | 122 | ||
99 | -// 小程序接口 | ||
100 | -@server( | ||
101 | - prefix: v1/mini | ||
102 | - group: article | ||
103 | - jwt: MiniAuth | 123 | +//获取列表,文章有哪些人进行了点赞 |
124 | +type ( | ||
125 | + MiniUserLikeArticleRequest { | ||
126 | + ArticleId int64 `json:"articleId"` // 文章id | ||
127 | + CompanyId int64 `json:"-"` //公司id | ||
128 | + Page int `json:"page"` //分页,第几页 | ||
129 | + Size int `json:"size"` //分页,每页几条 | ||
130 | + } | ||
131 | + MiniUserLikeArticleResponse { | ||
132 | + Total int64 `json:"total"` //总数 | ||
133 | + List []WhichUserLikeArticle `json:"list"` //列表 | ||
134 | + } | ||
135 | + WhichUserLikeArticle { | ||
136 | + ArticleId int64 `json:"articleId"` // 文章id | ||
137 | + UserId int64 `json:"userId"` // 人员id | ||
138 | + Name string `json:"name"` // 人员名称 | ||
139 | + Avatar string `json:"avatar"` // 人员头像 | ||
140 | + CreatedAt int64 `json:"createdAt"` // 点赞记录的时间 | ||
141 | + } | ||
104 | ) | 142 | ) |
105 | -service Core { | ||
106 | - @doc "小程序创建发布内容" | ||
107 | - @handler MiniCreateArticle | ||
108 | - post /article (MiniArticleCreateRequest) returns (MiniArticleCreateResponse) | ||
109 | - @doc "小程序获取我发布的文章" | ||
110 | - @handler MiniArticleSearchMe | ||
111 | - post /article/search/me (MiniArticleSearchMeRequest) returns (MiniArticleSearchMeResponse) | ||
112 | - @doc "小程序获取文章内容详情" | ||
113 | - @handler MiniGetArticle | ||
114 | - get /article/:id (MiniArticleGetRequest) returns (MiniArticleGetResponse) | ||
115 | -} | ||
143 | + | ||
144 | +// 人员点赞文章/评论 | ||
145 | +type ( | ||
146 | + MiniSetUserLikeRequset { | ||
147 | + ArticleId int64 `json:"articleId"` //文章id | ||
148 | + CommentId int64 `json:"commentId"` //评论id | ||
149 | + UserId int64 `json:"-"` //操作人 | ||
150 | + Flag int `json:"flag"` //点赞标志 1、点赞 2 、取消点赞 | ||
151 | + } | ||
152 | + MiniSetUserLikeResponse { | ||
153 | + ArticleId int64 `json:"articleId"` //文章id | ||
154 | + CommentId int64 `json:"commentId"` //评论id | ||
155 | + Count int `json:"count"` //现有的点赞数量 | ||
156 | + } | ||
157 | +) |
1 | +package article | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + | ||
6 | + "github.com/zeromicro/go-zero/rest/httpx" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata" | ||
11 | +) | ||
12 | + | ||
13 | +func MiniSetUserLikeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
14 | + return func(w http.ResponseWriter, r *http.Request) { | ||
15 | + var req types.MiniSetUserLikeRequset | ||
16 | + if err := httpx.Parse(r, &req); err != nil { | ||
17 | + httpx.ErrorCtx(r.Context(), w, err) | ||
18 | + return | ||
19 | + } | ||
20 | + | ||
21 | + l := article.NewMiniSetUserLikeLogic(r.Context(), svcCtx) | ||
22 | + token := contextdata.GetUserTokenFromCtx(r.Context()) | ||
23 | + req.UserId = token.UserId | ||
24 | + resp, err := l.MiniSetUserLike(&req) | ||
25 | + if err != nil { | ||
26 | + httpx.ErrorCtx(r.Context(), w, err) | ||
27 | + } else { | ||
28 | + httpx.OkJsonCtx(r.Context(), w, resp) | ||
29 | + } | ||
30 | + } | ||
31 | +} |
1 | +package article | ||
2 | + | ||
3 | +import ( | ||
4 | + "net/http" | ||
5 | + | ||
6 | + "github.com/zeromicro/go-zero/rest/httpx" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/article" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata" | ||
11 | +) | ||
12 | + | ||
13 | +func MiniUserLikeArticleHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
14 | + return func(w http.ResponseWriter, r *http.Request) { | ||
15 | + var req types.MiniUserLikeArticleRequest | ||
16 | + if err := httpx.Parse(r, &req); err != nil { | ||
17 | + httpx.ErrorCtx(r.Context(), w, err) | ||
18 | + return | ||
19 | + } | ||
20 | + | ||
21 | + l := article.NewMiniUserLikeArticleLogic(r.Context(), svcCtx) | ||
22 | + token := contextdata.GetUserTokenFromCtx(r.Context()) | ||
23 | + req.CompanyId = token.CompanyId | ||
24 | + resp, err := l.MiniUserLikeArticle(&req) | ||
25 | + if err != nil { | ||
26 | + httpx.ErrorCtx(r.Context(), w, err) | ||
27 | + } else { | ||
28 | + httpx.OkJsonCtx(r.Context(), w, resp) | ||
29 | + } | ||
30 | + } | ||
31 | +} |
@@ -192,6 +192,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | @@ -192,6 +192,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | ||
192 | Path: "/article/:id", | 192 | Path: "/article/:id", |
193 | Handler: article.MiniGetArticleHandler(serverCtx), | 193 | Handler: article.MiniGetArticleHandler(serverCtx), |
194 | }, | 194 | }, |
195 | + { | ||
196 | + Method: http.MethodPost, | ||
197 | + Path: "/article/user_like/list", | ||
198 | + Handler: article.MiniUserLikeArticleHandler(serverCtx), | ||
199 | + }, | ||
200 | + { | ||
201 | + Method: http.MethodPost, | ||
202 | + Path: "/article/user_like/set", | ||
203 | + Handler: article.MiniSetUserLikeHandler(serverCtx), | ||
204 | + }, | ||
195 | }, | 205 | }, |
196 | rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret), | 206 | rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret), |
197 | rest.WithPrefix("/v1/mini"), | 207 | rest.WithPrefix("/v1/mini"), |
1 | +package article | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" | ||
10 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" | ||
11 | + | ||
12 | + "github.com/zeromicro/go-zero/core/logx" | ||
13 | +) | ||
14 | + | ||
15 | +type MiniSetUserLikeLogic struct { | ||
16 | + logx.Logger | ||
17 | + ctx context.Context | ||
18 | + svcCtx *svc.ServiceContext | ||
19 | +} | ||
20 | + | ||
21 | +func NewMiniSetUserLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniSetUserLikeLogic { | ||
22 | + return &MiniSetUserLikeLogic{ | ||
23 | + Logger: logx.WithContext(ctx), | ||
24 | + ctx: ctx, | ||
25 | + svcCtx: svcCtx, | ||
26 | + } | ||
27 | +} | ||
28 | + | ||
29 | +// 设置点赞操作 | ||
30 | +func (l *MiniSetUserLikeLogic) MiniSetUserLike(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) { | ||
31 | + //取消点赞文章 | ||
32 | + if req.Flag == 2 && req.CommentId == 0 { | ||
33 | + return l.cancelSetUserLikeArticle(req) | ||
34 | + } | ||
35 | + // 取消点赞评论 | ||
36 | + if req.Flag == 2 && req.CommentId > 0 { | ||
37 | + return l.cancelSetUserLikeComment(req) | ||
38 | + } | ||
39 | + // 设置点赞文章 | ||
40 | + if req.Flag == 1 && req.CommentId == 0 { | ||
41 | + return l.setUserLikeArticle(req) | ||
42 | + } | ||
43 | + // 设置点赞评论 | ||
44 | + if req.Flag == 1 && req.CommentId > 0 { | ||
45 | + return l.setUserLikeComment(req) | ||
46 | + } | ||
47 | + | ||
48 | + return nil, xerr.NewErrMsg("操作失败") | ||
49 | +} | ||
50 | + | ||
51 | +// 取消文章点赞 | ||
52 | +func (l *MiniSetUserLikeLogic) cancelSetUserLikeArticle(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) { | ||
53 | + var conn = l.svcCtx.DefaultDBConn() | ||
54 | + | ||
55 | + // 检查id | ||
56 | + userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId) | ||
57 | + if err != nil { | ||
58 | + //无法确认 文章的id | ||
59 | + return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err) | ||
60 | + } | ||
61 | + articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId) | ||
62 | + if err != nil { | ||
63 | + //无法确认 文章的id | ||
64 | + return nil, xerr.NewErrMsgErr("取消点赞标识失败", err) | ||
65 | + } | ||
66 | + if userInfo.CompanyId != articleInfo.CompanyId { | ||
67 | + // 公司无法对应 | ||
68 | + return nil, xerr.NewErrMsg("没有操作权限") | ||
69 | + } | ||
70 | + queryOption := domain.NewQueryOptions(). | ||
71 | + WithFindOnly(). | ||
72 | + MustWithKV("articleId", req.ArticleId). | ||
73 | + MustWithKV("commentId", req.CommentId). | ||
74 | + MustWithKV("userId", req.UserId) | ||
75 | + _, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption) | ||
76 | + if err != nil { | ||
77 | + return nil, xerr.NewErrMsgErr("取消点赞标识失败", err) | ||
78 | + } | ||
79 | + if len(flagList) == 0 { | ||
80 | + //没有记录 | ||
81 | + resp = &types.MiniSetUserLikeResponse{ | ||
82 | + ArticleId: req.ArticleId, | ||
83 | + CommentId: req.CommentId, | ||
84 | + Count: articleInfo.CountLove, | ||
85 | + } | ||
86 | + return resp, nil | ||
87 | + } | ||
88 | + flagInfo := flagList[0] | ||
89 | + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { | ||
90 | + // 去除点赞标识 | ||
91 | + _, err = l.svcCtx.UserLoveFlagRepository.Delete(ctx, c, flagInfo) | ||
92 | + if err != nil { | ||
93 | + return err | ||
94 | + } | ||
95 | + // 减少文章的点赞数量 | ||
96 | + err = l.svcCtx.ArticleRepository.IncreaseCountLove(ctx, c, -1, articleInfo) | ||
97 | + if err != nil { | ||
98 | + return err | ||
99 | + } | ||
100 | + return nil | ||
101 | + }, true) | ||
102 | + if err != nil { | ||
103 | + return nil, xerr.NewErrMsgErr("取消点赞标识失败", err) | ||
104 | + } | ||
105 | + articleInfo, err = l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId) | ||
106 | + if err == nil { | ||
107 | + return nil, xerr.NewErrMsgErr("获取点赞数量失败", err) | ||
108 | + } | ||
109 | + resp = &types.MiniSetUserLikeResponse{ | ||
110 | + ArticleId: req.ArticleId, | ||
111 | + CommentId: req.CommentId, | ||
112 | + Count: articleInfo.CountLove, | ||
113 | + } | ||
114 | + return resp, nil | ||
115 | +} | ||
116 | + | ||
117 | +// 取消评论点赞 | ||
118 | +func (l *MiniSetUserLikeLogic) cancelSetUserLikeComment(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) { | ||
119 | + var conn = l.svcCtx.DefaultDBConn() | ||
120 | + // 检查id | ||
121 | + userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId) | ||
122 | + if err != nil { | ||
123 | + //无法确认 文章的id | ||
124 | + return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err) | ||
125 | + } | ||
126 | + articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId) | ||
127 | + if err != nil { | ||
128 | + //无法确认 文章的id | ||
129 | + return nil, xerr.NewErrMsgErr("取消点赞标识失败", err) | ||
130 | + } | ||
131 | + if userInfo.CompanyId != articleInfo.CompanyId { | ||
132 | + // 文章和人员的公司无法对应 | ||
133 | + return nil, xerr.NewErrMsg("没有操作权限") | ||
134 | + } | ||
135 | + commentInfo, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId) | ||
136 | + if err != nil { | ||
137 | + return nil, xerr.NewErrMsgErr("无法确认评论信息", err) | ||
138 | + } | ||
139 | + if commentInfo.ArticleId != articleInfo.Id { | ||
140 | + return nil, xerr.NewErrMsg("评论和文章不能对应") | ||
141 | + } | ||
142 | + | ||
143 | + queryOption := domain.NewQueryOptions(). | ||
144 | + WithFindOnly(). | ||
145 | + MustWithKV("articleId", req.ArticleId). | ||
146 | + MustWithKV("commentId", req.CommentId). | ||
147 | + MustWithKV("userId", req.UserId) | ||
148 | + _, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption) | ||
149 | + if err != nil { | ||
150 | + return nil, xerr.NewErrMsgErr("取消点赞标识失败", err) | ||
151 | + } | ||
152 | + if len(flagList) == 0 { | ||
153 | + //没有记录 | ||
154 | + resp = &types.MiniSetUserLikeResponse{ | ||
155 | + ArticleId: req.ArticleId, | ||
156 | + CommentId: req.CommentId, | ||
157 | + Count: commentInfo.CountUserLove, | ||
158 | + } | ||
159 | + return resp, nil | ||
160 | + } | ||
161 | + flagInfo := flagList[0] | ||
162 | + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { | ||
163 | + // 去除点赞标识 | ||
164 | + _, err = l.svcCtx.UserLoveFlagRepository.Delete(ctx, c, flagInfo) | ||
165 | + if err != nil { | ||
166 | + return err | ||
167 | + } | ||
168 | + // 减少评论的点赞数量 | ||
169 | + err = l.svcCtx.ArticleCommentRepository.IncreaseCountUserLove(ctx, c, -1, commentInfo) | ||
170 | + if err != nil { | ||
171 | + return err | ||
172 | + } | ||
173 | + return nil | ||
174 | + }, true) | ||
175 | + if err != nil { | ||
176 | + return nil, xerr.NewErrMsgErr("取消点赞标识失败", err) | ||
177 | + } | ||
178 | + commentInfo, err = l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId) | ||
179 | + if err != nil { | ||
180 | + return nil, xerr.NewErrMsgErr("获取评论的点赞数量失败", err) | ||
181 | + } | ||
182 | + resp = &types.MiniSetUserLikeResponse{ | ||
183 | + ArticleId: req.ArticleId, | ||
184 | + CommentId: req.CommentId, | ||
185 | + Count: commentInfo.CountUserLove, | ||
186 | + } | ||
187 | + return resp, nil | ||
188 | +} | ||
189 | + | ||
190 | +// 设置文章点赞 | ||
191 | +func (l *MiniSetUserLikeLogic) setUserLikeArticle(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) { | ||
192 | + var conn = l.svcCtx.DefaultDBConn() | ||
193 | + | ||
194 | + // 检查id | ||
195 | + userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId) | ||
196 | + if err != nil { | ||
197 | + //无法确认 文章的id | ||
198 | + return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err) | ||
199 | + } | ||
200 | + articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId) | ||
201 | + if err != nil { | ||
202 | + //无法确认 文章的id | ||
203 | + return nil, xerr.NewErrMsgErr("设置点赞标识失败", err) | ||
204 | + } | ||
205 | + if userInfo.CompanyId != articleInfo.CompanyId { | ||
206 | + // 公司无法对应 | ||
207 | + return nil, xerr.NewErrMsg("没有操作权限") | ||
208 | + } | ||
209 | + queryOption := domain.NewQueryOptions(). | ||
210 | + WithFindOnly(). | ||
211 | + MustWithKV("articleId", req.ArticleId). | ||
212 | + MustWithKV("commentId", req.CommentId). | ||
213 | + MustWithKV("userId", req.UserId) | ||
214 | + _, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption) | ||
215 | + if err != nil { | ||
216 | + return nil, xerr.NewErrMsgErr("设置点赞标识失败", err) | ||
217 | + } | ||
218 | + if len(flagList) > 0 { | ||
219 | + //已经有记录 | ||
220 | + resp = &types.MiniSetUserLikeResponse{ | ||
221 | + ArticleId: req.ArticleId, | ||
222 | + CommentId: req.CommentId, | ||
223 | + Count: articleInfo.CountLove, | ||
224 | + } | ||
225 | + return resp, nil | ||
226 | + } | ||
227 | + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { | ||
228 | + flagInfo := domain.UserLoveFlag{ | ||
229 | + Id: 0, | ||
230 | + ArticleId: req.ArticleId, | ||
231 | + CommentId: req.CommentId, | ||
232 | + UserId: req.UserId, | ||
233 | + } | ||
234 | + // 去除点赞标识 | ||
235 | + _, err = l.svcCtx.UserLoveFlagRepository.Insert(ctx, c, &flagInfo) | ||
236 | + if err != nil { | ||
237 | + return err | ||
238 | + } | ||
239 | + // 增加文章的点赞数量 | ||
240 | + err = l.svcCtx.ArticleRepository.IncreaseCountLove(ctx, c, 1, articleInfo) | ||
241 | + if err != nil { | ||
242 | + return err | ||
243 | + } | ||
244 | + return nil | ||
245 | + }, true) | ||
246 | + if err != nil { | ||
247 | + return nil, xerr.NewErrMsgErr("设置点赞标识失败", err) | ||
248 | + } | ||
249 | + articleInfo, err = l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId) | ||
250 | + if err == nil { | ||
251 | + return nil, xerr.NewErrMsgErr("获取点赞数量失败", err) | ||
252 | + } | ||
253 | + resp = &types.MiniSetUserLikeResponse{ | ||
254 | + ArticleId: req.ArticleId, | ||
255 | + CommentId: req.CommentId, | ||
256 | + Count: articleInfo.CountLove, | ||
257 | + } | ||
258 | + return resp, nil | ||
259 | +} | ||
260 | + | ||
261 | +// 设置评论点赞 | ||
262 | +func (l *MiniSetUserLikeLogic) setUserLikeComment(req *types.MiniSetUserLikeRequset) (resp *types.MiniSetUserLikeResponse, err error) { | ||
263 | + var conn = l.svcCtx.DefaultDBConn() | ||
264 | + // 检查id | ||
265 | + userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId) | ||
266 | + if err != nil { | ||
267 | + //无法确认 文章的id | ||
268 | + return nil, xerr.NewErrMsgErr("无法确认操作人员信息", err) | ||
269 | + } | ||
270 | + articleInfo, err := l.svcCtx.ArticleRepository.FindOne(l.ctx, conn, req.ArticleId) | ||
271 | + if err != nil { | ||
272 | + //无法确认 文章的id | ||
273 | + return nil, xerr.NewErrMsgErr("设置点赞标识失败", err) | ||
274 | + } | ||
275 | + if userInfo.CompanyId != articleInfo.CompanyId { | ||
276 | + // 文章和人员的公司无法对应 | ||
277 | + return nil, xerr.NewErrMsg("没有操作权限") | ||
278 | + } | ||
279 | + commentInfo, err := l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId) | ||
280 | + if err != nil { | ||
281 | + return nil, xerr.NewErrMsgErr("无法确认评论信息", err) | ||
282 | + } | ||
283 | + if commentInfo.ArticleId != articleInfo.Id { | ||
284 | + return nil, xerr.NewErrMsg("评论和文章不能对应") | ||
285 | + } | ||
286 | + | ||
287 | + queryOption := domain.NewQueryOptions(). | ||
288 | + WithFindOnly(). | ||
289 | + MustWithKV("articleId", req.ArticleId). | ||
290 | + MustWithKV("commentId", req.CommentId). | ||
291 | + MustWithKV("userId", req.UserId) | ||
292 | + _, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption) | ||
293 | + if err != nil { | ||
294 | + return nil, xerr.NewErrMsgErr("设置点赞标识失败", err) | ||
295 | + } | ||
296 | + if len(flagList) > 0 { | ||
297 | + //已经有记录 | ||
298 | + resp = &types.MiniSetUserLikeResponse{ | ||
299 | + ArticleId: req.ArticleId, | ||
300 | + CommentId: req.CommentId, | ||
301 | + Count: commentInfo.CountUserLove, | ||
302 | + } | ||
303 | + return resp, nil | ||
304 | + } | ||
305 | + | ||
306 | + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, c transaction.Conn) error { | ||
307 | + flagInfo := domain.UserLoveFlag{ | ||
308 | + Id: 0, | ||
309 | + ArticleId: req.ArticleId, | ||
310 | + CommentId: req.CommentId, | ||
311 | + UserId: req.UserId, | ||
312 | + } | ||
313 | + // 设置赞标识 | ||
314 | + _, err = l.svcCtx.UserLoveFlagRepository.Insert(ctx, c, &flagInfo) | ||
315 | + if err != nil { | ||
316 | + return err | ||
317 | + } | ||
318 | + // 增加评论的点赞数量 | ||
319 | + err = l.svcCtx.ArticleCommentRepository.IncreaseCountUserLove(ctx, c, 1, commentInfo) | ||
320 | + if err != nil { | ||
321 | + return err | ||
322 | + } | ||
323 | + return nil | ||
324 | + }, true) | ||
325 | + if err != nil { | ||
326 | + return nil, xerr.NewErrMsgErr("设置点赞标识失败", err) | ||
327 | + } | ||
328 | + commentInfo, err = l.svcCtx.ArticleCommentRepository.FindOne(l.ctx, conn, req.CommentId) | ||
329 | + if err != nil { | ||
330 | + return nil, xerr.NewErrMsgErr("获取评论的点赞数量失败", err) | ||
331 | + } | ||
332 | + resp = &types.MiniSetUserLikeResponse{ | ||
333 | + ArticleId: req.ArticleId, | ||
334 | + CommentId: req.CommentId, | ||
335 | + Count: commentInfo.CountUserLove, | ||
336 | + } | ||
337 | + return resp, nil | ||
338 | +} |
1 | +package article | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + | ||
6 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" | ||
7 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" | ||
8 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" | ||
9 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" | ||
10 | + | ||
11 | + "github.com/zeromicro/go-zero/core/logx" | ||
12 | +) | ||
13 | + | ||
14 | +type MiniUserLikeArticleLogic struct { | ||
15 | + logx.Logger | ||
16 | + ctx context.Context | ||
17 | + svcCtx *svc.ServiceContext | ||
18 | +} | ||
19 | + | ||
20 | +func NewMiniUserLikeArticleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniUserLikeArticleLogic { | ||
21 | + return &MiniUserLikeArticleLogic{ | ||
22 | + Logger: logx.WithContext(ctx), | ||
23 | + ctx: ctx, | ||
24 | + svcCtx: svcCtx, | ||
25 | + } | ||
26 | +} | ||
27 | + | ||
28 | +// 获取点赞的人员列表 | ||
29 | +func (l *MiniUserLikeArticleLogic) MiniUserLikeArticle(req *types.MiniUserLikeArticleRequest) (resp *types.MiniUserLikeArticleResponse, err error) { | ||
30 | + var conn = l.svcCtx.DefaultDBConn() | ||
31 | + queryOption := domain.NewQueryOptions(). | ||
32 | + WithOffsetLimit(req.Page, req.Size). | ||
33 | + MustWithKV("articleId", req.ArticleId). | ||
34 | + MustWithKV("commentId", 0) | ||
35 | + cnt, flagList, err := l.svcCtx.UserLoveFlagRepository.Find(l.ctx, conn, queryOption) | ||
36 | + if err != nil { | ||
37 | + return nil, xerr.NewErrMsgErr("获取人员列表失败", err) | ||
38 | + } | ||
39 | + uidList := []int64{} | ||
40 | + for _, val := range flagList { | ||
41 | + uidList = append(uidList, val.UserId) | ||
42 | + } | ||
43 | + queryOption = domain.NewQueryOptions(). | ||
44 | + WithFindOnly(). | ||
45 | + MustWithKV("ids", uidList) | ||
46 | + | ||
47 | + // 获取人员列表 | ||
48 | + _, userList, err := l.svcCtx.UserRepository.Find(l.ctx, conn, queryOption) | ||
49 | + if err != nil { | ||
50 | + return nil, xerr.NewErrMsgErr("获取人员列表失败", err) | ||
51 | + } | ||
52 | + userMap := map[int64]*domain.User{} | ||
53 | + for i := range userList { | ||
54 | + if userList[i].CompanyId != req.CompanyId { | ||
55 | + return nil, xerr.NewErrMsg("操作失败") | ||
56 | + } | ||
57 | + userMap[userList[i].Id] = userList[i] | ||
58 | + } | ||
59 | + resp = &types.MiniUserLikeArticleResponse{ | ||
60 | + Total: cnt, | ||
61 | + List: make([]types.WhichUserLikeArticle, len(flagList)), | ||
62 | + } | ||
63 | + for i, val := range flagList { | ||
64 | + item := types.WhichUserLikeArticle{ | ||
65 | + ArticleId: val.ArticleId, | ||
66 | + UserId: val.UserId, | ||
67 | + Name: "[用户已被清除]", | ||
68 | + Avatar: "", | ||
69 | + CreatedAt: val.CreatedAt, | ||
70 | + } | ||
71 | + if u, ok := userMap[val.UserId]; ok { | ||
72 | + item.Name = u.Name | ||
73 | + item.Avatar = u.Avatar | ||
74 | + } | ||
75 | + resp.List[i] = item | ||
76 | + } | ||
77 | + return resp, nil | ||
78 | +} |
@@ -349,3 +349,36 @@ type ArticleSearchMe struct { | @@ -349,3 +349,36 @@ type ArticleSearchMe struct { | ||
349 | CountComment int `json:"CountComment"` //评论数量 | 349 | CountComment int `json:"CountComment"` //评论数量 |
350 | Show int `json:"show"` //是否隐藏 [0显示、1不显示] | 350 | Show int `json:"show"` //是否隐藏 [0显示、1不显示] |
351 | } | 351 | } |
352 | + | ||
353 | +type MiniUserLikeArticleRequest struct { | ||
354 | + ArticleId int64 `json:"articleId"` // 文章id | ||
355 | + CompanyId int64 `json:"-"` //公司id | ||
356 | + Page int `json:"page"` //分页,第几页 | ||
357 | + Size int `json:"size"` //分页,每页几条 | ||
358 | +} | ||
359 | + | ||
360 | +type MiniUserLikeArticleResponse struct { | ||
361 | + Total int64 `json:"total"` //总数 | ||
362 | + List []WhichUserLikeArticle `json:"list"` //列表 | ||
363 | +} | ||
364 | + | ||
365 | +type WhichUserLikeArticle struct { | ||
366 | + ArticleId int64 `json:"articleId"` // 文章id | ||
367 | + UserId int64 `json:"userId"` // 人员id | ||
368 | + Name string `json:"name"` // 人员名称 | ||
369 | + Avatar string `json:"avatar"` // 人员头像 | ||
370 | + CreatedAt int64 `json:"createdAt"` // 点赞记录的时间 | ||
371 | +} | ||
372 | + | ||
373 | +type MiniSetUserLikeRequset struct { | ||
374 | + ArticleId int64 `json:"articleId"` //文章id | ||
375 | + CommentId int64 `json:"commentId"` //评论id | ||
376 | + UserId int64 `json:"-"` //操作人 | ||
377 | + Flag int `json:"flag"` //点赞标志 1、点赞 2 、取消点赞 | ||
378 | +} | ||
379 | + | ||
380 | +type MiniSetUserLikeResponse struct { | ||
381 | + ArticleId int64 `json:"articleId"` //文章id | ||
382 | + CommentId int64 `json:"commentId"` //评论id | ||
383 | + Count int `json:"count"` //现有的点赞数量 | ||
384 | +} |
@@ -194,6 +194,11 @@ func (repository *ArticleCommentRepository) DomainModelToModel(from *domain.Arti | @@ -194,6 +194,11 @@ func (repository *ArticleCommentRepository) DomainModelToModel(from *domain.Arti | ||
194 | return to, nil | 194 | return to, nil |
195 | } | 195 | } |
196 | 196 | ||
197 | +// TODO 点赞数量变动 | ||
198 | +func (repository *ArticleCommentRepository) IncreaseCountUserLove(ctx context.Context, conn transaction.Conn, incr int, dm *domain.ArticleComment) error { | ||
199 | + return nil | ||
200 | +} | ||
201 | + | ||
197 | func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository { | 202 | func NewArticleCommentRepository(cache *cache.CachedRepository) domain.ArticleCommentRepository { |
198 | return &ArticleCommentRepository{CachedRepository: cache} | 203 | return &ArticleCommentRepository{CachedRepository: cache} |
199 | } | 204 | } |
@@ -198,6 +198,24 @@ func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (* | @@ -198,6 +198,24 @@ func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (* | ||
198 | return to, nil | 198 | return to, nil |
199 | } | 199 | } |
200 | 200 | ||
201 | +// 点赞数量变动 | ||
202 | +func (repository *ArticleRepository) IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, article *domain.Article) error { | ||
203 | + // | ||
204 | + return nil | ||
205 | +} | ||
206 | + | ||
207 | +// 浏览数量变动 | ||
208 | +func (repository *ArticleRepository) IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, article *domain.Article) error { | ||
209 | + // | ||
210 | + return nil | ||
211 | +} | ||
212 | + | ||
213 | +// 评论数量变动 | ||
214 | +func (repository *ArticleRepository) IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, article *domain.Article) error { | ||
215 | + // | ||
216 | + return nil | ||
217 | +} | ||
218 | + | ||
201 | func NewArticleRepository(cache *cache.CachedRepository) domain.ArticleRepository { | 219 | func NewArticleRepository(cache *cache.CachedRepository) domain.ArticleRepository { |
202 | return &ArticleRepository{CachedRepository: cache} | 220 | return &ArticleRepository{CachedRepository: cache} |
203 | } | 221 | } |
@@ -120,6 +120,15 @@ func (repository *UserLoveFlagRepository) Find(ctx context.Context, conn transac | @@ -120,6 +120,15 @@ func (repository *UserLoveFlagRepository) Find(ctx context.Context, conn transac | ||
120 | ) | 120 | ) |
121 | queryFunc := func() (interface{}, error) { | 121 | queryFunc := func() (interface{}, error) { |
122 | tx = tx.Model(&ms).Order("id desc") | 122 | tx = tx.Model(&ms).Order("id desc") |
123 | + if v, ok := queryOptions["articleId"]; ok { | ||
124 | + tx = tx.Where("article_id=?", v) | ||
125 | + } | ||
126 | + if v, ok := queryOptions["commentId"]; ok { | ||
127 | + tx = tx.Where("comment_id=?", v) | ||
128 | + } | ||
129 | + if v, ok := queryOptions["userId"]; ok { | ||
130 | + tx = tx.Where("user_id=?", v) | ||
131 | + } | ||
123 | if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { | 132 | if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { |
124 | return dms, tx.Error | 133 | return dms, tx.Error |
125 | } | 134 | } |
@@ -37,6 +37,9 @@ type ArticleRepository interface { | @@ -37,6 +37,9 @@ type ArticleRepository interface { | ||
37 | UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error) | 37 | UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Article) (*Article, error) |
38 | FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Article, error) | 38 | FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Article, error) |
39 | Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*Article, error) | 39 | Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*Article, error) |
40 | + IncreaseCountLove(ctx context.Context, conn transaction.Conn, incr int, dm *Article) error //点赞数量变动 | ||
41 | + IncreaseCountComment(ctx context.Context, conn transaction.Conn, incr int, dm *Article) error //评论数量变动 | ||
42 | + IncreaseCountRead(ctx context.Context, conn transaction.Conn, incr int, dm *Article) error //浏览数量变动 | ||
40 | } | 43 | } |
41 | 44 | ||
42 | type ArticleTarget int | 45 | type ArticleTarget int |
@@ -46,4 +46,5 @@ type ArticleCommentRepository interface { | @@ -46,4 +46,5 @@ type ArticleCommentRepository interface { | ||
46 | Delete(ctx context.Context, conn transaction.Conn, dm *ArticleComment) (*ArticleComment, error) | 46 | Delete(ctx context.Context, conn transaction.Conn, dm *ArticleComment) (*ArticleComment, error) |
47 | FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleComment, error) | 47 | FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleComment, error) |
48 | Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleComment, error) | 48 | Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleComment, error) |
49 | + IncreaseCountUserLove(ctx context.Context, conn transaction.Conn, incr int, dm *ArticleComment) error //点赞数量变动 | ||
49 | } | 50 | } |
-
请 注册 或 登录 后发表评论