1 | using FinkiChattery.Common.Mediator.Contracs;
|
---|
2 | using FinkiChattery.Common.Mediator.Interfaces;
|
---|
3 | using FinkiChattery.Common.User;
|
---|
4 | using FinkiChattery.Persistence.Repositories.Contracts;
|
---|
5 | using FinkiChattery.Persistence.UnitOfWork;
|
---|
6 | using System;
|
---|
7 | using System.Linq;
|
---|
8 | using System.Threading;
|
---|
9 | using System.Threading.Tasks;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|