source: src/FinkiChattery/FinkiChattery.Commands/Questioning/EditAnswer/EditAnswerCommand.cs

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

Edit answer and answer response

  • Property mode set to 100644
File size: 2.6 KB
Line 
1using FinkiChattery.Common.Mediator.Contracs;
2using FinkiChattery.Common.Mediator.Interfaces;
3using FinkiChattery.Common.User;
4using FinkiChattery.Persistence.Repositories.Contracts;
5using FinkiChattery.Persistence.UnitOfWork;
6using System;
7using System.Linq;
8using System.Threading;
9using System.Threading.Tasks;
10
11namespace FinkiChattery.Commands.Questioning
12{
13 public class EditAnswerCommand : ICommand<AnswerQuestionStateDto>
14 {
15 public EditAnswerCommand(Guid questionUid, Guid answerUid, string text)
16 {
17 QuestionUid = questionUid;
18 AnswerUid = answerUid;
19 Text = text;
20 }
21
22 public Guid QuestionUid { get; }
23 public Guid AnswerUid { get; }
24 public string Text { get; }
25 }
26
27 public class EditAnswerHandler : ICommandHandler<EditAnswerCommand, AnswerQuestionStateDto>
28 {
29 public EditAnswerHandler(IUnitOfWork unitOfWork, ICurrentUser currentUser, IEventService eventService)
30 {
31 UnitOfWork = unitOfWork;
32 CurrentUser = currentUser;
33 EventService = eventService;
34 }
35
36 public IUnitOfWork UnitOfWork { get; }
37 public ICurrentUser CurrentUser { get; }
38 public IEventService EventService { get; }
39
40 public async Task<AnswerQuestionStateDto> Handle(EditAnswerCommand request, CancellationToken cancellationToken)
41 {
42 var answer = await UnitOfWork.Answers.GetByUidAsync(request.AnswerUid);
43 var student = await UnitOfWork.Students.GetStudent(CurrentUser.Id);
44
45 answer.Text = request.Text;
46
47 await UnitOfWork.SaveAsync();
48
49 return new AnswerQuestionStateDto(answer.Id,
50 answer.Uid,
51 answer.Text,
52 answer.CorrectAnswer,
53 answer.CreatedOn,
54 answer.VotesCount,
55 new AnswerStudentQuestionStateDto(student.Id,
56 student.Uid,
57 student.IndexNumber,
58 student.ImageUrl,
59 student.Reputation),
60 Enumerable.Empty<AnswerResponseQuestionStateDto>());
61 }
62 }
63}
Note: See TracBrowser for help on using the repository browser.