public async Task <IActionResult> Index() // EngineerVM engineerVM { var allBooks = await _repo.GetBooks(); //var allBooks = await _repo.GetBooks(); // tuka treba AutoMapper var allBooksDto = new List <BookListDto>(); foreach (var book in allBooks) { BookListDto BookListDto = new BookListDto() { Id = book.Id, Author = book.Author, Title = book.Title, Pages = book.Pages }; allBooksDto.Add(BookListDto); } // do tuka so Mapp return(Ok(allBooksDto)); }
public void TestEagerBookListDtoOk() { //SETUP var options = SqliteInMemory.CreateOptions <EfCoreContext>(); using (var context = new EfCoreContext(options)) { context.Database.EnsureCreated(); context.SeedDatabaseFourBooks(); } using (var context = new EfCoreContext(options)) { //ATTEMPT var firstBook = context.Books .Include(r => r.AuthorsLink) .ThenInclude(r => r.Author) .Include(r => r.Reviews) .Include(r => r.Promotion) .First(); var dto = new BookListDto { BookId = firstBook.BookId, Title = firstBook.Title, Price = firstBook.Price, ActualPrice = firstBook //#A .Promotion?.NewPrice //#A ?? firstBook.Price, //#A PromotionPromotionalText = firstBook //#A .Promotion?.PromotionalText, //#A AuthorsOrdered = string.Join(", ", //#B firstBook.AuthorsLink //#B .OrderBy(l => l.Order) //#B .Select(a => a.Author.Name)), //#B ReviewsCount = firstBook.Reviews.Count, //#C //The test on there being any reviews is needed because of bug in EF Core V2.0.0, issue #9516 ReviewsAverageVotes = //#D firstBook.Reviews.Count == 0 //#D ? null //#D : (double?)firstBook.Reviews //#D .Average(q => q.NumStars) //#D }; //VERIFY dto.BookId.ShouldNotEqual(0); dto.Title.ShouldNotBeNull(); dto.Price.ShouldNotEqual(0); dto.AuthorsOrdered.ShouldNotBeNull(); /********************************************************* #A Notice the use of ?. This returns null if Promotion is null, otherwise it returns the property #B This orders the authors' names by the Order and then extracts their names #C We simply count the number of reviews #D EF Core turns the LINQ average into the SQL AVG command that runs on the database * *******************************************************/ } }
private IReadOnlyList <BookListDto> ProcessQuery(IEnumerable <Book> entities) { return(entities.Select(x => { var item = new BookListDto { Isbn = x.Isbn, Author = x.Author, Title = x.Title, Year = x.Year, Publisher = x.Publisher, CreationTime = x.CreationTime, Id = x.Id }; return item; }).ToList()); }
public async Task <IActionResult> ProbaSearch() { int[] int_arr = { 60487999, 60489222 }; var tagIds = new List <int>(); tagIds.AddRange(int_arr); var returnBooks = new List <Book>(); var Books = await _repo.FoundBooks(tagIds); foreach (var searchTag in tagIds) { foreach (var book in Books) { foreach (var booktag in book.BookTags) { if (booktag.TagId == searchTag) { returnBooks.Add(book); } } break; } } var dtoBooks = new List <BookListDto>(); foreach (var book in returnBooks) { var newBookDto = new BookListDto() { Id = book.Id, Pages = book.Pages, Title = book.Title, }; dtoBooks.Add(newBookDto); } return(Ok(dtoBooks)); }
public async Task <ServiceResult <BookListDto> > GetAllBooks(PagingParams pagingParams) { try { IQueryable <BookDto> bookList; if (pagingParams.AuthorName == null) { bookList = _unitOfWork.Repository <Book>().GetAll() .Include(c => c.BookAuthors) .ThenInclude(v => v.Author) .Select(b => new BookDto { Title = b.Title, Description = b.Description, Pages = b.Pages, Year = b.Year, AuthorsName = b.BookAuthors.Select(ba => ba.Author.Name).ToList() }); } else { var authorExist = await _unitOfWork.Repository <Author>().GetAll() .AnyAsync(b => b.Name == pagingParams.AuthorName); if (!authorExist) { return(Error <BookListDto>("There is not such Author Name")); } bookList = _unitOfWork.Repository <Book>().GetAll() .Include(c => c.BookAuthors) .ThenInclude(v => v.Author) .Where(t => t.BookAuthors.Any(b => b.Author.Name == pagingParams.AuthorName)) .Select(b => new BookDto { Title = b.Title, Description = b.Description, Pages = b.Pages, Year = b.Year, AuthorsName = b.BookAuthors.Select(ba => ba.Author.Name).ToList() }); } var paginatedList = await PaginatedList <BookDto> .CreateAsync(bookList, pagingParams.PageNumber, pagingParams.PageSize); var paginationDto = new PaginationDto { HasNextPage = paginatedList.HasNextPage, HasPreviousPage = paginatedList.HasPreviousPage, TotalPages = paginatedList.TotalPages, PageIndex = paginatedList.PageIndex }; var bookListDto = new BookListDto { Books = paginatedList, Pagination = paginationDto }; return(Success(bookListDto)); } catch { return(Error <BookListDto>("Can't receive all books")); } }
private void RedirectToData(BookListDto book) { this.Navigation.NavigateTo($"/books/{book.Id}"); }