Esempio n. 1
0
        public async Task <IActionResult> EditModal([FromForm] int id, [FromForm] Complex cmp, [FromForm] List <DishComplex> DishComplexes)
        {
            if (id != cmp.Id)
            {
                return(NotFound());
            }


            var complex_orig = await _context.Complex.Include(c => c.DishComplex).ThenInclude(d => d.Dish).AsNoTracking().SingleOrDefaultAsync(c => c.Id == id);

            DishComplexes.ForEach(dc => dc.Dish = _context.Dishes.SingleOrDefaultAsync(c => c.Id == dc.DishId).Result);
            ViewData["CategoriesId"]            = new SelectList(_context.Categories.WhereCompany(User.GetCompanyID()).ToList(), "Id", "Name", cmp.CategoriesId);
            ViewData["DishKindId"] = new SelectList(GetDishesKindWithEmptyList(), "Value", "Text", cmp.DishKindId);;

            if (!ModelState.IsValid)
            {
                cmp.DishComplex = complex_orig.DishComplex;
                return(PartialView(cmp));
            }
            if (complex_orig != null)
            {
                var validate_error = await _complexRepo.ValidateComplexUpdate(cmp, User.GetCompanyID(), DishComplexes, complex_orig.DishComplex.ToList());

                if (!validate_error.Success)
                {
                    ModelState.AddModelError("", validate_error.Error);
                    cmp.DishComplex = complex_orig.DishComplex;
                    return(PartialView(cmp));
                }
            }
            if (Request.Form.Files.Count > 0)
            {
                Pictures pict = _context.Pictures.SingleOrDefault(p => p.Id == cmp.PictureId);
                if (pict == null || true) //to do always new
                {
                    pict = new Pictures();
                }
                var file = Request.Form.Files[0];
                using (var stream = Request.Form.Files[0].OpenReadStream())
                {
                    byte[] imgdata = new byte[stream.Length];
                    stream.Read(imgdata, 0, (int)stream.Length);
                    pict.PictureData = imgdata;

                    PicturesController.CompressPicture(pict, 350, 350);

                    //pict.PictureData = imgdata;
                }
                _context.Add(pict);
                await _context.SaveChangesAsync();

                cmp.PictureId = pict.Id;
            }
            var res = await this.UpdateDBCompanyDataAsyncEx2(cmp, _logger,
                                                             e => { return(_complexRepo.UpdateComplexEntity(e, DishComplexes, User.GetCompanyID())); });

            //var res = await this.UpdateDBCompanyDataAsyncEx(cmp, _logger);
            if (!ModelState.IsValid)
            {
                cmp.DishComplex = complex_orig.DishComplex;
                return(PartialView(cmp));
            }



            return(res);
        }
Esempio n. 2
0
        public async Task <IActionResult> EditModal(int id, [Bind("Id,Code,Name,Price,Description,CategoriesId,PictureId,ReadyWeight,CookingTechnologie")] Dish dish, /*List<string> IngredientsIds,*/ List <DishIngredients> proportion)
        {
            if (id != dish.Id)
            {
                return(NotFound());
            }
            if (dish.Code == null)
            {
                dish.Code = "";
            }
            if (dish.Description == null)
            {
                dish.Description = "";
            }
            if (dish.CookingTechnologie == null)
            {
                dish.CookingTechnologie = "";
            }
            var dish_orig = await _context.Dishes.Include(d => d.DishIngredients).ThenInclude(d => d.Ingredient).AsNoTracking().Where(d => d.Id == id).FirstOrDefaultAsync();

            //_context.Entry(dish_orig).Collection(d => d.DishIngredients).Query().Include(d => d.Ingredient).Load();
            if (Request.Form.Files.Count > 0)
            {
                Pictures pict = _context.Pictures.SingleOrDefault(p => p.Id == dish.PictureId);
                if (pict == null || true) //to do always new
                {
                    pict = new Pictures();
                }
                var file = Request.Form.Files[0];
                using (var stream = Request.Form.Files[0].OpenReadStream())
                {
                    byte[] imgdata = new byte[stream.Length];
                    stream.Read(imgdata, 0, (int)stream.Length);
                    pict.PictureData = imgdata;

                    PicturesController.CompressPicture(pict, 350, 350);

                    //pict.PictureData = imgdata;
                }
                _context.Add(pict);
                await _context.SaveChangesAsync();

                dish.PictureId = pict.Id;
            }


            ViewData["CategoriesId"] = new SelectList(_context.Categories.WhereCompany(User.GetCompanyID()).ToList(), "Id", "Name", dish.CategoriesId);
            var res = await this.UpdateDBCompanyDataAsyncEx(dish, _logger,
                                                            e => { return(_dishesRepo.UpdateDishEntity(e, proportion, User.GetCompanyID())); });

            if (!ModelState.IsValid)
            {
                if (ModelState["Name"].Errors.Count > 0)
                {
                    ModelState["Name"].Errors.Clear();
                    ModelState["Name"].Errors.Add(_localizer["Incorrect data"]);
                }
                if (ModelState["Code"].Errors.Count > 0)
                {
                    ModelState["Code"].Errors.Clear();
                    ModelState["Code"].Errors.Add(_localizer["Incorrect data"]);
                }
                if (ModelState["Price"].Errors.Count > 0)
                {
                    ModelState["Price"].Errors.Clear();
                    ModelState["Price"].Errors.Add(_localizer["Incorrect data"]);
                }
                if (ModelState["ReadyWeight"].Errors.Count > 0)
                {
                    ModelState["ReadyWeight"].Errors.Clear();
                    ModelState["ReadyWeight"].Errors.Add(_localizer["Incorrect data"]);
                }
                if (ModelState["Description"].Errors.Count > 0)
                {
                    ModelState["Description"].Errors.Clear();
                    ModelState["Description"].Errors.Add(_localizer["Incorrect data"]);
                }


                dish.DishIngredients = dish_orig.DishIngredients;
                return(PartialView(dish));
            }

            return(res);
        }