Changeset fcc3080


Ignore:
Timestamp:
11/14/21 13:58:42 (3 years ago)
Author:
Стојков Марко <mst@…>
Branches:
dev
Children:
f3c4950
Parents:
e071d30
Message:

Fixed bug when voting an answer

Location:
src
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question-state-response.models.ts

    re071d30 rfcc3080  
    8080  public voteUid!: string;
    8181  public voteType!: VoteType;
     82  public voteAlreadyExists!: boolean;
    8283}
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.mapper.ts

    re071d30 rfcc3080  
    9090
    9191  public static ToVoteAnswerViewModel(response: VoteAnswerResponse): VoteAnswerViewModel {
    92     return new VoteAnswerViewModel(response.answerUid, response.voteUid, response.voteType);
     92    return new VoteAnswerViewModel(response.answerUid, response.voteUid, response.voteType, response.voteAlreadyExists);
    9393  }
    9494
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.reducers.ts

    re071d30 rfcc3080  
    3939                  case VoteType.Upvote:
    4040                    votesCountNew++;
     41                    if (action.payload.voteAlreadyExists) {
     42                      votesCountNew++;
     43                    }
    4144                    break;
    4245                  case VoteType.Downvote:
    4346                    votesCountNew--;
     47                    if (action.payload.voteAlreadyExists) {
     48                      votesCountNew--;
     49                    }
    4450                    break;
    4551                }
  • src/Clients/Angular/finki-chattery/src/app/shared-app/models/question-state-view-models.models.ts

    re071d30 rfcc3080  
    7777
    7878export class VoteAnswerViewModel {
    79   constructor(public answerUid: string, public voteUid: string, public voteType: VoteType) {}
     79  constructor(public answerUid: string, public voteUid: string, public voteType: VoteType, public voteAlreadyExists: boolean) {}
    8080}
  • src/FinkiChattery/FinkiChattery.Api/ApplicationServices/Questioning/EventHandlers/UpdateAnswerVotesEventHandler.cs

    re071d30 rfcc3080  
    3030                case VoteType.Upvote:
    3131                    answer.VotesCount++;
     32
     33                    if (notification.VoteAlreadyExists)
     34                    {
     35                        answer.VotesCount++;
     36                    }
    3237                    break;
    3338                case VoteType.Downvote:
    3439                    answer.VotesCount--;
     40                    if (notification.VoteAlreadyExists)
     41                    {
     42                        answer.VotesCount--;
     43                    }
    3544                    break;
    3645            }
  • src/FinkiChattery/FinkiChattery.Api/ApplicationServices/Questioning/EventHandlers/UpdateStudentReputationEventHandler.cs

    re071d30 rfcc3080  
    3030                case VoteType.Upvote:
    3131                    student.Reputation++;
     32                    if (notification.VoteAlreadyExists)
     33                    {
     34                        student.Reputation++;
     35                    }
    3236                    break;
    3337                case VoteType.Downvote:
    3438                    student.Reputation--;
     39                    if (notification.VoteAlreadyExists)
     40                    {
     41                        student.Reputation++;
     42                    }
    3543                    break;
    3644            }
  • src/FinkiChattery/FinkiChattery.Api/Controllers/v1/VotesController.cs

    re071d30 rfcc3080  
    2929        {
    3030            VoteType voteType = request.VoteType == VoteTypeRequest.Upvote ? VoteType.Upvote : VoteType.Downvote;
    31             var voteUid = await MediatorService.SendAsync(new VoteAnswerCommand(voteType, answerUid, questionUid));
    32             return Ok(new VoteAnswerResponse(answerUid, voteUid, request.VoteType));
     31            var vote = await MediatorService.SendAsync(new VoteAnswerCommand(voteType, answerUid, questionUid));
     32            return Ok(new VoteAnswerResponse(answerUid, vote.VoteUid, request.VoteType, vote.VoteAlreadyExists));
    3333        }
    3434    }
  • src/FinkiChattery/FinkiChattery.Commands/Questioning/VoteAnswer/AnswerVotedEvent.cs

    re071d30 rfcc3080  
    77    public class AnswerVotedEvent : IEvent
    88    {
    9         public AnswerVotedEvent(Guid questionUid, Guid answerUid, Guid studentUid, VoteType voteType)
     9        public AnswerVotedEvent(Guid questionUid, Guid answerUid, Guid studentUid, VoteType voteType, bool voteAlreadyExists)
    1010        {
    1111            QuestionUid = questionUid;
     
    1313            StudentUid = studentUid;
    1414            VoteType = voteType;
     15            VoteAlreadyExists = voteAlreadyExists;
    1516        }
    1617
     
    1920        public Guid StudentUid { get; }
    2021        public VoteType VoteType { get; }
     22        public bool VoteAlreadyExists { get; }
    2123    }
    2224}
  • src/FinkiChattery/FinkiChattery.Commands/Questioning/VoteAnswer/VoteAnswerCommand.cs

    re071d30 rfcc3080  
    1111namespace FinkiChattery.Commands.Questioning
    1212{
    13     public class VoteAnswerCommand : ICommand<Guid>
     13    public class VoteAnswerCommand : ICommand<VoteAnswerDto>
    1414    {
    1515        public VoteAnswerCommand(VoteType voteType, Guid answerUid, Guid questionUid)
     
    2525    }
    2626
    27     public class VoteAnswerHandler : ICommandHandler<VoteAnswerCommand, Guid>
     27    public class VoteAnswerHandler : ICommandHandler<VoteAnswerCommand, VoteAnswerDto>
    2828    {
    2929        public VoteAnswerHandler(IUnitOfWork unitOfWork, ICurrentUser currentUser, IEventService eventService)
     
    3838        public IEventService EventService { get; }
    3939
    40         public async Task<Guid> Handle(VoteAnswerCommand request, CancellationToken cancellationToken)
     40        public async Task<VoteAnswerDto> Handle(VoteAnswerCommand request, CancellationToken cancellationToken)
    4141        {
    4242            var student = await UnitOfWork.Students.GetStudent(CurrentUser.Id);
    4343            var answer = await UnitOfWork.Answers.GetByUidAsync(request.AnswerUid);
    4444            var vote = await UnitOfWork.Votes.GetVoteForAnswerByStudent(request.AnswerUid, CurrentUser.Id);
     45            var voteAlreadyExists = false;
    4546
    4647            if (vote == null)
     
    5859            {
    5960                vote.VoteType = request.VoteType;
     61                voteAlreadyExists = true;
    6062            }
    6163
    6264            await UnitOfWork.SaveAsync();
    6365
    64             EventService.Enqueue(new AnswerVotedEvent(request.QuestionUid, request.AnswerUid, student.Uid, request.VoteType));
     66            EventService.Enqueue(new AnswerVotedEvent(request.QuestionUid, request.AnswerUid, student.Uid, request.VoteType, voteAlreadyExists));
    6567
    66             return vote.Uid;
     68            return new VoteAnswerDto(vote.Uid, voteAlreadyExists);
    6769        }
    6870    }
  • src/FinkiChattery/FinkiChattery.Contracts/Questioning/VoteAnswer/VoteAnswerResponse.cs

    re071d30 rfcc3080  
    55    public class VoteAnswerResponse
    66    {
    7         public VoteAnswerResponse(Guid answerUid, Guid voteUid, VoteTypeRequest voteType)
     7        public VoteAnswerResponse(Guid answerUid, Guid voteUid, VoteTypeRequest voteType, bool voteAlreadyExists)
    88        {
    99            AnswerUid = answerUid;
    1010            VoteUid = voteUid;
    1111            VoteType = voteType;
     12            VoteAlreadyExists = voteAlreadyExists;
    1213        }
    1314
     
    1516        public Guid VoteUid { get; }
    1617        public VoteTypeRequest VoteType { get; }
     18        public bool VoteAlreadyExists { get; }
    1719    }
    1820}
Note: See TracChangeset for help on using the changeset viewer.