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 RenewBookAsync(RenewBookDto renewBookDto) { var book = await this.dbContext.Books. FirstOrDefaultAsync(bRenew => bRenew.Id == renewBookDto.BookId); var user = await this.dbContext.Users .Include(uLend => uLend.BookLendings) .SingleOrDefaultAsync(uName => uName.Id == renewBookDto.UserId); var bookLending = await this.dbContext.BookLendings .FirstOrDefaultAsync(bLending => bLending.BookId == book.Id && bLending.UserId == user.Id); if (bookLending == null) { throw new BookException("The book has not beed lended by the current user!"); } if (renewBookDto.Days > 10) { throw new BookException("You cannot renew book for more than 10 days."); } bookLending.Date.AddDays(renewBookDto.Days); await this.dbContext.SaveChangesAsync(); }
public async Task <IActionResult> RenewBook(RenewBookViewModel vm) { try { var renewDto = new RenewBookDto { BookId = vm.BookId, UserId = User.FindFirstValue(ClaimTypes.NameIdentifier), Days = vm.Days }; await this.service.RenewBookAsync(renewDto); } catch (BookException ex) { return(View("Message", new MessageViewModel { Message = ex.Message })); } return(View("Message", new MessageViewModel { Message = $"The book was renewed successfully for {vm.Days} days!", IsSuccess = true })); }