|
3 | 3 | using Abp.AutoMapper; |
4 | 4 | using Abp.Domain.Entities; |
5 | 5 | using Abp.Domain.Repositories; |
| 6 | +using LetsDisc.Authorization.Users; |
6 | 7 | using LetsDisc.PostDetails; |
7 | 8 | using LetsDisc.Posts.Dto; |
8 | 9 | using LetsDisc.Tags; |
@@ -36,16 +37,19 @@ public class PostAppService : AsyncCrudAppService<Post, PostDto, int, PagedAndSo |
36 | 37 | private readonly IRepository<Vote> _voteRepository; |
37 | 38 | private readonly IRepository<Tag> _tagRepository; |
38 | 39 | private readonly IRepository<PostTag> _postTagRepository; |
| 40 | + private readonly IRepository<UserDetails, long> _userDetailsRepository; |
39 | 41 |
|
40 | 42 | public PostAppService(IRepository<Post> postRepository, |
41 | 43 | IRepository<Vote> voteRepository, |
42 | 44 | IRepository<Tag> tagRepository, |
43 | | - IRepository<PostTag> postTagRepository) : base(postRepository) |
| 45 | + IRepository<PostTag> postTagRepository, |
| 46 | + IRepository<UserDetails, long> userDetailsRepository) : base(postRepository) |
44 | 47 | { |
45 | 48 | _postRepository = postRepository; |
46 | 49 | _voteRepository = voteRepository; |
47 | 50 | _tagRepository = tagRepository; |
48 | 51 | _postTagRepository = postTagRepository; |
| 52 | + _userDetailsRepository = userDetailsRepository; |
49 | 53 | } |
50 | 54 |
|
51 | 55 | public override async Task<PostDto> Create(CreatePostDto input) |
@@ -287,69 +291,87 @@ public async Task<PagedResultDto<PostDto>> GetSearchPosts(PagedAndSortedResultRe |
287 | 291 | }; |
288 | 292 | } |
289 | 293 |
|
| 294 | + private static VoteChangeOutput ReturnVoteChangeOutput(Post post, bool upvote, bool downvote) |
| 295 | + { |
| 296 | + return new VoteChangeOutput() |
| 297 | + { |
| 298 | + VoteCount = post.Score, |
| 299 | + UpVote = upvote, |
| 300 | + DownVote = downvote, |
| 301 | + PostTypeId = post.PostTypeId, |
| 302 | + PostId = post.Id |
| 303 | + }; |
| 304 | + } |
| 305 | + |
| 306 | + private void decreaseUserReputation(UserDetails user, int dec) |
| 307 | + { |
| 308 | + user.Reputation -= dec; |
| 309 | + } |
| 310 | + |
| 311 | + private void increaseUserReputation(UserDetails user, int inc) |
| 312 | + { |
| 313 | + user.Reputation += inc; |
| 314 | + } |
| 315 | + |
290 | 316 | //Voting Up the Post both Question and Answer, where there will be two things either Upvoting or Downvoting |
291 | 317 | public async Task<VoteChangeOutput> PostVoteUp(int id) |
292 | 318 | { |
293 | 319 | var post = await _postRepository.GetAsync(id); |
294 | 320 | var isUserVoted = _voteRepository.FirstOrDefault(userVote => userVote.CreatorUserId == AbpSession.UserId && userVote.PostId == id); |
| 321 | + var user = _userDetailsRepository.FirstOrDefault(u => u.UserId == post.CreatorUserId); |
295 | 322 | if (isUserVoted != null) |
296 | 323 | { |
297 | 324 | if(isUserVoted.VoteTypeId == (int)VoteTypes.Upvote) |
298 | 325 | { |
299 | 326 | post.Score--; |
300 | 327 | await _voteRepository.DeleteAsync(isUserVoted.Id); |
| 328 | + decreaseUserReputation(user, 10); |
301 | 329 | } |
302 | 330 | else if( isUserVoted.VoteTypeId == (int)VoteTypes.Downvote) |
303 | 331 | { |
304 | 332 | post.Score += 2; |
305 | 333 | isUserVoted.VoteTypeId = (int)VoteTypes.Upvote; |
| 334 | + increaseUserReputation(user, 12); |
306 | 335 | return ReturnVoteChangeOutput(post, true, false); |
307 | 336 | } |
308 | 337 | } |
309 | 338 | else |
310 | 339 | { |
311 | 340 | post.Score++; |
312 | 341 | await _voteRepository.InsertAsync(new Vote(id, (int)VoteTypes.Upvote)); |
| 342 | + increaseUserReputation(user, 10); |
313 | 343 | return ReturnVoteChangeOutput(post, true, false); |
314 | 344 | } |
315 | 345 | return ReturnVoteChangeOutput(post, false, false); |
316 | 346 | } |
317 | 347 |
|
318 | | - private static VoteChangeOutput ReturnVoteChangeOutput(Post post, bool upvote, bool downvote) |
319 | | - { |
320 | | - return new VoteChangeOutput() |
321 | | - { |
322 | | - VoteCount = post.Score, |
323 | | - UpVote = upvote, |
324 | | - DownVote = downvote, |
325 | | - PostTypeId = post.PostTypeId, |
326 | | - PostId = post.Id |
327 | | - }; |
328 | | - } |
329 | | - |
330 | 348 | //Voting Down the Post both Question and Answer, where there will be two things either Upvoting or Downvoting |
331 | 349 | public async Task<VoteChangeOutput> PostVoteDown(int id) |
332 | 350 | { |
333 | 351 | var post = await _postRepository.GetAsync(id); |
334 | 352 | var isUserVoted = _voteRepository.FirstOrDefault(userVote => userVote.CreatorUserId == AbpSession.UserId && userVote.PostId == id); |
| 353 | + var user = _userDetailsRepository.FirstOrDefault(u => u.UserId == post.CreatorUserId); |
335 | 354 | if (isUserVoted != null) |
336 | 355 | { |
337 | 356 | if (isUserVoted.VoteTypeId == (int)VoteTypes.Downvote) |
338 | 357 | { |
339 | 358 | post.Score++; |
340 | 359 | await _voteRepository.DeleteAsync(isUserVoted.Id); |
| 360 | + increaseUserReputation(user, 2); |
341 | 361 | } |
342 | 362 | else if (isUserVoted.VoteTypeId == (int)VoteTypes.Upvote) |
343 | 363 | { |
344 | 364 | post.Score -= 2; |
345 | 365 | isUserVoted.VoteTypeId = (int)VoteTypes.Downvote; |
| 366 | + decreaseUserReputation(user, 12); |
346 | 367 | return ReturnVoteChangeOutput(post, false, true); |
347 | 368 | } |
348 | 369 | } |
349 | 370 | else |
350 | 371 | { |
351 | 372 | post.Score--; |
352 | 373 | await _voteRepository.InsertAsync(new Vote(id, (int)VoteTypes.Downvote)); |
| 374 | + decreaseUserReputation(user, 2); |
353 | 375 | return ReturnVoteChangeOutput(post, false, true); |
354 | 376 | } |
355 | 377 | return ReturnVoteChangeOutput(post, false, false); |
|
0 commit comments