public ActionResult Create(CategoryAddViewModel newCategory) { try { using (var dbContext = new MvclibraryEntities()) { int?rootId = null; if (newCategory.SelectedCategoryId != null) { rootId = int.Parse(newCategory.SelectedCategoryId); } var category = new category { Name = newCategory.Name, RootCategoryId = rootId }; dbContext.Entry(category).State = EntityState.Added; dbContext.SaveChanges(); return(RedirectToAction("Index")); } } catch (Exception e) { return(View()); } }
public ActionResult Create(MessageViewModel collection) { try { if (ModelState.IsValid) { using (var dbContext = new MvclibraryEntities()) { var message = new message { Title = collection.Title, Contnet = collection.Contnet, DateCreated = DateTime.Now }; dbContext.Entry(message).State = EntityState.Added; dbContext.SaveChanges(); return(RedirectToAction("Index", "Home")); } } // TODO: Add insert logic here return(View(collection)); } catch { return(View()); } }
public ActionResult Create(BookAddViewModel newBook) { try { if (ModelState.IsValid) { using (var dbContext = new MvclibraryEntities()) { if (dbContext.Books.Where(b => b.ISBN == newBook.ISBN).FirstOrDefault() != null) { ModelState.AddModelError("BookExist", "Książka o podanym ISBN juz istnieje"); var s = dbContext.category.ToList(); newBook.CategoryList = s.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }); return(View(newBook)); } var book = new Books { Author = newBook.Author, Status = "Dostepna", Title = newBook.Title, ISBN = newBook.ISBN, CategoryId = int.Parse(newBook.SelectedCategoryId), DateCreatead = DateTime.Now }; dbContext.Entry(book).State = EntityState.Added; dbContext.SaveChanges(); return(RedirectToAction("Index")); } } using (var dbContext = new MvclibraryEntities()) { var s = dbContext.category.ToList(); newBook.CategoryList = s.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }); } return(View(newBook)); } catch { return(View(newBook)); } }
public ActionResult Verify(int id) { using (var dbContext = new MvclibraryEntities()) { var us = dbContext.User.Where(u => u.Id == id).FirstOrDefault(); us.IsUserVerified = true; dbContext.Entry(us).State = EntityState.Modified; dbContext.SaveChanges(); return(RedirectToAction("UsersToVerify")); } }
public ActionResult History(Cart cart, string value) { try { var cartViewModel = new CartBookViewModel(); cartViewModel.SerchWord = value; using (var dbContext = new MvclibraryEntities()) { var userEmail = User.Identity.Name; var user = dbContext.User.Where(u => u.Email == userEmail).FirstOrDefault(); var history = new History { Word = value, UserID = user.Id }; dbContext.Entry(history).State = EntityState.Added; TempData["Message"] = "Zostało zapisane do bazy"; TempData["Status"] = "true"; List <Books> books = null; if (value != null) { books = dbContext.Books.Where(b => b.ISBN.Contains(value) || b.Title.Contains(value) || b.Author.Contains(value)).ToList(); } else { books = dbContext.Books.ToList(); } var bookvm = new List <BookViewModel>(); foreach (var item in books) { cartViewModel.BookViewModels.Add(new BookViewModel { Author = item.Author, ISBN = item.ISBN, status = item.Status, NameCategory = dbContext.category.Where(c => c.Id == item.CategoryId).FirstOrDefault().Name, Title = item.Title, Id = item.Id }); } dbContext.SaveChanges(); cartViewModel.cart = cart; return(View("Index", cartViewModel)); } } catch { return(View()); } }
public ActionResult LendBooks(Cart cart) { string url = this.Request.UrlReferrer.AbsolutePath; CartServic cartService = new CartServic(); var cartViewModel = cartService.GetCartViewModel(); var userEmail = User.Identity.Name; using (var dbContext = new MvclibraryEntities()) { var user = dbContext.User.Where(u => u.Email == userEmail).FirstOrDefault(); var lends = dbContext.Lend.ToList(); var userLends = lends.Where(l => l.UserId == user.Id && (l.State == "Odebrano" || l.State == "Na półce czytelnika")).ToList(); if (userLends.Count + cart.books.Count <= user.BorrowLimit) { foreach (var item in cart.books) { var lend = new Lend { BookId = item.Id, UserId = user.Id, DateBorrowed = DateTime.Now, State = "Na półce czytelnika", DateReturn = DateTime.Now.AddDays(14) }; item.Status = "Wypożyczona"; dbContext.Entry(item).State = EntityState.Modified; dbContext.Lend.Add(lend); } dbContext.SaveChanges(); TempData["Message"] = "Książki czekają na Twój odbiór"; TempData["Status"] = "true"; } else { TempData["Message"] = "Masz już wypożyczone " + userLends.Count + " ksiązki. Twój limit wynosi " + user.BorrowLimit; TempData["Status"] = "false"; } } cart.books.Clear(); cartViewModel.cart = cart; return(Redirect(url)); }
public ActionResult Borrow(int id) { string url = this.Request.UrlReferrer.AbsolutePath; using (var dbContext = new MvclibraryEntities()) { var lend = dbContext.Lend.Where(l => l.Id == id).FirstOrDefault(); lend.State = "Odebrano"; dbContext.Entry(lend).State = EntityState.Modified; dbContext.SaveChanges(); return(Redirect(url)); } }
public ActionResult Edit(BookAddViewModel bvm) { try { // TODO: Add update logic here if (ModelState.IsValid) { using (var dbContext = new MvclibraryEntities()) { var books = dbContext.Books.ToList(); if (books.Where(b => b.ISBN == bvm.ISBN && b.Id != bvm.Id).FirstOrDefault() != null) { ModelState.AddModelError("BookExist", "Książka o podanym ISBN juz istnieje"); bvm.CategoryList = dbContext.category.ToList().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }); return(View(bvm)); } var editBook = dbContext.Books.Where(b => b.Id == bvm.Id).FirstOrDefault(); editBook.ISBN = bvm.ISBN; editBook.Title = bvm.Title; editBook.Author = bvm.Author; editBook.CategoryId = int.Parse(bvm.SelectedCategoryId); dbContext.Entry(editBook).State = EntityState.Modified; dbContext.SaveChanges(); return(RedirectToAction("Index")); } } using (var dbContext = new MvclibraryEntities()) { bvm.CategoryList = dbContext.category.ToList().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }); return(View(bvm)); } } catch { return(View()); } }
public ActionResult Edit(CategoryAddViewModel c) { try { using (var dbContext = new MvclibraryEntities()) { var s = dbContext.category.ToList(); var editCategory = dbContext.category.Where(b => b.Id == c.Id).FirstOrDefault(); editCategory.Name = c.Name; int?rootId = null; if (c.SelectedCategoryId != null) { rootId = int.Parse(c.SelectedCategoryId); } editCategory.RootCategoryId = rootId; dbContext.Entry(editCategory).State = EntityState.Modified; dbContext.SaveChanges(); } return(RedirectToAction("Index")); } catch (Exception e) { using (var dbContext = new MvclibraryEntities()) { var s = dbContext.category.ToList(); var editCategory = dbContext.category.Where(ce => ce.Id == c.Id).FirstOrDefault(); var zvm = new CategoryAddViewModel { Id = editCategory.Id, Name = editCategory.Name, SelectedCategoryId = editCategory.RootCategoryId.ToString() }; zvm.CategoryList = s.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }); return(View(zvm)); } } }
public ActionResult Return(int id) { string url = this.Request.UrlReferrer.AbsolutePath; using (var dbContext = new MvclibraryEntities()) { var lend = dbContext.Lend.Where(l => l.Id == id).FirstOrDefault(); lend.State = "Zwrócono"; dbContext.Entry(lend).State = EntityState.Modified; var book = dbContext.Books.Where(b => b.Id == lend.BookId).FirstOrDefault(); book.Status = "Dostepna"; dbContext.Entry(book).State = EntityState.Modified; dbContext.SaveChanges(); return(Redirect(url)); } }
public ActionResult Edit(EditUserViewModel collection) { try { if (ModelState.IsValid) { using (var dbContext = new MvclibraryEntities()) { var us = dbContext.User.Where(u => u.Id == collection.Id).FirstOrDefault(); us.FirstName = collection.FirstName; us.LastName = collection.LastName; us.Email = collection.Email; us.Role = collection.Role; us.BorrowLimit = collection.BorrowLimit; us.WaitLimit = collection.WaitLimit; dbContext.Entry(us).State = EntityState.Modified; dbContext.SaveChanges(); } return(RedirectToAction("Index")); } Dictionary <string, string> Role = new Dictionary <string, string>(); Role.Add("Worker", "Worker"); Role.Add("User", "User"); collection.Rols = Role.Select(x => new SelectListItem { Value = x.Key, Text = x.Value }); return(View(collection)); } catch { return(View()); } }