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)); } }
public CartBookViewModel GetBookViewModel(Cart cart) { var cartBookViewModel = new CartBookViewModel(); cartBookViewModel.cart = cart; try { var books = _bookRepository.GetAllBoks(); if (books == null) { return(cartBookViewModel); } foreach (var item in books) { cartBookViewModel.BookViewModels.Add( new BookViewModel { Id = item.Id, Author = item.Author, Title = item.Title, ISBN = item.ISBN, Available = item.Available } ); } return(cartBookViewModel); } catch { return(cartBookViewModel); } }
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 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)); } }
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); }