Exemplo n.º 1
0
        public ActionResult Create(FarmCropViewModel model)
        {
            var cache = MemoryCache.Default;
            //если есть в кэше - берём из него, в противном случае запрашиваем из IFarmService
            var rc = (cache.Get("regions") as IEnumerable <NamedItemViewModel>) ?? farmService.GetRegions().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider);
            var fc = (cache.Get("farmers") as IEnumerable <NamedItemViewModel>) ?? farmService.GetFarmers().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider);
            var ac = cache.Get("agricultures") as IEnumerable <NamedItemViewModel> ?? farmService.GetAgricultures().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider);

            //TODO: использовать полноценный тип для View
            ViewBag.Regions      = rc;
            ViewBag.Farmers      = fc;
            ViewBag.Agricultures = ac;

            if (model.Area <= 0)
            {
                ModelState.AddModelError("Area", "Площадь должна быть больше нуля");
            }

            if (model.Gather < 0)
            {
                ModelState.AddModelError("Gather", "Урожай не может быть меньше нуля");
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                var farmCrop = mapper.Map <FarmCropDto>(model);
                farmService.AddFarmCrop(farmCrop);
            }
            catch (ValidationException ex)
            {
                ModelState.AddModelError(ex.Property, ex.Message);
                return(View(model));
            }

            return(RedirectToAction("List"));
        }
Exemplo n.º 2
0
        public ActionResult Create(FarmCropViewModel model)
        {
            /*
             * CR-1 - add check argument for null
             */

            /*
             * CR-1
             * Reduce code duplication with previous method, use ValueProvider class from comments above
             * Add cached values only in case of redirection back to create form because of failed validation
             */
            var cache = MemoryCache.Default;
            //если есть в кэше - берём из него, в противном случае запрашиваем из IFarmService
            var rc = (cache.Get("regions") as IEnumerable <NamedItemViewModel>) ?? farmService.GetRegions().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider);
            var fc = (cache.Get("farmers") as IEnumerable <NamedItemViewModel>) ?? farmService.GetFarmers().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider);
            var ac = cache.Get("agricultures") as IEnumerable <NamedItemViewModel> ?? farmService.GetAgricultures().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider);

            ViewBag.Regions      = rc;
            ViewBag.Farmers      = fc;
            ViewBag.Agricultures = ac;

            /*
             * CR-1
             * Gather all validation rules into one place - class FarmCropViewModel, let it validate its data,
             * Implement method FarmCropViewModel.Validate that returns dictionary with validation errors instead of exceptions
             * Check validation results here in controller. If there are no errors, save farm to DB.
             * If there are an errors, add them to ModelState and redirect back to form
             */

            /*
             * CR-1 - change condition from "<=" to "<".
             */
            if (model.Area <= 0)
            {
                ModelState.AddModelError("Area", "Площадь должна быть больше нуля");
            }

            if (model.Gather < 0)
            {
                ModelState.AddModelError("Gather", "Урожай не может быть меньше нуля");
            }

            if (!ModelState.IsValid)
            {
                /*
                 * CR-1 - add regions, farmers and agricultures only here
                 */
                return(View(model));
            }

            /*
             * CR-1 - get rid of exception based logic, use validation results from FarmCropViewModel
             */
            try
            {
                var farmCrop = mapper.Map <FarmCropDto>(model);
                farmService.AddFarmCrop(farmCrop);
            }
            catch (ValidationException ex)
            {
                ModelState.AddModelError(ex.Property, ex.Message);
                return(View(model));
            }

            return(RedirectToAction("List"));
        }