source: src/FinkiChattery/FinkiChattery.Api/ApplicationServices/Questioning/EventHandlers/UpdateAnswerVotesEventHandler.cs@ fcc3080

dev
Last change on this file since fcc3080 was fcc3080, checked in by Стојков Марко <mst@…>, 3 years ago

Fixed bug when voting an answer

  • Property mode set to 100644
File size: 1.5 KB
Line 
1using FinkiChattery.Commands.Questioning;
2using FinkiChattery.Common.Mediator.Contracs;
3using FinkiChattery.Persistence.Helpers;
4using FinkiChattery.Persistence.UnitOfWork;
5using System.Threading;
6using System.Threading.Tasks;
7
8namespace FinkiChattery.Api.ApplicationServices.Questioning.EventHandlers
9{
10 public class UpdateAnswerVotesEventHandler : IEventHandler<AnswerVotedEvent>
11 {
12 public UpdateAnswerVotesEventHandler(IUnitOfWork unitOfWork)
13 {
14 UnitOfWork = unitOfWork;
15 }
16
17 public IUnitOfWork UnitOfWork { get; }
18
19 public async Task Handle(AnswerVotedEvent notification, CancellationToken cancellationToken)
20 {
21 var answer = await UnitOfWork.Answers.GetByUidAsync(notification.AnswerUid);
22
23 if (answer == null)
24 {
25 return;
26 }
27
28 switch (notification.VoteType)
29 {
30 case VoteType.Upvote:
31 answer.VotesCount++;
32
33 if (notification.VoteAlreadyExists)
34 {
35 answer.VotesCount++;
36 }
37 break;
38 case VoteType.Downvote:
39 answer.VotesCount--;
40 if (notification.VoteAlreadyExists)
41 {
42 answer.VotesCount--;
43 }
44 break;
45 }
46
47 await UnitOfWork.SaveAsync();
48 }
49 }
50}
Note: See TracBrowser for help on using the repository browser.