public void Return(Book book, User user) { Reservation reservationToReturn = db.Reservations .Where(reservation => reservation.BookId == book.Id && reservation.EndDate == null).SingleOrDefault(); if (reservationToReturn != null) { var userId = GetUserId(user.Name); if (reservationToReturn.UserId != userId) { throw new System.InvalidOperationException("You cannot return a book that someone else borrowed"); } else { reservationToReturn.EndDate = System.DateTime.Now.ToString(); db.SaveChanges(); } } else { throw new System.InvalidOperationException("You cannot return a book that has not been borrowed"); } }
public void Add(Reservation entity) { Book relevantBook = _dbContext.Books .OrderBy(b => b.id) .Where(b => b.id == entity.book.id) .SingleOrDefault(); if (relevantBook == null) { throw new InvalidOperationException("That book doesn't exist"); } entity.book = relevantBook; if (reservationValid(entity)) { _dbContext.Reservations.Add(entity); _dbContext.SaveChanges(); } else { throw new InvalidOperationException("Book already reserved at that time"); }; }
public FilledBookRepository(ILibraryContext dbContext) { _dbContext = dbContext; if (_dbContext.Books.Count() == 0) { _dbContext.Books.Add(new Book { title = "Tennis", isbn = "11111", author = "Andy Murray", publishDate = "01/01/2001" }); _dbContext.Books.Add(new Book { title = "All about tennis", isbn = "22222", author = "Tim Henman", publishDate = "02/02/2002" }); _dbContext.Books.Add(new Book { title = "Yeah tennis", isbn = "33333", author = "Boris Becker", publishDate = "03/03/2003" }); } _dbContext.SaveChanges(); }
public void Add(Book entity) { _dbContext.Books.Add(entity); _dbContext.SaveChanges(); }
public void Add(Book entity) { db.Books.Add(entity); db.SaveChanges(); }
public void SaveChanges() { _db.SaveChanges(); }
public void SaveChanges() { LibraryDb.SaveChanges(); }