public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var examQuestionRepository = _unitOfWork.Repository <ExamQuestion>(); var examQuestionWithVideosSpecification = new ExamQuestionWithVideosSpecification(request.ExamQuestionId); var examQuestion = await examQuestionRepository.GetEntityWithSpecificationAsync(examQuestionWithVideosSpecification); if (examQuestion == null) { throw new RestException(HttpStatusCode.NotFound, "Exam question not found"); } var videoUploadResult = await _videoService.UploadVideoAsync(request.File, "OnlineEducation/VideoAnswerForExamQuestion"); var videoAnswerForExamQuestion = new VideoAnswerForExamQuestion { Url = videoUploadResult.Url, CreatedAt = videoUploadResult.CreatedAt, PublicId = videoUploadResult.PublicId, }; examQuestion.VideoAnswerForExamQuestion = videoAnswerForExamQuestion; var success = await _unitOfWork.CompleteAsync() > 0; if (success) { return(Unit.Value); } throw new Exception(ExceptionMessages.ProblemSavingChanges); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var examQuestionRepository = _unitOfWork.Repository <ExamQuestion>(); var videoAnswerForExamQuestionRepository = _unitOfWork.Repository <VideoAnswerForExamQuestion>(); var examQuestionWithVideosSpecification = new ExamQuestionWithVideosSpecification(request.ExamQuestionId); var examQuestion = await examQuestionRepository.GetEntityWithSpecificationAsync(examQuestionWithVideosSpecification); if (examQuestion == null) { throw new RestException(HttpStatusCode.NotFound, "Exam question not found"); } if (examQuestion.VideoAnswerForExamQuestion == null) { return(Unit.Value); } var deleteResult = await _videoService.DeleteVideoAsync(examQuestion.VideoAnswerForExamQuestion.PublicId); if (!deleteResult) { throw new Exception(ExceptionMessages.ProblemDeletingVideo); } videoAnswerForExamQuestionRepository.Delete(examQuestion.VideoAnswerForExamQuestion); examQuestion.VideoAnswerForExamQuestion = null; var success = await _unitOfWork.CompleteAsync() > 0; if (success) { return(Unit.Value); } throw new Exception(ExceptionMessages.ProblemSavingChanges); }