public override void Process(TagHelperContext context, TagHelperOutput output) { var allEditions = editionService.GetAllEditions(); output.TagName = "select"; output.Attributes.SetAttribute("id", For.Name); output.Attributes.SetAttribute("name", For.Name); output.Attributes.Add("class", "form-control"); foreach (var edition in allEditions) { TagBuilder myOption = new TagBuilder("option") { TagRenderMode = TagRenderMode.Normal }; myOption.Attributes.Add("value", edition.Name); myOption.InnerHtml.Append(edition.Name); output.Content.AppendHtml(myOption); } }
private List <BookResource> GetBooks() { var authorIdQuery = Request.Query.AuthorId; var bookIdsQuery = Request.Query.BookIds; var slugQuery = Request.Query.TitleSlug; var includeAllAuthorBooksQuery = Request.Query.IncludeAllAuthorBooks; if (!Request.Query.AuthorId.HasValue && !bookIdsQuery.HasValue && !slugQuery.HasValue) { var books = _bookService.GetAllBooks(); var authors = _authorService.GetAllAuthors().ToDictionary(x => x.AuthorMetadataId); var editions = _editionService.GetAllEditions().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList()); foreach (var book in books) { book.Author = authors[book.AuthorMetadataId]; if (editions.TryGetValue(book.Id, out var bookEditions)) { book.Editions = bookEditions; } else { book.Editions = new List <Edition>(); } } return(MapToResource(books, false)); } if (authorIdQuery.HasValue) { int authorId = Convert.ToInt32(authorIdQuery.Value); var books = _bookService.GetBooksByAuthor(authorId); var author = _authorService.GetAuthor(authorId); var editions = _editionService.GetEditionsByAuthor(authorId) .GroupBy(x => x.BookId) .ToDictionary(x => x.Key, y => y.ToList()); foreach (var book in books) { book.Author = author; if (editions.TryGetValue(book.Id, out var bookEditions)) { book.Editions = bookEditions; } else { book.Editions = new List <Edition>(); } } return(MapToResource(books, false)); } if (slugQuery.HasValue) { string titleSlug = slugQuery.Value.ToString(); var book = _bookService.FindBySlug(titleSlug); if (book == null) { return(MapToResource(new List <Book>(), false)); } if (includeAllAuthorBooksQuery.HasValue && Convert.ToBoolean(includeAllAuthorBooksQuery.Value)) { return(MapToResource(_bookService.GetBooksByAuthor(book.AuthorId), false)); } else { return(MapToResource(new List <Book> { book }, false)); } } string bookIdsValue = bookIdsQuery.Value.ToString(); var bookIds = bookIdsValue.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(e => Convert.ToInt32(e)) .ToList(); return(MapToResource(_bookService.GetBooks(bookIds), false)); }
// GET: Editions public ActionResult Index() { IEnumerable <Editions> allEditions = editionService.GetAllEditions(); return(View(allEditions)); }
public List <BookResource> GetBooks([FromQuery] int?authorId, [FromQuery] List <int> bookIds, [FromQuery] string titleSlug, [FromQuery] bool includeAllAuthorBooks = false) { if (!authorId.HasValue && !bookIds.Any() && titleSlug.IsNullOrWhiteSpace()) { var books = _bookService.GetAllBooks(); var authors = _authorService.GetAllAuthors().ToDictionary(x => x.AuthorMetadataId); var editions = _editionService.GetAllEditions().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList()); foreach (var book in books) { book.Author = authors[book.AuthorMetadataId]; if (editions.TryGetValue(book.Id, out var bookEditions)) { book.Editions = bookEditions; } else { book.Editions = new List <Edition>(); } } return(MapToResource(books, false)); } if (authorId.HasValue) { var books = _bookService.GetBooksByAuthor(authorId.Value); var author = _authorService.GetAuthor(authorId.Value); var editions = _editionService.GetEditionsByAuthor(authorId.Value) .GroupBy(x => x.BookId) .ToDictionary(x => x.Key, y => y.ToList()); foreach (var book in books) { book.Author = author; if (editions.TryGetValue(book.Id, out var bookEditions)) { book.Editions = bookEditions; } else { book.Editions = new List <Edition>(); } } return(MapToResource(books, false)); } if (titleSlug.IsNotNullOrWhiteSpace()) { var book = _bookService.FindBySlug(titleSlug); if (book == null) { return(MapToResource(new List <Book>(), false)); } if (includeAllAuthorBooks) { return(MapToResource(_bookService.GetBooksByAuthor(book.AuthorId), false)); } else { return(MapToResource(new List <Book> { book }, false)); } } return(MapToResource(_bookService.GetBooks(bookIds), false)); }