public List <LendViewModel> GetViewModel(List <Lend> lends) { var lendViewModel = new List <LendViewModel>(); using (var dbContext = new MvclibraryEntities()) { var books = dbContext.Books.ToList(); var users = dbContext.User.ToList(); foreach (var item in lends) { var book = books.Where(b => b.Id == item.BookId).FirstOrDefault(); var user = users.Where(u => u.Id == item.UserId).FirstOrDefault(); lendViewModel.Add( new LendViewModel { Id = item.Id, Author = book.Author, DateBorrowed = item.DateBorrowed ?? DateTime.Now, DateReturn = item.DateReturn ?? DateTime.Now.AddDays(14), State = item.State, Surname = user.LastName, Name = user.FirstName, ISBN = book.ISBN, Title = book.Title, Email = user.Email } ); } } return(lendViewModel); }
public ActionResult Index() { var homeViewModel = new HomeViewModel(); using (var dbContext = new MvclibraryEntities()) { var books = dbContext.Books.OrderByDescending(b => b.DateCreatead).Take(5).ToList(); var bookViewModel = new List <BookViewModel>(); foreach (var item in books) { bookViewModel.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 }); } var messages = dbContext.message.OrderByDescending(b => b.DateCreated).Take(3).ToList(); homeViewModel.messages = messages; homeViewModel.bookViewModel = bookViewModel; } return(View(homeViewModel)); }
public ActionResult Search(Cart cart, string value) { var cartViewModel = new CartBookViewModel(); cartViewModel.SerchWord = value; using (var dbContext = new MvclibraryEntities()) { 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 }); } cartViewModel.cart = cart; return(View("Index", cartViewModel)); } }
// GET: User/Edit/5 public ActionResult Edit(int id) { using (var dbContext = new MvclibraryEntities()) { var us = dbContext.User.Where(u => u.Id == id).FirstOrDefault(); EditUserViewModel zvm = new EditUserViewModel() { Id = id, BorrowLimit = us.BorrowLimit, Email = us.Email, FirstName = us.FirstName, LastName = us.LastName, WaitLimit = us.WaitLimit, Role = us.Role }; Dictionary <string, string> Role = new Dictionary <string, string>(); Role.Add("Worker", "Worker"); Role.Add("User", "User"); zvm.Rols = Role.Select(x => new SelectListItem { Value = x.Key, Text = x.Value }); return(View(zvm)); } }
public ActionResult AddBookToCart(Cart cart, int id) { string url = this.Request.UrlReferrer.AbsolutePath; CartServic cartService = new CartServic(); var cartViewModel = cartService.GetCartViewModel(); if (cart.books.Count < 5) { if (cart.books.Find(b => b.Id == id) == null) { using (var dbContext = new MvclibraryEntities()) { var books = dbContext.Books.ToList(); var book = books.Where(b => b.Id == id).FirstOrDefault(); cart.books.Add(book); } } else { TempData["Message"] = "Podana książka znajduje się już w koszyku"; TempData["Status"] = "false"; } } else { TempData["Message"] = "Nie można przekroczyć 5 książek w koszyku"; TempData["Status"] = "false"; } cartViewModel.cart = cart; return(Redirect(url)); }
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()); } }
// GET: Books/Edit/5 public ActionResult Edit(int id) { using (var dbContext = new MvclibraryEntities()) { var b = dbContext.Books.ToList(); var editBooks = dbContext.Books.Where(c => c.Id == id).FirstOrDefault(); var zvm = new BookAddViewModel { Id = editBooks.Id, SelectedCategoryId = editBooks.CategoryId.ToString(), Author = editBooks.Author, ISBN = editBooks.ISBN, Title = editBooks.Title }; zvm.CategoryList = dbContext.category.ToList().Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }); return(View(zvm)); } }
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 UsersToVerify() { using (var dbContext = new MvclibraryEntities()) { var users = dbContext.User.Where(u => u.Role == "User" && u.IsUserVerified == false).ToList(); return(View(users)); } }
public ActionResult Workers() { using (var dbContext = new MvclibraryEntities()) { var workers = dbContext.User.Where(u => u.Role == "Worker").ToList(); return(View("Index", workers)); } }
// GET: Category/Delete/5 public ActionResult Delete(int id) { using (var dbContext = new MvclibraryEntities()) { var s = dbContext.category.ToList(); return(RedirectToAction("Index")); } }
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)); } }
// GET: Lend public ActionResult Index() { using (var dbContext = new MvclibraryEntities()) { var lends = dbContext.Lend.ToList(); var lendServicew = new LendService(); var lendViewModel = lendServicew.GetViewModel(lends.OrderBy(o => o.DateBorrowed).ToList()); return(View(lendViewModel)); } }
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 Lend() { using (var dbContext = new MvclibraryEntities()) { var lends = dbContext.Lend.Where(l => l.State == "Odebrano").ToList(); var lendServicew = new LendService(); var lendViewModel = lendServicew.GetViewModel(lends.OrderBy(o => o.DateBorrowed).ToList()); return(View("Index", lendViewModel)); } }
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)); } }
// GET: Books/Create public ActionResult Create() { using (var dbContext = new MvclibraryEntities()) { var s = dbContext.category.ToList(); var zvm = new BookAddViewModel(); zvm.CategoryList = s.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name }); return(View(zvm)); } }
public ActionResult UserLEnd() { using (var dbContext = new MvclibraryEntities()) { var userEmail = User.Identity.Name; var user = dbContext.User.Where(u => u.Email == userEmail).FirstOrDefault(); var lends = dbContext.Lend.Where(l => l.UserId == user.Id).ToList(); var lendServicew = new LendService(); var lendViewModel = lendServicew.GetViewModel(lends.OrderBy(o => o.DateBorrowed).ToList()); return(View(lendViewModel)); } }
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 Index(Cart cart) { using (var dbContext = new MvclibraryEntities()) { var histroy = dbContext.History.ToList(); var user = dbContext.User.Where(u => u.Email == User.Identity.Name).FirstOrDefault(); var userHistory = histroy.Where(h => h.UserID == user.Id).ToList(); var bookvm = new List <BookViewModel>(); var bookse = dbContext.Books.ToList(); var cartBookViewModel = new CartBookViewModel(); cartBookViewModel.cart = cart; foreach (var h in userHistory) { if (h.Word == null) { h.Word = ""; } var books = bookse.Where(b => b.ISBN.Contains(h.Word) || b.Title.Contains(h.Word) || b.Author.Contains(h.Word)).ToList(); foreach (var item in books) { cartBookViewModel.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 }); } } return(View(cartBookViewModel)); } }
// GET: Category/Edit/5 public ActionResult Edit(int id) { using (var dbContext = new MvclibraryEntities()) { var s = dbContext.category.ToList(); var editCategory = dbContext.category.Where(c => c.Id == 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 CartBookViewModel GetCartViewModel() { var cartBookViewModel = new CartBookViewModel(); using (var dbContext = new MvclibraryEntities()) { var books = dbContext.Books.ToList(); var bookvm = new List <BookViewModel>(); foreach (var item in books) { cartBookViewModel.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 }); } } return(cartBookViewModel); }
// GET: Category public ActionResult Index() { using (var dbContext = new MvclibraryEntities()) { var categories = dbContext.category.ToList(); var categoriesvm = new List <CategoryViewModel>(); foreach (var item in categories) { var rootName = string.Empty; if (item.RootCategoryId != null) { rootName = categories.Where(c => c.Id == item.RootCategoryId).FirstOrDefault().Name; } categoriesvm.Add(new CategoryViewModel { Name = item.Name, RootName = rootName, Id = item.Id }); } return(View(categoriesvm)); } }
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()); } }