public bool EliminateBook(BookCriteria pCriteriaKey, String pCriteriaValue) { bool mBoolSuccess = false; String mStrSQL = ""; String[] ArrCriteria = GetDBFieldByCriteria(pCriteriaKey, pCriteriaValue); mStrSQL = "DELETE FROM BOOKS " + "WHERE " + ArrCriteria[cCRITERIAKEY] + " = " + ArrCriteria[cCRITERIAVALUE]; try { oConnection.OpenConnection(); oConnection.UpdateSQL(mStrSQL, null); mBoolSuccess = true; } catch (Exception ex) { throw ex; } finally { oConnection.CloseConnection(); } return mBoolSuccess; }
public List <Book> GetAllBooksSortedByName(BookCriteria criteria) { return(GetFullGraph() .Where(x => string.IsNullOrEmpty(criteria.Title) || string.Equals(x.Title, criteria.Title, StringComparison.InvariantCultureIgnoreCase)) .OrderBy(x => x.Title).ToList()); }
public async Task GetBooksAsync_TitleBookSortAscending_ReturnedBooksSortedByTitleDescending() { // Arrange var books = new List <Book>() { new Book() { Title = "a" }, new Book() { Title = "b" } }; await this.Target.AddBooksAsync(books); // Act var criteria = new BookCriteria() { Page = 1, PageSize = int.MaxValue, Sort = BookSort.Title, SortDirection = SortDirection.Descending }; var actual = await this.Target.GetBooksAsync(criteria); // Assert BookAssert.AreEqual(books.OrderByDescending(b => b.Title), actual.Items); }
public async Task GetBooksAsync_FilterTextThatShouldMatch_MatchingBookReturned( string filter) { var expected = new Book() { Title = "Title", Authors = new List <Author> { new Author() { FirstName = "John", LastName = "Smith", MiddleName = "Carver" } } }; // Arrange var books = new List <Book>() { expected, new Book() { Title = "xxxxx", Authors = new List <Author> { new Author() { FirstName = "xxxxx", LastName = "xxxxx", MiddleName = "xxxxx" } } }, }; await this.Target.AddBooksAsync(books); // Act var criteria = new BookCriteria() { Page = 1, PageSize = int.MaxValue, FilterText = filter }; var actual = await this.Target.GetBooksAsync(criteria); // Assert Assert.AreEqual( 1, actual.Items.Count(), $"Expected exactly one book to be returned for filter text '{filter}'. Actual number of books returned was {actual.Items.Count()}."); BookAssert.AreEqual(expected, actual.Items.Single()); }
public async Task GetBooksAsync_RatingBookSortDescending_ReturnedBooksSortedByRatingDescending() { // Arrange var books = new List <Book>() { new Book() { Title = "a", Reviews = new List <BookReview>() { new BookReview() { Rating = 1 } } }, new Book() { Title = "b", Reviews = new List <BookReview>() { new BookReview() { Rating = 2 } } }, new Book() { Title = "c", Reviews = new List <BookReview>() { new BookReview() { Rating = null } } } }; await this.Target.AddBooksAsync(books); // Act var criteria = new BookCriteria() { Page = 1, PageSize = int.MaxValue, Sort = BookSort.Rating, SortDirection = SortDirection.Descending }; var actual = await this.Target.GetBooksAsync(criteria); // Assert BookAssert.AreEqual(books.OrderByDescending(b => b.Reviews.Single().Rating), actual.Items); }
public async Task <PagedEnumerable <Book> > Get(int?page, int?pageSize, BookSort?sort, SortDirection?sortDirection, string filter = null) { var criteria = new BookCriteria() { Page = page ?? 1, PageSize = pageSize ?? 12, Sort = sort ?? BookSort.None, SortDirection = sortDirection ?? SortDirection.Ascending, FilterText = filter }; return(await this.books.GetBooksAsync(criteria)); }
public async Task GetBooksAsync_NumberOfItemsLargerThanPageSize_PageSizeNumberOfItemsReturned() { // Arrange var books = this.Fixture.Build <Book>() .CreateMany(4); await this.Target.AddBooksAsync(books); // Act var criteria = new BookCriteria() { Page = 1, PageSize = 2 }; var actual = await this.Target.GetBooksAsync(criteria); // Assert Assert.AreEqual(2, actual.Items.Count()); }
public Task <PagedEnumerable <Book> > GetBooksAsync(BookCriteria criteria) { var filteredBooks = Books.AsEnumerable(); var pagination = new Pagination() { Page = criteria.Page, PageSize = criteria.PageSize }; if (!string.IsNullOrEmpty(criteria.FilterText)) { filteredBooks = filteredBooks.Where(b => b.Title.IndexOf(criteria.FilterText, StringComparison.CurrentCultureIgnoreCase) >= 0 || b.Authors.Any(a => a.FirstName.IndexOf(criteria.FilterText, StringComparison.CurrentCultureIgnoreCase) >= 0 || a.LastName.IndexOf(criteria.FilterText, StringComparison.CurrentCultureIgnoreCase) >= 0)); } filteredBooks = filteredBooks.Skip((criteria.Page - 1) * criteria.PageSize).Take(criteria.PageSize); var result = new PagedEnumerable <Book>(filteredBooks, pagination); return(Task.FromResult(result)); }
public async Task GetBooksAsync_LastPage_ItemFromLastPageReturned() { // Arrange var books = this.Fixture.Build <Book>() .CreateMany(3); await this.Target.AddBooksAsync(books); // Act var criteria = new BookCriteria() { Page = 2, PageSize = 2 }; var actual = await this.Target.GetBooksAsync(criteria); // Assert var expected = books.Last(); BookAssert.AreEqual(expected, actual.Items.Single()); }
private String[] GetDBFieldByCriteria(BookCriteria pCriteriaKey, String pCriteriaValue) { String[] ArrCriteriaField = new String[2]; switch (pCriteriaKey) { case BookCriteria.cBOOKID: ArrCriteriaField[cCRITERIAKEY] = "BOOKID"; ArrCriteriaField[cCRITERIAVALUE] = pCriteriaValue; break; case BookCriteria.cBOOKNAME: ArrCriteriaField[cCRITERIAKEY] = "BOOKNAME"; ArrCriteriaField[cCRITERIAVALUE] = "'" + pCriteriaValue + "'"; break; case BookCriteria.cBOOKAUTHOR: ArrCriteriaField[cCRITERIAKEY] = "BOOKAUTHOR"; ArrCriteriaField[cCRITERIAVALUE] = "'" + pCriteriaValue + "'"; break; case BookCriteria.cBOOKISBN: ArrCriteriaField[cCRITERIAKEY] = "BOOKISBN"; ArrCriteriaField[cCRITERIAVALUE] = "'" + pCriteriaValue + "'"; break; case BookCriteria.cBOOKDESCRIPTION: ArrCriteriaField[cCRITERIAKEY] = "BOOKDESCRIPTION"; ArrCriteriaField[cCRITERIAVALUE] = "'" + pCriteriaValue + "'"; break; } return ArrCriteriaField; }
public String[,] ListBooksByCriteria(BookCriteria pCriteriaKey, String pCriteriaValue) { String[,] ArrBooks; String[] ArrCriteria = GetDBFieldByCriteria(pCriteriaKey, pCriteriaValue); String mStrSQL = ""; mStrSQL = "SELECT BOOKID, BOOKNAME, BOOKAUTHOR, BOOKISBN, BOOKDESCRIPTION " + "FROM BOOKS " + "WHERE " + ArrCriteria[cCRITERIAKEY] + " = " + ArrCriteria[cCRITERIAVALUE]; try { oConnection.OpenConnection(); ArrBooks = oConnection.QuerySQLResultFlatArray(mStrSQL); } catch (Exception ex) { throw ex; } finally { oConnection.CloseConnection(); } return ArrBooks; }
public async Task <PagedEnumerable <Book> > GetBooksAsync(BookCriteria criteria) { if (criteria.Page <= 0) { throw new ArgumentOutOfRangeException(nameof(criteria), criteria.Page, "The page number must be at least 1."); } if (criteria.PageSize <= 0) { throw new ArgumentOutOfRangeException(nameof(criteria), criteria.PageSize, "The page size must at least be 1."); } var books = await this.GetBooksAsync(); if (!string.IsNullOrEmpty(criteria.FilterText)) { books = books.Where(b => b.Title?.Contains(criteria.FilterText, StringComparison.OrdinalIgnoreCase) == true || b.Authors.Any(a => a.FirstName?.Contains(criteria.FilterText, StringComparison.OrdinalIgnoreCase) == true || a.LastName?.Contains(criteria.FilterText, StringComparison.OrdinalIgnoreCase) == true || a.MiddleName?.Contains(criteria.FilterText, StringComparison.OrdinalIgnoreCase) == true)); } if (criteria.Sort != BookSort.None) { Func <Book, object> sortSelector; switch (criteria.Sort) { case BookSort.Rating: sortSelector = b => b.Reviews.SingleOrDefault()?.Rating ?? 0; break; case BookSort.Title: sortSelector = b => b.Title; break; default: throw new NotSupportedException($"Unknown sort field {criteria.Sort}."); } if (criteria.SortDirection == SortDirection.Ascending) { books = books.OrderBy(sortSelector); } else { books = books.OrderByDescending(sortSelector); } } var count = books.Count(); var pagination = new Pagination(criteria.Page, criteria.PageSize, count); var offset = (criteria.Page - 1) * criteria.PageSize; books = books.Skip(offset).Take(criteria.PageSize); return(new PagedEnumerable <Book>(books, pagination)); }
public async Task <PagedResult <Book> > GetPagedAsync(BookCriteria criteria) { var result = await _bookRepository.GetPagedAsync(criteria); return(result); }
public async Task <PagedResult <Book> > GetPagedAsync(BookCriteria criteria) { var data = await _db.Books.GetPagedAsync(criteria.Page, criteria.PageSize); return(data); }
public async Task <IActionResult> Get([FromQuery] BookCriteria criteria) { var result = await _bookService.GetPagedAsync(criteria); return(Ok(result)); }