//public ActionResult StoreOffer(OfferModel offerModel) public async Task <ActionResult> StoreOffer() { var offerModel = Session["couponDetails"] as OfferModel; if (offerModel == null) { return(View("CreateOffer")); } var offerObj = new Offer() { DiscountRate = offerModel.Discount, //MerchantName = offerModel.Merchants.ToString(), OfferBegins = offerModel.OfferStarts.Value, OfferEnds = offerModel.OfferEnds.Value, OfferDetails = offerModel.OfferDescription, TotalOffer = offerModel.TotalOffers, OfferName = offerModel.OfferName, MerchantID = offerModel.OffererId }; //Allot Coupons to Production await LoadProductionCoupon.LoadCoupons(offerModel.TotalOffers); _context.Offers.Add(offerObj); _context.SaveChanges(); return(RedirectToAction("CreateOffer", "Admin")); }
public async Task <ActionResult> StoreOffer(MerchantCreateOfferViewModel model) { var userId = User.Identity.GetUserId(); //retrieve the image file stored in tempData var file = TempData["img"] as HttpPostedFileBase; if (model == null) { return(RedirectToAction("CreateOffer"));//return View("CreateOffer"); } //Create a new Offer var offerObj = new Offer() { DiscountRate = model.DiscountRate.Value, OfferBegins = model.OfferBegins.Value, OfferEnds = model.OfferEnds.Value, OfferDetails = model.OfferDetails, TotalOffer = model.TotalOffer, OfferName = model.OfferName, MerchantID = model.MerchantID, CreationDate = DateTime.Now.Date, //Set the creation date //CouponDurationInMonths = model.CouponDurationInMonths.Value, //CouponPrice = model.CouponPrice.Value, Categories = new List <Category>() }; //path for storing img to file System var basePath = Server.MapPath(@"~\Content\images\" + userId + @"\" + offerObj.OfferName + @"\"); //path stored in database for image retrieval var basePathWithoutServer = @"~\Content\images\" + userId + @"\" + offerObj.OfferName + @"\"; //1.Checks if the path exist before creating if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } //Delete previous file from file system //Permission to delete has to be granted on the file system //else //{ // /**************************/ // /*Code delete files in dir*/ // /**************************/ // var files = new DirectoryInfo(basePath); // foreach (var item in files.GetFiles()) // item.Delete(); //} if (file != null && file.ContentLength > 0 && file.ContentLength < MAXMEGABYTES) { //get file name var fileName = Path.GetFileName(file.FileName); //1.Check if the file already exist before create if (!System.IO.File.Exists(basePath + fileName)) { try { /*************************/ /**Code to save the file**/ /*************************/ file.SaveAs(basePath + fileName); //store file location to database offerObj.OfferImageLocation = basePathWithoutServer; //store offer image name offerObj.OfferImageName = fileName; } catch (Exception ex) { Console.WriteLine(ex.StackTrace.ToString()); throw; } } } //1.Add each category the user selects foreach (byte item in model.CategoryIds) { offerObj.Categories.Add(_context.Categories.First(c => c.CategoryId == item)); } //2.Add the offer created to memory _context.Offers.Add(offerObj); //3.Update the database with the offer _context.SaveChanges(); //1. Ensures that production database has //enough coupon codes generated await LoadProductionCoupon.LoadCoupons(model.TotalOffer.Value); return(RedirectToAction("CreateOffer")); }