public ActionResult EditRent() { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); BooksRepository booksRepository = new BooksRepository(context); CustomersRepository customersRepository = new CustomersRepository(context); RentsEditRentVM model = new RentsEditRentVM(); model.Customers = customersRepository.GetAll(); model.Books = booksRepository.GetAll(); model.RentDate = DateTime.Now.Date; model.UserID = AuthenticationManager.LoggedUser.ID; return(View(model)); }
public ActionResult EditRent(string customerInfo, string bookInfo, RentsEditRentVM model) { LibraryManagementSystemContext context = new LibraryManagementSystemContext(); RentsRepository rentsRepository = new RentsRepository(context); BooksRepository booksRepository = new BooksRepository(context); CustomersRepository customersRepository = new CustomersRepository(context); // makes an customer info array in format {Personal No.}{Empty}{Empty}{First name}{Last name} string[] customerInfoSplitted = customerInfo.Split(' ', '-'); // makes an book info array in format {Barcode No.}{Empty}{Empty}{Book title} string[] bookInfoSplitted = bookInfo.Split(' ', '-'); if (string.IsNullOrEmpty(customerInfo) || customerInfoSplitted[0] == "") { ModelState.AddModelError("CustomerPersonalNumber", "* personal No. required"); } if (string.IsNullOrEmpty(bookInfo) || bookInfoSplitted[0] == "") { ModelState.AddModelError("BookBarcodeNumber", "* barcode required"); } if (!ModelState.IsValid) { if (model.ID <= 0) { model.RentDate = DateTime.Now; } model.Customers = customersRepository.GetAll(); model.Books = booksRepository.GetAll(); return(View(model)); } model.CustomerPersonalNumber = int.Parse(customerInfoSplitted[0]); model.Customer = customersRepository .GetAll(filter: c => c.PersonalNumber == model.CustomerPersonalNumber) .FirstOrDefault(); model.BookBarcodeNumber = int.Parse(bookInfoSplitted[0]); model.Books = booksRepository .GetAll(filter: b => b.Barcodes.Any(bc => bc.BarcodeNumber == model.BookBarcodeNumber)); if (model.Books.Any(b => b.StockCount > 0)) { Rent rent = new Rent(); rent.Books = new List <Book>(); rent.Books = model.Books; rent.Books.FirstOrDefault().StockCount--; rent.DateToReturn = DateTime.Now.AddMonths(1); rent.RentDate = model.RentDate; rent.UserID = model.UserID; rent.CustomerID = model.Customer.ID; rentsRepository.Save(rent); } else { ModelState.AddModelError("BookBarcodeNumber", "* book not in stock at the moment"); if (model.ID <= 0) { model.RentDate = DateTime.Now; } model.Customers = customersRepository.GetAll(); model.Books = booksRepository.GetAll(); return(View(model)); } return(RedirectToAction("Index", "Rents")); }