public async Task <IActionResult> GetCategories(int libraryId, CancellationToken token = default(CancellationToken))
        {
            var query      = new GetCategoriesQuery(libraryId);
            var categories = await _queryProcessor.ExecuteAsync(query, cancellationToken : token);

            return(new OkObjectResult(_categoryRenderer.Render(categories, libraryId)));
        }
Exemple #2
0
        public BookView Render(BookModel source, int libraryId)
        {
            var result = source.Map();
            var links  = new List <LinkView>
            {
                _linkRenderer.Render(new Link {
                    ActionName = nameof(BookController.GetBookById),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Self,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }),
                _linkRenderer.Render(new Link {
                    ActionName = nameof(AuthorController.GetAuthorById),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Author,
                    Parameters = new { libraryId = libraryId, authorId = source.Authors.First().Id }
                }),
                _linkRenderer.Render(new Link {
                    ActionName = nameof(ChapterController.GetChaptersByBook),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Chapters,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }),
                _linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookPageController.GetPagesByBook),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Pages,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                })
            };

            if (source.SeriesId.HasValue)
            {
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(SeriesController.GetSeriesById),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Series,
                    Parameters = new { libraryId = libraryId, seriesId = source.SeriesId }
                }));
            }

            if (!string.IsNullOrWhiteSpace(source.ImageUrl) && _fileStorage.SupportsPublicLink)
            {
                links.Add(new LinkView {
                    Href   = _fileStorage.GetPublicUrl(source.ImageUrl),
                    Method = "GET",
                    Rel    = RelTypes.Image,
                    Accept = MimeTypes.Jpg
                });
            }
            else if (source.ImageId.HasValue)
            {
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(FileController.GetFile),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Image,
                    Parameters = new { fileId = source.ImageId.Value }
                }));
            }

            if (_userHelper.IsWriter(libraryId) || _userHelper.IsAdmin || _userHelper.IsLibraryAdmin(libraryId))
            {
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookController.UpdateBook),
                    Method     = HttpMethod.Put,
                    Rel        = RelTypes.Update,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookController.DeleteBook),
                    Method     = HttpMethod.Delete,
                    Rel        = RelTypes.Delete,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookController.UpdateBookImage),
                    Method     = HttpMethod.Put,
                    Rel        = RelTypes.ImageUpload,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookController.CreateBookContent),
                    Method     = HttpMethod.Post,
                    Rel        = RelTypes.AddFile,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(ChapterController.CreateChapter),
                    Method     = HttpMethod.Post,
                    Rel        = RelTypes.CreateChapter,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookPageController.CreatePage),
                    Method     = HttpMethod.Post,
                    Rel        = RelTypes.AddPages,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));

                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookPageController.UploadPages),
                    Method     = HttpMethod.Post,
                    Rel        = RelTypes.MultiCreate,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));
            }

            if (_userHelper.IsAdmin || _userHelper.IsLibraryAdmin(libraryId))
            {
                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookController.BindBook),
                    Method     = HttpMethod.Get,
                    Rel        = RelTypes.Download,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));

                links.Add(_linkRenderer.Render(new Link
                {
                    ActionName = nameof(BookController.PublishBook),
                    Method     = HttpMethod.Post,
                    Rel        = RelTypes.Publish,
                    Parameters = new { libraryId = libraryId, bookId = source.Id }
                }));
            }

            if (_userHelper.IsAuthenticated)
            {
                if (source.IsFavorite)
                {
                    links.Add(_linkRenderer.Render(new Link
                    {
                        ActionName = nameof(BookController.RemoveBookFromFavorites),
                        Method     = HttpMethod.Delete,
                        Rel        = RelTypes.RemoveFavorite,
                        Parameters = new { libraryId = libraryId, bookId = source.Id }
                    }));
                }
                else
                {
                    links.Add(_linkRenderer.Render(new Link
                    {
                        ActionName = nameof(BookController.AddBookToFavorites),
                        Method     = HttpMethod.Post,
                        Rel        = RelTypes.CreateFavorite,
                        Parameters = new { libraryId = libraryId, bookId = source.Id }
                    }));
                }
            }

            result.Links = links;

            if (source.Authors.Any())
            {
                var authors = new List <AuthorView>();
                foreach (var author in source.Authors)
                {
                    authors.Add(_authorRenderer.Render(author, source.LibraryId));
                }

                result.Authors = authors;
            }

            if (source.Categories != null)
            {
                var categories = new List <CategoryView>();
                foreach (var category in source.Categories)
                {
                    categories.Add(_categoryRenderer.Render(category, source.LibraryId));
                }

                result.Categories = categories;
            }

            if (source.Contents.Any())
            {
                var contents = new List <BookContentView>();
                foreach (var content in source.Contents)
                {
                    contents.Add(Render(content, source.LibraryId));
                }

                result.Contents = contents;
            }

            return(result);
        }