private async Task GiveBookToFirstReservation(string bookId, string userId) { if (bookId == null) { throw new ArgumentException("Invalid parameters: book id cannot be null."); } if (userId == null) { throw new ArgumentException("Invalid parameters: user id cannot be null."); } //remove old history registry var oldHistoryRegistry = await _context.HistoryRegistries.FirstAsync(hr => hr.BookId == bookId).ConfigureAwait(false); _context.HistoryRegistries.Remove(oldHistoryRegistry); await _context.SaveChangesAsync().ConfigureAwait(false); var historyRegistry = new HistoryRegistry() { BookId = bookId, UserId = userId, CheckOutDate = DateTime.Now.ToShortDateString(), ReturnDate = DateTime.Now.AddDays(10) }; if (historyRegistry == null) { throw new ArgumentException("Invalid operation: history registry cannot be null."); } _context.HistoryRegistries.Add(historyRegistry); await _context.SaveChangesAsync().ConfigureAwait(false); }
public async Task <HistoryRegistry> CheckoutBookAsync(string bookId, string userId) { if (bookId == null) { throw new ArgumentException("Invalid checkout parameters: book id cannot be null."); } if (userId == null) { throw new ArgumentException("Invalid checkout parameters: user id cannot be null."); } var historyRegistry = new HistoryRegistry() { BookId = bookId, UserId = userId, CheckOutDate = DateTime.Now.ToShortDateString(), ReturnDate = DateTime.Now.AddDays(10) }; if (historyRegistry == null) { throw new ArgumentException("Invalid operation: history registry cannot be null."); } _context.HistoryRegistries.Add(historyRegistry); var book = await _context.Books.FirstAsync(x => x.Id == bookId).ConfigureAwait(false); book.IsCheckedOut = true; //avlb. copies prop. of this book decr. with 1 in each book await _context.Books.Where(b => b.Title == book.Title && b.Author.Name == book.Author.Name && b.Language == book.Language).ForEachAsync(bc => bc.Copies--).ConfigureAwait(false); await _context.SaveChangesAsync().ConfigureAwait(false); return(historyRegistry); }
private void ChangeToReturned(HistoryRegistry historyRegistryToChange) { historyRegistryToChange.IsReturned = true; }