private async Task ValidateTakeBookAsync(BookTakeDto bookDto, MobileBookOfficeLogsDto officeBookWithLogs) { var applicationUser = await _userDbSet.FirstOrDefaultAsync(u => u.Id == bookDto.ApplicationUserId); _serviceValidator.ThrowIfUserDoesNotExist(applicationUser); _serviceValidator.ThrowIfBookDoesNotExist(officeBookWithLogs != null); _serviceValidator.ChecksIfUserHasAlreadyBorrowedSameBook(officeBookWithLogs?.LogsUserIDs, bookDto.ApplicationUserId); _serviceValidator.ThrowIfBookIsAlreadyBorrowed(officeBookWithLogs); }
public async Task TakeBookAsync(int bookOfficeId, UserAndOrganizationDto userAndOrg) { var bookDto = new BookTakeDto { ApplicationUserId = userAndOrg.UserId, BookOfficeId = bookOfficeId, OrganizationId = userAndOrg.OrganizationId }; await TakeBookAsync(bookDto); }
public void Should_Throw_On_Take_Book_When_No_Users_Exist() { MockBooksByOffice(); _userDbSet.SetDbSetDataForAsync(new List <ApplicationUser>()); var bookTake = new BookTakeDto { ApplicationUserId = "testUser1", BookOfficeId = 1, OrganizationId = 2 }; _validationService .When(x => x.ThrowIfUserDoesNotExist(null)) .Do(_ => { throw new BookException("ThrowIfUserDoesNotExist"); }); Assert.ThrowsAsync <BookException>(async() => await _bookService.TakeBookAsync(bookTake)); }
public void Should_Add_Correct_Data_To_Logs_On_Take_Book() { MockCreateNewBook(); MockBooksByOffice(); MockApplicationUsers(); MockDeleteBookEntities(); var takeBook = new BookTakeDto { ApplicationUserId = "testUser1", BookOfficeId = 1, OrganizationId = 2 }; _bookService.TakeBookAsync(takeBook); _bookLogsDbSet.Received().Add(Arg.Is <BookLog>(b => b.OrganizationId == 2 && b.ApplicationUserId == "testUser1" && b.OrganizationId == 2)); }
public async Task TakeBookAsync(BookTakeDto bookDto) { MobileBookOfficeLogsDto officeBookWithLogs; await _takeBookLock.WaitAsync(); try { officeBookWithLogs = await _bookOfficesDbSet .Include(b => b.Book) .Include(b => b.BookLogs) .Where(b => b.OrganizationId == bookDto.OrganizationId && b.Id == bookDto.BookOfficeId) .Select(MapOfficeBookWithLogsToDto()) .FirstOrDefaultAsync(); await ValidateTakeBookAsync(bookDto, officeBookWithLogs); await BorrowBookAsync(officeBookWithLogs, bookDto); } finally { _takeBookLock.Release(); } var book = new TakenBookDto { UserId = bookDto.ApplicationUserId, OrganizationId = bookDto.OrganizationId, BookOfficeId = bookDto.BookOfficeId }; if (officeBookWithLogs != null) { book.OfficeId = officeBookWithLogs.OfficeId; book.Author = officeBookWithLogs.Author; book.Title = officeBookWithLogs.Title; } _asyncRunner.Run <IBooksNotificationService>(async notifier => await notifier.SendEmailAsync(book), _uow.ConnectionName); }
private async Task BorrowBookAsync(MobileBookOfficeLogsDto officeBookWithLogs, BookTakeDto bookDto) { var bookLog = new BookLog { ApplicationUserId = bookDto.ApplicationUserId, BookOfficeId = officeBookWithLogs.BookOfficeId, ModifiedBy = bookDto.ApplicationUserId, Modified = DateTime.UtcNow, TakenFrom = DateTime.UtcNow, Created = DateTime.UtcNow, CreatedBy = bookDto.ApplicationUserId, OrganizationId = bookDto.OrganizationId }; _bookLogsDbSet.Add(bookLog); await _uow.SaveChangesAsync(false); }