public ActionResult CreateOffer() { //check if the action was called by edit offer if (TempData["result"] != null) { ViewBag.IsEdit = true; } var userId = User.Identity.GetUserId(); var getUser = _context.Users.Include(c => c.Merchant).FirstOrDefault(c => c.Id == userId); //Gets and set the Merchant Id to the page var viewModel = new MerchantCreateOfferViewModel(); viewModel.MerchantID = getUser.Merchant.MerchantID; viewModel.Merchant = getUser.Merchant; //1.Initialize the category list viewModel.Categories = new List <Category>(); //2.Fetch the list of available Categories viewModel.Categories = _context.Categories.ToList(); return(View(viewModel)); }
public ActionResult Preview(MerchantCreateOfferViewModel model) { //Initialize and populate category model.Categories = new List <Category>(); model.Categories = _context.Categories.ToList(); //Returns to Create Offer if Offer Invalid if (!ModelState.IsValid) { return(View("CreateOffer", model)); } //Check if merchant has package var getMerchantId = User.Identity.GetUserId(); var getMerchant = _context.Merchants .Include(c => c.Package) .FirstOrDefault(c => c.MerchantID == getMerchantId); if (getMerchant.Package == null) { TempData["NoPackage"] = "Please purchase a package first."; return(View("CreateOffer", model)); } //1.Store model in a variable //To Send send to View var viewModel = model; //2.Stores the image in temp varibale to //pass to another action if (model.OfferImg != null) { TempData["img"] = model.OfferImg; } return(View(model)); }
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")); }