public override async Task <UpdateBookPageRequest> HandleAsync(UpdateBookPageRequest command, CancellationToken cancellationToken = new CancellationToken())
        {
            var book = await _bookRepository.GetBookById(command.LibraryId, command.BookPage.BookId, command.AccountId, cancellationToken);

            if (book == null)
            {
                throw new BadRequestException();
            }

            var existingBookPage = await _bookPageRepository.GetPageBySequenceNumber(command.LibraryId, command.BookPage.BookId, command.BookPage.SequenceNumber, cancellationToken);

            if (existingBookPage == null)
            {
                command.Result.BookPage = await _bookPageRepository.AddPage(command.LibraryId, command.BookPage.BookId, command.BookPage.SequenceNumber, command.BookPage.Text, 0, command.BookPage.ChapterId, cancellationToken);

                command.Result.HasAddedNew = true;
            }
            else
            {
                command.Result.BookPage = await _bookPageRepository.UpdatePage(command.LibraryId, command.BookPage.BookId, command.BookPage.SequenceNumber, command.BookPage.Text, existingBookPage.ImageId ?? 0, command.BookPage.Status, command.BookPage.ChapterId, cancellationToken);
            }

            var previousPage = await _bookPageRepository.GetPageBySequenceNumber(command.LibraryId, command.BookPage.BookId, command.SequenceNumber - 1, cancellationToken);

            var nextPage = await _bookPageRepository.GetPageBySequenceNumber(command.LibraryId, command.BookPage.BookId, command.SequenceNumber + 1, cancellationToken);

            command.Result.BookPage.PreviousPage = previousPage;
            command.Result.BookPage.NextPage     = nextPage;

            return(await base.HandleAsync(command, cancellationToken));
        }
Exemple #2
0
        public override async Task <UploadBookPages> HandleAsync(UploadBookPages command, CancellationToken cancellationToken = new CancellationToken())
        {
            var pageNumber = await _bookPageRepository.GetLastPageNumberForBook(command.LibraryId, command.BookId, cancellationToken);

            IEnumerable <FileModel> files = new List <FileModel>();

            if (command.Files.Count() == 1 && command.Files.Single().MimeType == MimeTypes.Pdf)
            {
                var pages = _pdfConverter.ExtractImagePages(command.Files.Single().Contents);
                files = pages.Select(p => new FileModel()
                {
                    Contents = p.Value,
                    FileName = p.Key,
                    MimeType = MimeTypes.Jpg
                });
            }
            else if (command.Files.Count() == 1 && (command.Files.Single().MimeType == MimeTypes.Zip || command.Files.Single().MimeType == MimeTypes.CompressedFile))
            {
                files = _zipOpener.ExtractImages(command.Files.Single().Contents);
            }
            else
            {
                files = command.Files;
            }

            foreach (var file in files)
            {
                var extension      = Path.GetExtension(file.FileName).Trim('.');
                var sequenceNumber = ++pageNumber;
                var url            = await AddImageToFileStore(command.BookId, sequenceNumber, $"{sequenceNumber:0000}.{extension}", file.Contents, file.MimeType, cancellationToken);

                var fileModel = await _fileRepository.AddFile(new FileModel
                {
                    IsPublic    = false,
                    FilePath    = url,
                    DateCreated = DateTime.UtcNow,
                    FileName    = file.FileName,
                    MimeType    = file.MimeType
                }, cancellationToken);

                var bookPage = await _bookPageRepository.AddPage(command.LibraryId, command.BookId, pageNumber, string.Empty, fileModel.Id, null, cancellationToken);
            }

            return(await base.HandleAsync(command, cancellationToken));
        }
Exemple #3
0
        public override async Task <AddBookPageRequest> HandleAsync(AddBookPageRequest command, CancellationToken cancellationToken = new CancellationToken())
        {
            var book = await _bookRepository.GetBookById(command.LibraryId, command.BookPage.BookId, command.AccountId, cancellationToken);

            if (book == null)
            {
                throw new BadRequestException();
            }

            var existingBookPage = await _bookPageRepository.GetPageBySequenceNumber(command.LibraryId, command.BookPage.BookId, command.BookPage.SequenceNumber, cancellationToken);

            if (existingBookPage == null)
            {
                command.Result = await _bookPageRepository.AddPage(command.LibraryId, command.BookPage.BookId, command.BookPage.SequenceNumber, command.BookPage.Text, 0, command.BookPage.ChapterId, cancellationToken);

                command.IsAdded = true;
            }
            else
            {
                command.Result = await _bookPageRepository.UpdatePage(command.LibraryId, command.BookPage.BookId, command.BookPage.SequenceNumber, command.BookPage.Text, 0, command.BookPage.Status, command.BookPage.ChapterId, cancellationToken);
            }

            return(await base.HandleAsync(command, cancellationToken));
        }