public async Task <CommandResult> ExecuteAsync(List <int> bookIds) { var userId = int.Parse(_httpContext?.User?.UserId()); List <int> listBooKInCartExisted = await _bookCartRepository.TableNoTracking .Where(x => x.UserId == userId && bookIds.Contains(x.BookId) && (x.Status == (int)BookStatus.InOrder || x.Status == (int)BookStatus.Waiting)) .Select(x => x.BookId) .ToListAsync(); List <int> listBooKInCartNew = bookIds.Where(x => !listBooKInCartExisted.Contains(x)).ToList(); if (!listBooKInCartNew.Any()) { return(CommandResult.Success); } List <BookCarts> entities = new List <BookCarts>(); listBooKInCartNew.ForEach(x => { BookCarts entity = new BookCarts(); entity.BookId = x; entity.UserId = userId; entity.Status = (int)BookStatus.InOrder; entities.Add(entity); }); await _bookCartRepository.InsertAsync(entities); return(CommandResult.SuccessWithData(entities)); }
public async Task GetListBookInCart_Test() { var bookCarts1 = new BookCarts { Id = 1, UserId = 1, BookId = 1, Status = (int)BookStatus.InOrder, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCarts1); var bookCarts2 = new BookCarts { Id = 2, UserId = 1, BookId = 2, Status = (int)BookStatus.Pending, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCarts2); var bookCarts3 = new BookCarts { Id = 3, UserId = 1, BookId = 3, Status = (int)BookStatus.Waiting, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCarts3); await _dbContext.SaveChangesAsync(); var bookCartRepository = new EfRepository <BookCarts>(_dbContext); var bookRepository = new EfRepository <Books>(_dbContext); var libraryRepository = new EfRepository <Libraries>(_dbContext); var getListBookInCartQuery = new GetListBookInCartQuery(bookCartRepository, _httpContextAccessor); var bookInCart = await getListBookInCartQuery.ExecuteAsync(); Assert.NotNull(bookInCart); Assert.Equal(2, bookInCart.Count); }
public async Task <CommandResult> ExecuteAsync(int bookId) { var userId = int.Parse(_httpContext?.User?.UserId()); BookCarts entity = await _bookCartRepository.TableNoTracking .Where(x => x.UserId == userId && x.BookId == bookId).FirstOrDefaultAsync(); if (entity == null) { return(CommandResult.Failed(new CommandResultError() { Code = (int)HttpStatusCode.NotFound, Description = "Data not found" })); } await _bookCartRepository.DeleteAsync(entity); return(CommandResult.Success); }
public async Task AddBookToCartCommandTest_Test() { var bookCarts1 = new BookCarts { Id = 10, UserId = 1, BookId = 1, Status = (int)BookStatus.InOrder, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCarts1); var bookCarts2 = new BookCarts { Id = 20, UserId = 1, BookId = 2, Status = (int)BookStatus.Waiting, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCarts2); await _dbContext.SaveChangesAsync(); var bookCartRepository = new EfRepository <BookCarts>(_dbContext); var addBookToCartCommand = new AddBookToCartCommand(bookCartRepository, _httpContextAccessor); List <int> bookIds = new List <int>() { 3, 4 }; var result = await addBookToCartCommand.ExecuteAsync(bookIds); Assert.True(result.Succeeded); }
public async Task GetBookInCartForBorrow_Test() { var bookCart = new BookCarts { Id = 1, UserId = 1, BookId = 1, Status = (int)BookStatus.InOrder, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCart); var book = new Books { Id = 1, BookName = "Book", BookCode = "ABC", LibraryId = 1, CategoryId = 1, SupplierId = 1, PublisherId = 1, Author = "Author", AmountAvailable = 10, Amount = 20, Enabled = true, MaximumDateBorrow = 30 }; _dbContext.Set <Books>().Add(book); var library = new Libraries { Id = 1, Name = "library", Enabled = true }; _dbContext.Set <Libraries>().Add(library); await _dbContext.SaveChangesAsync(); var bookCartRepository = new EfRepository <BookCarts>(_dbContext); var bookRepository = new EfRepository <Books>(_dbContext); var libraryRepository = new EfRepository <Libraries>(_dbContext); var getBookInCartForBorrowQuery = new GetBookInCartForBorrowQuery(bookCartRepository, bookRepository, libraryRepository, _httpContextAccessor); var bookInCartBorrow = await getBookInCartForBorrowQuery.ExecuteAsync(); Assert.NotNull(bookInCartBorrow); Assert.Single(bookInCartBorrow); BookInCartDetailViewModel bookInCart = bookInCartBorrow.FirstOrDefault(); Assert.Equal(book.Id, bookInCart.BookId); Assert.Equal(book.BookCode, bookInCart.BookCode); Assert.Equal(book.BookCode, bookInCart.BookCode); Assert.Equal(book.BookName, bookInCart.BookName); Assert.Equal(book.Author, bookInCart.Author); Assert.Equal(book.Amount, bookInCart.Amoun); Assert.Equal(book.AmountAvailable, bookInCart.AmountAvailable); Assert.Equal(book.MaximumDateBorrow, bookInCart.MaximumDateBorrow); Assert.Equal(bookCart.ModifiedDate, bookInCart.ModifiedDate); Assert.Equal(bookCart.Status, bookInCart.Status); Assert.Equal(library.Name, bookInCart.LibraryName); Assert.Equal(DateTime.Now.Date.AddDays(book.MaximumDateBorrow + 1), bookInCart.ReturnDate); }
public async Task GetBookInCartDetailNoData_Test() { var library = new Libraries { Id = 1, Name = "library", Enabled = true }; _dbContext.Set <Libraries>().Add(library); var book1 = new Books { Id = 1, BookName = "Book1", LibraryId = 1, CategoryId = 1, SupplierId = 1, PublisherId = 1, AmountAvailable = 10, Amount = 20, Enabled = true }; _dbContext.Set <Books>().Add(book1); var book2 = new Books { Id = 2, BookName = "Book2", LibraryId = 1, CategoryId = 1, SupplierId = 1, PublisherId = 1, AmountAvailable = 10, Amount = 20, Enabled = true }; _dbContext.Set <Books>().Add(book2); var bookCarts1 = new BookCarts { Id = 1, UserId = 1, BookId = 1, Status = (int)BookStatus.InOrder, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCarts1); var bookCarts2 = new BookCarts { Id = 2, UserId = 1, BookId = 2, Status = (int)BookStatus.Waiting, ModifiedDate = DateTime.Now }; _dbContext.Set <BookCarts>().Add(bookCarts2); await _dbContext.SaveChangesAsync(); var bookCartRepository = new EfRepository <BookCarts>(_dbContext); var bookRepository = new EfRepository <Books>(_dbContext); var libraryRepository = new EfRepository <Libraries>(_dbContext); var getBookInCartDetailQuery = new GetBookInCartDetailQuery(bookCartRepository, bookRepository, libraryRepository, _httpContextAccessor); var bookInCart = await getBookInCartDetailQuery.ExecuteAsync(); Assert.NotNull(bookInCart); Assert.Equal(2, bookInCart.Count); }