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, "Chapter not found");
                }

                var videoUploadResult =
                    await _videoService.UploadVideoAsync(request.File, "OnlineEducation/ChapterVideos");

                var chapterVideo = new ChapterVideo
                {
                    PublicId  = videoUploadResult.PublicId,
                    Url       = videoUploadResult.Url,
                    CreatedAt = videoUploadResult.CreatedAt,
                };

                chapter.ChapterVideos.Add(chapterVideo);

                var success = await _unitOfWork.CompleteAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception(ExceptionMessages.ProblemSavingChanges);
            }
Beispiel #2
0
            public async Task <ChapterWithChapterVideosDto> Handle(Query request,
                                                                   CancellationToken cancellationToken)
            {
                var chapterRepository = _unitOfWork.Repository <Chapter>();

                var spec     = new ChapterWithChapterVideosAndLessonSpecification(request.ChapterId);
                var chapters = await chapterRepository.GetEntityWithSpecificationAsync(spec);

                var mappedData =
                    _mapper.Map <Chapter, ChapterWithChapterVideosDto>(
                        chapters);

                return(mappedData);
            }
            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);
            }