private void ChceckInstore(Book book) { int added = book.InStore; if (added > 0 && db.Reservations.Where(r => r.BookID == book.BookID).ToList().Count() > 0) { List<Reservation> reservations = db.Reservations.Where(r => r.BookID == book.BookID).ToList(); reservations = reservations.OrderBy(o => o.QueueNumber).ToList(); reservations = reservations.Take(added).ToList(); //int nr = 1; foreach (Reservation r in reservations) { Borrow borrow = new Borrow(); borrow.BookID = r.book.BookID; borrow.book = book; borrow.BorrowDate = DateTime.Now; borrow.ReturnDate = DateTime.Now; borrow.Fine = 0; borrow.User = r.User; borrow.Status = "Waiting"; db.Borrows.Add(borrow); db.Reservations.Remove(r); book.InStore--; //db.Books.Attach(dbBook); //dbBook.InStore = b.InStore - 1; db.Entry(book).State = EntityState.Modified; db.SaveChanges(); } reservations = db.Reservations.Where(r => r.BookID == book.BookID).ToList(); reservations = reservations.OrderBy(o => o.QueueNumber).ToList(); int nr = 1; foreach (Reservation r in reservations) { r.QueueNumber = nr; db.Entry(r).State = EntityState.Modified; db.SaveChanges(); nr++; } } }
public ActionResult DoBorrow() { if (Session["Cart" + User.Identity.Name] != null) { var db = new ApplicationDbContext(); List<Book> cart = (List<Book>)Session["Cart" + User.Identity.Name]; Session["Cart" + User.Identity.Name] = new List<Book>(); foreach (Book b in cart) { //Book dbBook = new Book { BookID = b.BookID }; Borrow borrow = new Borrow(); borrow.BookID = b.BookID; //borrow.book = b; dodawalo autorow borrow.BorrowDate = DateTime.Now; borrow.ReturnDate = DateTime.Now; borrow.Fine = 0; borrow.User = db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name); borrow.Status = "Waiting"; db.Borrows.Add(borrow); b.InStore--; //db.Books.Attach(dbBook); //dbBook.InStore = b.InStore - 1; db.Entry(b).State = EntityState.Modified; db.SaveChanges(); } db.SaveChanges(); TempData["notice"] = "Books will be ready soon!"; return RedirectToAction("Index"); } return View(); }