public IActionResult Create(Book book) { if (ModelState.IsValid) { try { if (context.Books.Contains(book)) { context.Entry(book).State = Microsoft.EntityFrameworkCore.EntityState.Modified; } else { context.Books.Add(book); } context.SaveChanges(); } catch { return(View()); } return(RedirectToAction("Index")); } else { return(View()); } }
public async Task <IActionResult> PutBook(int id, Book book) { if (id != book.Id) { return(BadRequest()); } _context.Entry(book).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BookExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Index(int bookId, string category, string searchString, int resultsPerPage = 10, int page = 1) { var book = db.Books.Single(b => b.ID == bookId); BookCard bc; bool conflict = false; //no one is reading it? borrow! if (book.Reader == null) { bc = new BookCard { BookID = bookId, BorrowDate = DateTime.Now, Reader = User.Identity.Name }; book.Reader = User.Identity.Name; db.Cards.Add(bc); db.Entry(book).State = EntityState.Modified; } //user is the reader? return! else if (book.Reader == User.Identity.Name) { bc = db.Cards.Single(c => c.BookID == bookId && c.ReturnDate == null); bc.ReturnDate = DateTime.Now; book.Reader = null; db.Entry(bc).State = EntityState.Modified; db.Entry(book).State = EntityState.Modified; } else //this will probably only happen if the book was borrowed by someone else //after the page was loaded but before the button click { conflict = true; } db.SaveChanges(); if (Request.IsAjaxRequest()) { return(Content((HtmlHelpers.GetButton(bookId, book.Reader, User.Identity.Name, conflict)).ToHtmlString())); } var books = db.Books.Where(b => b.Status == true).ToList(); var model = BuildIndexViewModel(category, searchString, resultsPerPage, page, books); return(View(model)); }
public void Refresh() { var query = from r in db.Reservations where (r.EndOfReservation < DateTime.Now) select r; List <int> listID = new List <int>(); foreach (var item in query.ToList()) { listID.Add(item.BookID); db.Reservations.Remove(item); } db.SaveChanges(); var bookQry = from b in db.Books select b; foreach (var itm in bookQry.ToList()) { if (listID.Contains(itm.ID)) { itm.Quantity++; db.Entry(itm).State = EntityState.Modified; db.SaveChanges(); } } }
public ActionResult Edit([Bind(Include = "ID,Title,Author,Pages,Genre,Price")] Novel novel) { if (ModelState.IsValid) { db.Entry(novel).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(novel)); }
public ActionResult Edit([Bind(Include = "userID,ISBN,Name,Author,Published")] Books books) { if (ModelState.IsValid) { db.Entry(books).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(books)); }
public ActionResult Edit([Bind(Include = "userID,ISBN,imgPath")] Bookimage bookimage) { if (ModelState.IsValid) { db.Entry(bookimage).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(bookimage)); }