public IActionResult OnPost(int id) { if (!ModelState.IsValid) { return(this.Page()); } if (this.BorrowBookBindingModel.StartDate > this.BorrowBookBindingModel.EndDate) { return(this.Page()); } var borrower = this._context .Borrowers .FirstOrDefault(x => x.Name.Equals(BorrowBookBindingModel.BorrowerName)); if (borrower == null) { borrower = new BookLibrary.Models.Borrower { Name = BorrowBookBindingModel.BorrowerName }; this._context.Borrowers.Add(borrower); this._context.SaveChanges(); } var book = this._context.Books.Find(id); if (book == null) { return(this.Page()); } this._context.BookBorrowerses.Add(new BookBorrowers { BookId = book.Id, BorrowDate = this.BorrowBookBindingModel.StartDate, BorrowerId = borrower.Id, ReturnDate = this.BorrowBookBindingModel.EndDate }); book.IsBorrowed = true; this._context.SaveChanges(); return(RedirectToPage("/Book/Details", new { id = id })); }
public IActionResult Borrow(BorrowBindingModel model, int id) { if (!ModelState.IsValid || model.StartDate > model.EndDate) { return(this.View()); } var borrower = this._context .Borrowers .FirstOrDefault(x => x.Name.Equals(model.BorrowerName)); if (borrower == null) { borrower = new BookLibrary.Models.Borrower { Name = model.BorrowerName }; this._context.Borrowers.Add(borrower); this._context.SaveChanges(); } var movie = this._context.Movies.Find(id); if (movie == null) { return(this.View()); } this._context.MovieBorrowerses.Add(new MovieBorrowers() { MovieId = movie.Id, BorrowDate = model.StartDate, BorrowerId = borrower.Id, ReturnDate = model.EndDate }); movie.IsBorrowed = true; this._context.SaveChanges(); return(RedirectToAction("Details", "Movie", new { id = id })); }