Changeset cbdbb49


Ignore:
Timestamp:
11/04/21 22:19:08 (3 years ago)
Author:
Стојков Марко <mst@…>
Branches:
dev
Children:
7e7cc4c
Parents:
dd264cb
Message:

Fixed response for answer question

Location:
src/FinkiChattery
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/FinkiChattery/FinkiChattery.Api/ApplicationServices/Questioning/Mapper/QuestionMapper.cs

    rdd264cb rcbdbb49  
    3838            if (questionState.AnswersDto.Any())
    3939            {
    40                 answers = questionState.AnswersDto.Select(x =>
    41                 {
    42                     IEnumerable<AnswerResponseQuestionStateResponse> answerResponses = Enumerable.Empty<AnswerResponseQuestionStateResponse>();
    43 
    44                     if (x.AnswerResponsesDto.Any())
    45                     {
    46                         answerResponses = x.AnswerResponsesDto.Select(y =>
    47                         {
    48                             var answerResponseStudent = new AnswerResponseStudentQuestionStateResponse(y.StudentDto.Id, y.StudentDto.Uid, y.StudentDto.Index, y.StudentDto.ImageUrl, y.StudentDto.Reputation);
    49 
    50                             return ToAnswerResponseQuestionStateResponse(y);
    51                         });
    52                     }
    53 
    54                     var answerStudent = new AnswerStudentQuestionStateResponse(x.StudentDto.Id, x.StudentDto.Uid, x.StudentDto.Index, x.StudentDto.ImageUrl, x.StudentDto.Reputation);
    55 
    56                     return new AnswerQuestionStateResponse(x.Id, x.Uid, x.Text, x.CorrectAnswer, x.CreatedOn, x.VotesCount, answerStudent, answerResponses);
    57                 });
     40                answers = questionState.AnswersDto.Select(x => ToAnswerQuestionStateResponse(x));
    5841            }
    5942
     
    7760        }
    7861
     62        public static AnswerQuestionStateResponse ToAnswerQuestionStateResponse(this AnswerQuestionStateDto dto)
     63        {
     64            IEnumerable<AnswerResponseQuestionStateResponse> answerResponses = Enumerable.Empty<AnswerResponseQuestionStateResponse>();
     65
     66            if (dto.AnswerResponsesDto.Any())
     67            {
     68                answerResponses = dto.AnswerResponsesDto.Select(y => ToAnswerResponseQuestionStateResponse(y));
     69            }
     70
     71            var answerStudent = new AnswerStudentQuestionStateResponse(dto.StudentDto.Id, dto.StudentDto.Uid, dto.StudentDto.Index, dto.StudentDto.ImageUrl, dto.StudentDto.Reputation);
     72
     73            return new AnswerQuestionStateResponse(dto.Id, dto.Uid, dto.Text, dto.CorrectAnswer, dto.CreatedOn, dto.VotesCount, answerStudent, answerResponses);
     74        }
     75
    7976        public static AnswerResponseQuestionStateResponse ToAnswerResponseQuestionStateResponse(this AnswerResponseQuestionStateDto dto)
    8077        {
  • src/FinkiChattery/FinkiChattery.Api/Controllers/v1/AnswersController.cs

    rdd264cb rcbdbb49  
    11using FinkiChattery.Api.ApplicationServices.Authentication;
     2using FinkiChattery.Api.ApplicationServices.Questioning;
    23using FinkiChattery.Commands.Questioning;
    34using FinkiChattery.Common.Mediator.Interfaces;
     
    2728        public async Task<IActionResult> AnswerQuestion([FromRoute] Guid questionUid, [FromBody] AnswerQuestionRequest request)
    2829        {
    29             var answerUid = await MediatorService.SendAsync(new AnswerQuestionCommand(questionUid, request.Text));
    30             return Ok(answerUid);
     30            var answer = await MediatorService.SendAsync(new AnswerQuestionCommand(questionUid, request.Text));
     31            return Ok(answer.ToAnswerQuestionStateResponse());
    3132        }
    3233
  • src/FinkiChattery/FinkiChattery.Commands/Questioning/AnswerQuestion/AnswerQuestionCommand.cs

    rdd264cb rcbdbb49  
    33using FinkiChattery.Common.User;
    44using FinkiChattery.Persistence.Models;
     5using FinkiChattery.Persistence.Repositories.Contracts;
    56using FinkiChattery.Persistence.UnitOfWork;
    67using System;
     8using System.Linq;
    79using System.Threading;
    810using System.Threading.Tasks;
     
    1012namespace FinkiChattery.Commands.Questioning
    1113{
    12     public class AnswerQuestionCommand : ICommand<Guid>
     14    public class AnswerQuestionCommand : ICommand<AnswerQuestionStateDto>
    1315    {
    1416        public AnswerQuestionCommand(Guid questionUid, string text)
     
    2224    }
    2325
    24     public class AnswerQuestionHandler : ICommandHandler<AnswerQuestionCommand, Guid>
     26    public class AnswerQuestionHandler : ICommandHandler<AnswerQuestionCommand, AnswerQuestionStateDto>
    2527    {
    2628        public AnswerQuestionHandler(IUnitOfWork unitOfWork, ICurrentUser currentUser, IEventService eventService)
     
    3537        public IEventService EventService { get; }
    3638
    37         public async Task<Guid> Handle(AnswerQuestionCommand request, CancellationToken cancellationToken)
     39        public async Task<AnswerQuestionStateDto> Handle(AnswerQuestionCommand request, CancellationToken cancellationToken)
    3840        {
    3941            var question = await UnitOfWork.Questions.GetByUidAsync(request.QuestionUid);
     
    5254            EventService.Enqueue(new QuestionAnsweredEvent(question.Uid, answer.Uid, student.Uid));
    5355
    54             return answer.Uid;
     56            return new AnswerQuestionStateDto(answer.Id,
     57                                              answer.Uid,
     58                                              answer.Text,
     59                                              answer.CorrectAnswer,
     60                                              answer.CreatedOn,
     61                                              answer.VotesCount,
     62                                              new AnswerStudentQuestionStateDto(student.Id,
     63                                                                                student.Uid,
     64                                                                                student.IndexNumber,
     65                                                                                student.ImageUrl,
     66                                                                                student.Reputation),
     67                                              Enumerable.Empty<AnswerResponseQuestionStateDto>());
    5568        }
    5669    }
Note: See TracChangeset for help on using the changeset viewer.