Example #1
0
        public async Task ThrowExeptionWhenBookLendingIsNullRenewBook_Test()
        {
            var isbn     = "TestIsbn";
            var username = "******";
            var days     = 5;

            var renewBookDto = new RenewBookDto();
            var lendDto      = new BaseTitleDto();

            var options = TestUtilities.GetOptions(nameof(ThrowExeptionWhenBookLendingIsNullRenewBook_Test));

            using (var actContext = new LibrarySystemContext(options))
            {
                var book = await actContext.Books.AddAsync(new Book { ISBN = isbn });

                var user = await actContext.Users.AddAsync(new User { UserName = username });

                await actContext.SaveChangesAsync();

                renewBookDto.BookId = days;
                renewBookDto.BookId = book.Entity.Id;
                renewBookDto.UserId = user.Entity.Id;
            }

            using (var assertContext = new LibrarySystemContext(options))
            {
                var sut = new BookWebService(assertContext);
                await sut.RenewBookAsync(renewBookDto);

                var actuaDays = assertContext.BookLendings.Select(book => book.Date);
            }
        }
        public async Task <bool> ReserveBookAsync(BaseTitleDto baseTitleDto)
        {
            var book = await this.dbContext.Books
                       .FirstOrDefaultAsync(bTitle => bTitle.Title == baseTitleDto.Title);

            var user = await this.dbContext.Users
                       .Include(uLend => uLend.BookReservations)
                       .SingleOrDefaultAsync(uId => uId.Id == baseTitleDto.UserId);

            if (book == null)
            {
                return(false);
            }
            var bookReservation = new BookReservation()
            {
                Book = book,
                User = user,
                Date = DateTime.Now
            };


            await this.dbContext.BookReservations.AddAsync(bookReservation);

            await this.dbContext.SaveChangesAsync();

            return(true);
        }
Example #3
0
        public async Task LendBook_Test()
        {
            var title       = "TestTitle";
            var username    = "******";
            var lendBookDto = new BaseTitleDto();

            var options = TestUtilities.GetOptions(nameof(LendBook_Test));


            using (var actContext = new LibrarySystemContext(options))
            {
                await actContext.Books.AddAsync(new Book { Title = title });

                var user = await actContext.Users.AddAsync(new User { UserName = username });

                await actContext.SaveChangesAsync();

                lendBookDto.Title  = title;
                lendBookDto.UserId = user.Entity.Id;
            }


            using (var assertContext = new LibrarySystemContext(options))
            {
                var sut = new BookWebService(assertContext);
                await sut.LendBookAsync(lendBookDto);

                Assert.AreEqual(1, assertContext.BookLendings.Count());
            }
        }
Example #4
0
        public async Task ReturnFalseWhenBookIsNullReserveBook_Test()
        {
            string title          = null;
            var    username       = "******";
            var    reserveBookDto = new BaseTitleDto();

            var options = TestUtilities.GetOptions(nameof(ReturnFalseWhenBookIsNullReserveBook_Test));

            using (var actContext = new LibrarySystemContext(options))
            {
                var user = await actContext.Users.AddAsync(new User { UserName = username });

                await actContext.SaveChangesAsync();

                reserveBookDto.Title  = title;
                reserveBookDto.UserId = user.Entity.Id;
            }


            using (var assertContext = new LibrarySystemContext(options))
            {
                var sut         = new BookWebService(assertContext);
                var bookReserve = await sut.ReserveBookAsync(reserveBookDto);

                Assert.IsFalse(bookReserve);
            }
        }
Example #5
0
        public async Task ThrowExeptionWhenBookIdIsNullReturnBook_Test()
        {
            var title     = "TestTitle";
            var username  = "******";
            var reviewDto = new ReviewDto();
            var lendDto   = new BaseTitleDto();

            var options = TestUtilities.GetOptions(nameof(ThrowExeptionWhenBookIdIsNullReturnBook_Test));

            using (var actContext = new LibrarySystemContext(options))
            {
                var book = await actContext.Books.AddAsync(new Book { Title = title });

                var user = await actContext.Users.AddAsync(new User { UserName = username });

                await actContext.SaveChangesAsync();

                reviewDto.UserId = user.Entity.Id;

                lendDto.UserId = user.Entity.Id;

                var bookLending = new BookWebService(actContext);
                await bookLending.LendBookAsync(lendDto);
            }

            using (var assertContext = new LibrarySystemContext(options))
            {
                var sut = new BookWebService(assertContext);
                await sut.ReturnBookAsync(reviewDto);
            }
        }
        public async Task LendBookAsync(BaseTitleDto baseTitleDto)
        {
            var book = await this.dbContext.Books
                       .FirstOrDefaultAsync(bTitle => bTitle.Title == baseTitleDto.Title);

            var user = await this.dbContext.Users
                       .Include(uLend => uLend.BookLendings)
                       .SingleOrDefaultAsync(uId => uId.Id == baseTitleDto.UserId);

            if (book == null)
            {
                throw new BookException("The book was not found!");
            }


            if (user.CountLendBooks < minLendedBooks)
            {
                throw new Exception("You cannot take more than 3 books!");
            }

            var bookLending = await this.dbContext.BookLendings
                              .FirstOrDefaultAsync(bLending => bLending.BookId == book.Id &&
                                                   !bLending.ReturnDate.HasValue &&
                                                   bLending.Date.AddDays(10) > DateTime.Now); // This is enshures automatic return after 10 days


            if (bookLending != null)
            {
                throw new BookException("The book has already been lended!");
            }

            bookLending = new BookLending()
            {
                Book = book,
                User = user,
                Date = DateTime.Now
            };
            user.CountLendBooks -= 1;


            var bookReservation = await this.dbContext.BookReservations
                                  .Include(item => item.Notification)
                                  .FirstOrDefaultAsync(item => item.BookId == book.Id && item.UserId == user.Id && item.Active);

            if (bookReservation != null)
            {
                bookReservation.Active            = false;
                bookReservation.Notification.Seen = true;
            }

            await this.dbContext.BookLendings.AddAsync(bookLending);

            await this.dbContext.SaveChangesAsync();
        }
        public async Task <IActionResult> CheckMyBooks()
        {
            var baseDto = new BaseTitleDto
            {
                UserId = User.FindFirstValue(ClaimTypes.NameIdentifier)
            };

            var book = (await this.service.GetAllBookLendings(baseDto))
                       .Select(b => new SearchViewModel(b));

            var results = new SearchResultsViewModel(book);

            return(View("CheckMyBooks", results));
        }
        public async Task <IEnumerable <Book> > GetAllBookLendings(BaseTitleDto baseTitleDto)
        {
            var userId = await this.dbContext.Users
                         .FirstOrDefaultAsync(uId => uId.Id == baseTitleDto.UserId);

            var bookLending = await this.dbContext.BookLendings
                              .Include(u => u.User)
                              .Where(bLending => bLending.UserId == baseTitleDto.UserId)
                              .Select(book => book.Book)
                              .ToListAsync();

            if (bookLending == null)
            {
                throw new BookException("You do not have any lended books.");
            }
            return(bookLending);
        }
        public async Task <IActionResult> ReserveBook(BookTitleViewModel vm)
        {
            var reserveDto = new BaseTitleDto
            {
                Title  = vm.Title,
                UserId = User.FindFirstValue(ClaimTypes.NameIdentifier)
            };

            var result = await this.service.ReserveBookAsync(reserveDto);

            if (!result)
            {
                return(View("Message", new MessageViewModel {
                    Message = "Book was not found!"
                }));
            }

            return(View("Message", new MessageViewModel {
                Message = $"The book {vm.Title} was reserved successfully!", IsSuccess = true
            }));
        }
        public async Task <IActionResult> LendBook(BookTitleViewModel vm)
        {
            try
            {
                var lendDto = new BaseTitleDto
                {
                    Title  = vm.Title,
                    UserId = User.FindFirstValue(ClaimTypes.NameIdentifier)
                };

                await this.service.LendBookAsync(lendDto);
            }
            catch (BookException ex)
            {
                return(View("Message", new MessageViewModel {
                    Message = ex.Message
                }));
            }

            return(View("Message", new MessageViewModel {
                Message = $"The book {vm.Title} was lended successfully!", IsSuccess = true
            }));
        }
Example #11
0
        public async Task ReturnBook_Test()
        {
            var isbn      = "TestIsbn";
            var username  = "******";
            var reviewDto = new ReviewDto();
            var lendDto   = new BaseTitleDto();

            var options = TestUtilities.GetOptions(nameof(ReturnBook_Test));

            using (var actContext = new LibrarySystemContext(options))
            {
                var book = await actContext.Books.AddAsync(new Book { ISBN = isbn });

                var user = await actContext.Users.AddAsync(new User { UserName = username });

                await actContext.SaveChangesAsync();

                reviewDto.ISBN   = book.Entity.ISBN;
                reviewDto.BookId = book.Entity.Id;
                reviewDto.UserId = user.Entity.Id;

                lendDto.Title  = book.Entity.Title;
                lendDto.UserId = user.Entity.Id;

                var bookLending = new BookWebService(actContext);
                await bookLending.LendBookAsync(lendDto);
            }

            using (var assertContext = new LibrarySystemContext(options))
            {
                var sut = new BookWebService(assertContext);
                await sut.ReturnBookAsync(reviewDto);

                Assert.AreEqual(0, assertContext.BookLendings.Count());
            }
        }