Exemplo n.º 1
0
        public async Task <IActionResult> UpdateAsync(UpdateMealBindingModel bindingModel)
        {
            var result = await _mealService.UpdateAsync(bindingModel);

            if (result.ErrorOccurred)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
Exemplo n.º 2
0
        private async Task <Response <object> > ValidateUpdateViewModel(UpdateMealBindingModel bindingModel)
        {
            var  response   = new Response <object>();
            bool mealExists = await _mealRepository.ExistAsync(x => x.Id == bindingModel.Id);

            if (!mealExists)
            {
                response.AddError(Key.Meal, Error.NotExist);
            }

            return(response);
        }
Exemplo n.º 3
0
        public async Task <Response <object> > UpdateAsync(UpdateMealBindingModel bindingModel)
        {
            //sprawdz czy posilek istnieje
            var response = await ValidateUpdateViewModel(bindingModel);

            var mealProductsTmp = new List <MealProducts>();

            if (response.ErrorOccurred)
            {
                return(response);
            }

            //pobierz dane posilku do aktualizacji
            var meal = await _mealRepository.GetByAsync(x => x.Id == bindingModel.Id);

            //odłącz posiłek z Meal
            _mealRepository.Detach(meal);

            //Dla każdego podanego id produktu w posilku dodaj wpis w MealProducts
            foreach (var productId in bindingModel.ProductsIds)
            {
                var updatedMealProduct = new MealProducts()
                {
                    MealId    = meal.Id,
                    ProductId = productId,
                    UserId    = meal.UserId
                };

                //Dodaj wpis do listy która posłuży do aktualizacji mealProducts
                mealProductsTmp.Add(updatedMealProduct);
            }


            //Wyciągnięcie istniejącego wpisu produktów aktualizowanego posiłku
            var mealProducts = await _mealProductsRepository.GetAllByAsync(x => x.MealId == meal.Id).Result.ToListAsync();

            //Usunięcie istniejących produktów w posiłku
            foreach (var element in mealProducts)
            {
                bool deleteSucceed = await _mealProductsRepository.RemoveAsync(element);

                if (!deleteSucceed)
                {
                    response.AddError(Key.MealProducts, Error.NotExist);
                    return(response);
                }
            }

            var updatedMeal = _mapper.Map <Meal>(bindingModel);

            updatedMeal.MealProducts = mealProductsTmp;
            updatedMeal.UserId       = meal.UserId;
            updatedMeal.User         = meal.User;
            updatedMeal.Id           = meal.Id;

            //Aktualizacja posiłku z produktami
            bool updateSucceed = await _mealRepository.UpdateAsync(updatedMeal);

            if (!updateSucceed)
            {
                response.AddError(Key.Meal, Error.UpdateError);
            }

            response.SuccessResult = bindingModel;
            return(response);
        }