Ejemplo n.º 1
0
        public ActionResult Edit(int id, UpdateOfferModel model, HttpPostedFileBase offerimage)
        {
            try
            {
                // Get previous details
                var existingoffer = Repository.Find(id);
                if (existingoffer == null)
                {
                    throw new InvalidOperationException("Sorry the update cannot be done as no such offer exist");
                }

                if (existingoffer is SeasonalOffer && model.PublishedBy == UpdateOfferModel.PublishedVia.Advertisement &&
                    offerimage != null && (offerimage.ContentLength < 1 ||
                                           (offerimage.ContentType != "image/jpeg" && offerimage.ContentType != "image/png")))
                {
                    ModelState.AddModelError("updatestatus",
                                             "An Seasonal Offer Advertisement needs to have a valid Advertisement Image, Only JPEG and PNG images are supported");
                    ViewBag.PictureFileName = (existingoffer as SeasonalOffer).PictureFileName;
                }

                if (ModelState.IsValid)
                {
                    // Attempt to Update the offer
                    if (existingoffer is SeasonalOffer && offerimage != null && offerimage.ContentLength > 0)
                    {
                        var previd          = (existingoffer as SeasonalOffer).PictureFileName;
                        var imagecontroller = new ImagesController();
                        (existingoffer as SeasonalOffer).PictureFileName =
                            imagecontroller.PutImage(offerimage, null).ToString("n");
                        // if both image ids are not same then delete the prev id.
                        if (!String.Equals(previd, (existingoffer as SeasonalOffer).PictureFileName))
                        {
                            imagecontroller.DeleteImage(previd);
                        }
                    }

                    existingoffer.Description = model.Description;
                    existingoffer.Title       = model.Title;
                    existingoffer.ValidTill   = model.ValidTill;

                    //And push the changed details
                    if (Repository.Update(existingoffer))
                    {
                        TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                        {
                            Message = String.Format("Details for {0} was successfully Updated", existingoffer.GetType().Name),
                            Result  = true,
                            State   = ActionResultNotification.MessageState.Information
                        };
                        return(RedirectToAction("Index"));
                    }
                }
                // If we got this far, something failed, redisplay form
                TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                {
                    Message = ModelState.ContainsKey("updatestatus") ? ModelState["updatestatus"].Errors[0].ErrorMessage : "Cannot Update the details of the offer with given details, please try again",
                    Result  = false,
                    State   = ActionResultNotification.MessageState.Warning
                };
                return(View(model));
            }
            catch (Exception e)
            {
                TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                {
                    Message = e.Message,
                    Result  = false,
                    State   = ActionResultNotification.MessageState.Error
                };
                return(View(model));
            }
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int id, UpdateOfferModel model, HttpPostedFileBase offerimage)
        {
            try
            {
                // Get previous details
                var existingoffer = Repository.Find(id);
                if (existingoffer == null) throw new InvalidOperationException("Sorry the update cannot be done as no such offer exist");

                if (existingoffer is SeasonalOffer && model.PublishedBy == UpdateOfferModel.PublishedVia.Advertisement
                    && offerimage != null && (offerimage.ContentLength < 1
                    || (offerimage.ContentType != "image/jpeg" && offerimage.ContentType != "image/png")))
                {
                    ModelState.AddModelError("updatestatus",
                                             "An Seasonal Offer Advertisement needs to have a valid Advertisement Image, Only JPEG and PNG images are supported");
                    ViewBag.PictureFileName = (existingoffer as SeasonalOffer).PictureFileName;
                }

                if (ModelState.IsValid)
                {

                    // Attempt to Update the offer
                    if (existingoffer is SeasonalOffer && offerimage != null && offerimage.ContentLength > 0)
                    {
                        var previd = (existingoffer as SeasonalOffer).PictureFileName;
                        var imagecontroller = new ImagesController();
                        (existingoffer as SeasonalOffer).PictureFileName =
                            imagecontroller.PutImage(offerimage, null).ToString("n");
                        // if both image ids are not same then delete the prev id.
                        if (!String.Equals(previd, (existingoffer as SeasonalOffer).PictureFileName)) imagecontroller.DeleteImage(previd);
                    }

                    existingoffer.Description = model.Description;
                    existingoffer.Title = model.Title;
                    existingoffer.ValidTill = model.ValidTill;

                    //And push the changed details
                    if (Repository.Update(existingoffer))
                    {
                        TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                            {
                                Message = String.Format("Details for {0} was successfully Updated", existingoffer.GetType().Name),
                                Result = true,
                                State = ActionResultNotification.MessageState.Information
                            };
                        return RedirectToAction("Index");
                    }
                }
                // If we got this far, something failed, redisplay form
                TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                {
                    Message = ModelState.ContainsKey("updatestatus") ? ModelState["updatestatus"].Errors[0].ErrorMessage : "Cannot Update the details of the offer with given details, please try again",
                    Result = false,
                    State = ActionResultNotification.MessageState.Warning
                };
                return View(model);
            }
            catch (Exception e)
            {
                TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                {
                    Message = e.Message,
                    Result = false,
                    State = ActionResultNotification.MessageState.Error
                };
                return View(model);
            }
        }