public async Task <IActionResult> AddBook(SellBookModel model, IFormFile file) { if (ModelState.IsValid) { model.ProfilePicUrl = UploadFile(file, model.Id); _context.SellBook.Add(model); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult AddToCart(SellBookModel book, int qty) { OrderModel O = new OrderModel(); O.ProductName = book.Name; O.Quantity = qty; O.Price = book.Price; O.Bill = qty * O.Price; if (ViewData["cart"] == null) { li.Add(O); ViewData["cart"] = li; } else { List <OrderModel> li2 = ViewBag.cart as List <OrderModel>; int flag = 0; foreach (var item in li2) { if (item.ProductName == O.ProductName) { item.Quantity += O.Quantity; item.Bill += O.Bill; flag = 1; } } if (flag == 0) { li2.Add(O); } ViewData["cart"] = li2; } return(RedirectToAction("Index", "Home")); }
public ActionResult AddBook(SellBookModel model, bool deliveryBool1, bool deliveryBool2, bool deliveryBool3, bool deliveryBool4, IEnumerable <HttpPostedFileBase> files) { List <bool> CheckList = new List <bool> { deliveryBool1, deliveryBool2, deliveryBool3, deliveryBool4 }; if (!ModelState.IsValid) { List <string> errors = new List <string>(); foreach (var modelStateVal in ViewData.ModelState.Values) { foreach (var error in modelStateVal.Errors) { var errorMessage = error.ErrorMessage; var exception = error.Exception; errors.Add(errorMessage); } } foreach (string error in errors) { ModelState.AddModelError("", error); } return(View()); } var context = new AppDbContext(); Debug.WriteLine(context.Users.Find(System.Web.HttpContext.Current.User.Identity.GetUserId()).BankNumber + model.Price + deliveryBool1); if (context.Users.Find(System.Web.HttpContext.Current.User.Identity.GetUserId()).BankNumber == "Nie podano" && model.Price != 0 && deliveryBool1 == false) { ModelState.AddModelError("", "W ustawieniach użytkownika nie podano numeru konta potrzebnego do ewentualnej finalizacji tranzakcji. Aby wystawić książkę na sprzedaż należy uzupełnić te dane."); return(View()); } { } if (model.Price == null) { model.Price = 0; } var book = new Book { Author = model.Author, Title = model.Title, Genre = model.Genre, Description = model.Description, AddDate = DateTime.Now, isChanged = false, isSold = false, Price = model.Price, Publisher = model.Publisher, Changeable = model.Changeable, PublicationDate = model.PublicationDate, SellerId = System.Web.HttpContext.Current.User.Identity.GetUserId(), Seller = context.Users.Find(System.Web.HttpContext.Current.User.Identity.GetUserId()), }; bool bookIsAdded = false; bool checkDelivery = false; if (deliveryBool1 == true) { if (!bookIsAdded) { context.Books.Add(book); context.SaveChanges(); bookIsAdded = true; } bookIsAdded = true; checkDelivery = true; var delivery = new Delivery { Name = "Odbiór osobisty", Price = model.DeliveryPrice[0], DeliveryPriceId = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault().BookId, DeliveryPrices = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault() }; context.DeliveryOptions.Add(delivery); context.SaveChanges(); } if (deliveryBool2 == true) { if (!bookIsAdded) { context.Books.Add(book); context.SaveChanges(); bookIsAdded = true; } checkDelivery = true; var delivery = new Delivery { Name = "Przesyłka pocztowa - priorytetowa", Price = model.DeliveryPrice[1], DeliveryPriceId = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault().BookId, DeliveryPrices = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault() }; context.DeliveryOptions.Add(delivery); context.SaveChanges(); } if (deliveryBool3 == true) { if (!bookIsAdded) { context.Books.Add(book); context.SaveChanges(); bookIsAdded = true; } checkDelivery = true; var delivery = new Delivery { Name = "Przesyłka pocztowa - ekonomiczna", Price = model.DeliveryPrice[2], DeliveryPriceId = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault().BookId, DeliveryPrices = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault() }; context.DeliveryOptions.Add(delivery); context.SaveChanges(); } if (deliveryBool4 == true) { if (!bookIsAdded) { context.Books.Add(book); context.SaveChanges(); bookIsAdded = true; } checkDelivery = true; var delivery = new Delivery { Name = "Przesyłka kurierska", Price = model.DeliveryPrice[3], DeliveryPriceId = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault().BookId, DeliveryPrices = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault() }; context.DeliveryOptions.Add(delivery); context.SaveChanges(); } if (!checkDelivery) { ModelState.AddModelError("", "Proszę wybrać opcję dostawy"); return(View()); } byte[] uploadedFile = null; BookImage bookImage = null; if (Request.Files != null) { int i = 0; foreach (string image in Request.Files) { HttpPostedFileBase hpf = Request.Files[image] as HttpPostedFileBase; if (image != "files") { if (hpf.InputStream.Length > 0) { uploadedFile = new byte[hpf.InputStream.Length]; Debug.WriteLine(uploadedFile.Length); hpf.InputStream.Read(uploadedFile, 0, uploadedFile.Length); bookImage = new BookImage { Image = uploadedFile, BookImgId = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault().BookId, BookImg = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault() }; context.BookImages.Add(bookImage); context.SaveChanges(); } } else { HttpPostedFileBase hpf2 = Request.Files.Get(i); if (hpf2.InputStream.Length > 0) { uploadedFile = new byte[hpf2.InputStream.Length]; Debug.WriteLine(uploadedFile.Length); hpf2.InputStream.Read(uploadedFile, 0, uploadedFile.Length); bookImage = new BookImage { Image = uploadedFile, BookImgId = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault().BookId, BookImg = context.Books.OrderByDescending(o => o.BookId).FirstOrDefault() }; context.BookImages.Add(bookImage); context.SaveChanges(); } } i++; } //próba skalowania obrazu //Image image = Image.FromStream(new MemoryStream(uploadedFile)); //Image newImage; //newImage = ScaleImage(image, 300, 150); //var ms = new MemoryStream(); //newImage.Save(ms, ImageFormat.Gif); //ms.ToArray(); //uploadedFile = ms.ToArray(); //model.BookImage.InputStream.Read(uploadedFile, 0, uploadedFile.Length); } LuceneSearchIndexer.UpdateBooksIndex(); return(RedirectToAction("index", "home")); }