Ejemplo n.º 1
0
        public ActionResult Add(RestaurantMenuViewModel model, HttpPostedFileBase menuimage)
        {
            try
            {
                //Explicitly Validate Model for menu image
                if (menuimage == null || menuimage.ContentLength < 1 || (menuimage.ContentType != "image/jpeg" && menuimage.ContentType != "image/png"))
                    ModelState.AddModelError("addstatus", "A Menu Item needs to have a valid Image, Only JPEG and PNG images are supported");
                if (ModelState.IsValid)
                {
                    // Attempt to add the offer
                    var restauranttable = new RestaurantMenuItem
                    {
                        Name = model.Name,
                        Description = model.Description,
                        Price = model.Price,
                        PictureFile = new ImagesController().PutImage(menuimage, null).ToString("n")
                    };

                    var itemid = Repository.Add(restauranttable);
                    if (itemid > 0)
                    {
                        TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                        {
                            Message = String.Format("Menu Item \"{0}\" with Id:{1} was successfully Added", model.Name, itemid),
                            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("addstatus") ? ModelState["addstatus"].Errors[0].ErrorMessage : "There was an Error in adding the new Menu item, 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, RestaurantMenuViewModel model, HttpPostedFileBase menuimage)
        {
            try
            {
                // Get previous details
                var existingmenuitem = Repository.Find(id);
                if (existingmenuitem == null) throw new InvalidOperationException("Sorry the update cannot be done as no such menu Item exist");

                if (menuimage != null && (menuimage.ContentLength < 1
                    || (menuimage.ContentType != "image/jpeg" && menuimage.ContentType != "image/png")))
                {
                    ModelState.AddModelError("updatestatus",
                                             "A Menu Item needs to have a valid Image, Only JPEG and PNG images are supported");
                    model.PictureFileName = existingmenuitem.PictureFile;
                }

                if (ModelState.IsValid)
                {

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

                    existingmenuitem.Description = model.Description;
                    existingmenuitem.Name = model.Name;
                    existingmenuitem.Price = model.Price;

                    //And push the changed details
                    if (Repository.Update(existingmenuitem))
                    {
                        TempData[TempDataStringResuorce.ActionResultNotification] = new ActionResultNotification
                        {
                            Message = String.Format("Details for {0} was successfully Updated", existingmenuitem.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);
            }
        }