public async Task <ActionResult> DeleteVideoAsync([BindRequired] Guid videoId) { try { var result = await _videoService.GetVideoByIdAsync(videoId); if (result == null) { return(NotFound($"Video with id {videoId} not found")); } string fileName = result.FileName; if (!string.IsNullOrEmpty(fileName)) { var fileToDelete = Path.Combine(Directory.GetCurrentDirectory() + "\\videos\\" + fileName); if (System.IO.File.Exists(fileToDelete)) { System.IO.File.Delete(fileToDelete); } } await _videoService.DeleteVideoAsync(videoId); return(StatusCode(StatusCodes.Status200OK, "Video deleted succesfull")); } catch { return(StatusCode(StatusCodes.Status500InternalServerError, "Error when try to delete video")); } }
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); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var chapterRepository = _unitOfWork.Repository <Chapter>(); var spec = new ChapterWithChapterVideosAndLessonSpecification(request.ChapterId); var chapter = await chapterRepository.GetEntityWithSpecificationAsync(spec); if (chapter == null) { throw new RestException(HttpStatusCode.NotFound); } var chapterVideo = chapter.ChapterVideos.FirstOrDefault(x => x.Id == request.ChapterVideoId); if (chapterVideo != null && string.IsNullOrEmpty(chapterVideo.PublicId)) { throw new RestException(HttpStatusCode.NotFound); } var result = await _videoService.DeleteVideoAsync(chapterVideo.PublicId); if (!result) { throw new Exception(ExceptionMessages.ProblemDeletingVideo); } chapter.ChapterVideos.Remove(chapterVideo); var success = await _unitOfWork.CompleteAsync() > 0; if (success) { return(Unit.Value); } throw new Exception(ExceptionMessages.ProblemSavingChanges); }