public async Task <ActionResult> AddOffer(FormCollection collection) { List <OfferPicture> pictures = new List <OfferPicture>(); User editUser = db.Users.Where(u => u.Login == HttpContext.User.Identity.Name).FirstOrDefault(); int categoryID = int.Parse(collection["Category"]); Category offerCategory = db.Categories.Where(o => o.CategoryID == categoryID).FirstOrDefault(); string priceWithDot = collection["Price"].Replace(',', '.'); if (!double.TryParse(priceWithDot, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out double priceDouble) && priceDouble > 0) { return(RedirectToAction("AddOffer", "UserPanel", new { success = false })); } if (!int.TryParse(collection["State"], out int offerStateInt)) { return(RedirectToAction("AddOffer", "UserPanel", new { success = false })); } Offer offer = new Offer { Title = collection["Name"], Description = collection["Description"], InStockOriginaly = Convert.ToDouble(collection["Quantity"]), Price = priceDouble, OfferState = (OfferState)offerStateInt, Category = offerCategory, User = editUser, IsActive = true, CreationDate = DateTime.Now }; offer.InStockNow = offer.InStockOriginaly; db.Offers.Add(offer); db.SaveChanges(); HttpFileCollectionBase filesForOffer = null; if (TempData["offerImages"] != null) { filesForOffer = (HttpFileCollectionBase)TempData["offerImages"]; } if (filesForOffer != null && filesForOffer.Count > 0 && offer != null) { var files = filesForOffer; if (files != null && files.Count > 0) { try { for (int i = 0; i < files.Count; i++) { var workFile = files[i]; var fileUrl = await FileManager.UploadOfferImage(workFile, offer.OfferID, i); if (fileUrl != null) { OfferPicture offerPicture = new OfferPicture() { PathToFile = fileUrl, Offer = offer }; pictures.Add(offerPicture); } else { return(RedirectToAction("Offers", "UserPanel", new { success = false, offerID = offer.OfferID })); } } } catch (Exception ex) { ViewBag.Error = "Wystąpił błąd: " + ex.Message.ToString(); return(RedirectToAction("Offers", "UserPanel", new { success = false, offerID = offer.OfferID })); } } else { return(RedirectToAction("Offers", "UserPanel", new { success = false, offerID = offer.OfferID })); } if (ViewBag.Error == null) { offer.OfferPictures = pictures; db.SaveChanges(); } } else { ViewBag.Error = "Wystąpiły problemy ze zdjęciami"; return(RedirectToAction("Offers", "UserPanel", new { success = false, offerID = offer.OfferID })); } if (offer.OfferPictures.Count == 0) { return(RedirectToAction("Offers", "UserPanel", new { success = false, offerID = offer.OfferID })); } if (ViewBag.Error == null) { offer.Category.Offers.Add(offer); db.SaveChanges(); editUser.Offers.Add(offer); db.SaveChanges(); return(RedirectToAction("Offers", "UserPanel", new { success = true })); } else { return(RedirectToAction("AddOffer", "UserPanel", new { success = false, offerID = offer.OfferID })); } }
public async System.Threading.Tasks.Task <IHttpActionResult> AddAsync() { var uploadPath = HostingEnvironment.MapPath("/") + @"/Uploads"; Directory.CreateDirectory(uploadPath); var provider = new MultipartFormDataStreamProvider(uploadPath); await Request.Content.ReadAsMultipartAsync(provider); // Form data // string serializedModel = ""; foreach (var key in provider.FormData.AllKeys) { foreach (var val in provider.FormData.GetValues(key)) { if (val != "") { serializedModel = val; } } } OfferBindingModel model = JsonConvert.DeserializeObject <OfferBindingModel>(serializedModel); User user = db.Users.Where(u => u.UserID == model.UserID).FirstOrDefault(); int categoryID = model.CategoryID; Category offerCategory = db.Categories.Where(o => o.CategoryID == categoryID).FirstOrDefault(); string priceWithDot = model.Price.Contains(',') ? model.Price.Replace(',', '.') : model.Price; Offer offer = new Offer { Title = model.Title, Description = model.Description, InStockOriginaly = model.InStockOriginaly, Price = Convert.ToDouble(priceWithDot), Category = offerCategory, User = user, IsActive = true, CreationDate = DateTime.Now, OfferState = model.OfferState }; offer.InStockNow = offer.InStockOriginaly; db.Offers.Add(offer); db.SaveChanges(); var filesData = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files : null; List <OfferPicture> pictures = new List <OfferPicture>(); HttpFileCollection filesForOffer = null; if (filesData != null) { filesForOffer = filesData; } if (filesForOffer != null && filesForOffer.Count > 0 && offer != null) { var files = filesForOffer; if (files != null && files.Count > 0) { try { for (int i = 0; i < files.Count; i++) { var workFile = new HttpPostedFileWrapper(files[i]); var fileUrl = await FileManager.UploadOfferImage(workFile, offer.OfferID, i); if (fileUrl != null) { OfferPicture offerPicture = new OfferPicture() { PathToFile = fileUrl, Offer = offer }; pictures.Add(offerPicture); } } } catch (Exception ex) { } } else if (files.Count == 0) { OfferPicture offerPicture = new OfferPicture() { PathToFile = "../../Images/product.jpg", Offer = offer }; pictures.Add(offerPicture); } offer.OfferPictures = pictures; db.SaveChanges(); offer.Category.Offers.Add(offer); db.SaveChanges(); user.Offers.Add(offer); db.SaveChanges(); System.IO.DirectoryInfo di = new DirectoryInfo(uploadPath); foreach (FileInfo file in di.GetFiles()) { file.Delete(); } } if (offer != null) { return(Ok()); } return(BadRequest("Couldn't create offer")); }
public ActionResult AddOffer(AddOfferViewModel aovm, HttpPostedFileBase AddPicture) { if (ModelState.IsValid) { try { bool result = ol.CheckVendorIdInOffer(aovm.VendorId); if (result == true) { result = ol.CheckTypeId(aovm.VendorId, aovm.OfferTypeId); if (result == true) { result = ol.CheckOverlap(aovm.OfferStartTime, aovm.OfferStopTime, aovm.VendorId, aovm.OfferTypeId); if (result == true) { TempData["Success"] = "At the same time you can't two offers of same type."; aovm.OfferTypeList = new SelectList(entity.OfferTypes, "OfferTypeId", "OfferTypeName"); return(View(aovm)); } } } result = ol.CheckNameExistance(aovm.OfferName, aovm.VendorId); if (result == true) { TempData["Success"] = "You have a same Offer in your List please check that"; aovm.OfferTypeList = new SelectList(entity.OfferTypes, "OfferTypeId", "OfferTypeName"); return(View(aovm)); } Offer offer = new Offer(); offer.OfferName = aovm.OfferName; offer.VendorId = aovm.VendorId; offer.CountryId = aovm.CountryId; offer.CityId = aovm.CityId; offer.OfferTypeId = aovm.OfferTypeId; offer.OfferPrice = aovm.OfferPrice; offer.OfferStartTime = Convert.ToDateTime(aovm.OfferStartTime); offer.OfferStopTime = Convert.ToDateTime(aovm.OfferStopTime); if (aovm.OfferDetail != null) { offer.OfferDetail = aovm.OfferDetail; } else { offer.OfferDetail = "Not given yet."; } offer.OfferStatus = true; offer.OfferAdminsPermit = true; offer.OfferDateOfAccountCreation = DateTime.Now; entity.Offers.Add(offer); Vendor vendor = (from c in entity.Vendors where c.VendorId == aovm.VendorId select c).FirstOrDefault(); vendor.VendorOfferStatus = true; if (entity.SaveChanges() > 0) { var p = (from c in entity.Offers where c.OfferName == aovm.OfferName && c.CountryId == aovm.CountryId && c.CityId == aovm.CityId select new { c.OfferId, c.CountryId, c.CityId }).FirstOrDefault(); if (AddPicture != null) { OfferAlbum pa = new OfferAlbum(); pa.OfferAlbumName = "Profile Picture"; pa.OfferId = p.OfferId; pa.AlbumTypeId = 1; pa.CityId = p.CityId; pa.CountryId = p.CountryId; pa.VendorId = aovm.VendorId; pa.OfferAlbumAdminPermit = true; pa.OfferAlbumPrivacy = true; pa.OfferAlbumDateOfAdded = DateTime.Now; entity.OfferAlbums.Add(pa); entity.SaveChanges(); OfferPicture pp = new OfferPicture(); pp.OfferPictureData = new byte[AddPicture.ContentLength]; AddPicture.InputStream.Read(pp.OfferPictureData, 0, AddPicture.ContentLength); pp.OfferId = p.OfferId; var albumId = (from c in entity.OfferAlbums where c.OfferId == p.OfferId && c.AlbumTypeId == 1 select c.OfferAlbumId).Single(); int AlbumId = Convert.ToInt32(albumId); pp.OfferAlbumId = AlbumId; pp.AlbumTypeId = 1; pp.OfferPictureAdminPermit = true; pp.CountryId = p.CountryId; pp.CityId = p.CityId; pp.VendorId = aovm.VendorId; pp.OfferPicturePrivacy = true; pp.OfferPictureDateOfAdded = DateTime.Now; entity.OfferPictures.Add(pp); if (entity.SaveChanges() > 0) { Offer o = (from c in entity.Offers where c.OfferId == p.OfferId select c).FirstOrDefault(); int pictureId = (from c in entity.OfferPictures where c.OfferId == p.OfferId && c.AlbumTypeId == 1 orderby c.OfferPictureId descending select c.OfferPictureId).First(); o.OfferProfilePicId = pictureId; entity.SaveChanges(); } } ModelState.Clear(); TempData["Success"] = "Added Successfully"; int VendorId = (int)Session["VendorId"]; var d = (from c in entity.Vendors where c.VendorId == VendorId select c).FirstOrDefault(); aovm.CountryId = Convert.ToInt32(d.CountryId); aovm.CityId = Convert.ToInt32(d.CityId); aovm.VendorId = Convert.ToInt32(d.VendorId); aovm.OfferTypeList = new SelectList(entity.OfferTypes, "OfferTypeId", "OfferTypeName"); return(View(aovm)); } else { aovm.OfferTypeList = new SelectList(entity.OfferTypes, "OfferTypeId", "OfferTypeName"); TempData["Success"] = "Error not Saved"; return(View(aovm)); } } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "Offer", "AddOffer"))); } } else { aovm.OfferTypeList = new SelectList(entity.OfferTypes, "OfferTypeId", "OfferTypeName"); return(View(aovm)); } }
public static OfferItemPicture ConvertOfferPictureToOfferItemPicture(OfferPicture offerPicture) { return(new OfferItemPicture(offerPicture.OfferImageID, offerPicture.PathToFile, offerPicture.Offer.OfferID)); }