Exemplo n.º 1
0
        public ActionResult Create()
        {
            var model = new DishFormModel
            {
                Categories = _categoryService.GetCategories().Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value = x.CategoryId.ToString()
                }),
                Markups = _markupService.GetMarkups().Select(m => new SelectListItem
                {
                    Text = m.Name,
                    Value = m.MarkupId.ToString()
                })
            };

            return View(model);
        }
Exemplo n.º 2
0
        public ActionResult Save(DishFormModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                model.File = file.FileName;
                if (model.MarkupId == 2)
                {
                    model.Discount = 5;
                }
                var dish = Mapper.Map<DishFormModel, Dish>(model);

                _dishService.CreateDish(dish);
                string dishImg = Path.GetFileName(file.FileName);
                string path = Path.Combine(Server.MapPath("~/Images/"), dishImg);
                file.SaveAs(path);
                _dishService.Save();

                return RedirectToAction("Index", "Dish");
            }

            return RedirectToAction("Index", "Dish");
        }
Exemplo n.º 3
0
        public ActionResult Update(DishFormModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null && model.File != file.FileName)
                {
                    string pathToDel = Path.Combine(Server.MapPath("~/Images/"), model.File);
                    if (System.IO.File.Exists(pathToDel))
                    {
                        System.IO.File.Delete(pathToDel);
                    }

                    model.File = file.FileName;
                    string dishImg = Path.GetFileName(file.FileName);
                    string path = Path.Combine(Server.MapPath("~/Images/"), dishImg);
                    file.SaveAs(path);
                }

                var dish = Mapper.Map<DishFormModel, Dish>(model);

                _dishService.Update(dish);

                return RedirectToAction("Index", "Dish");
            }

            return RedirectToAction("Index", "Dish");
        }
Exemplo n.º 4
0
        public ActionResult Edit(int id)
        {
            var dish = _dishService.GetDish(id);
            var model = Mapper.Map<Dish, DishModel>(dish);
            var categories = _categoryService.GetCategoriesQuery().Where(x => x.CategoryId != model.CategoryId).ToList();
            categories.Add(_categoryService.GetCategory(model.CategoryId));

            categories.Reverse();

            var formModel = new DishFormModel
            {
                Id = model.DishId,

                DishTitle = model.Name,

                DishPriority = model.Priority,

                DishWeight = model.Weight ?? 0,

                DishCallories = model.Callories ?? 0,

                DishPieces = model.Pieces ?? 1,

                DishPrice = model.Price,

                Discount = model.Discount,

                Enabled = model.Enabled,

                CategoryId = model.CategoryId,

                Composition = model.Composition,

                File = model.Image,

                OrderCount = model.OrderCount ?? 0,

                MarkupId = model.MarkupId,

                Categories = categories.Select(x => new SelectListItem
                {
                    Text = x.Name,
                    Value = x.CategoryId.ToString()
                }),
            };

            return View(formModel);
        }