Ejemplo n.º 1
0
        private void MetaAddIngredientsAndStatistics(int productId, int mealId, decimal Weight, Meal modMeal)
        {
            Meal = mealData.Recomposite(modMeal, Product, Weight);

            if (Meal.MealProducts != null)
            {
                MealProducts = Meal.MealProducts.ToList();
            }

            MealProduct = mealProductData.SetValues(MealProduct, mealId, productId, Weight);

            mealProductData.Add(MealProduct);
            MealProducts.Add(MealProduct);


            Meal.Products     = Products;
            Meal.MealProducts = MealProducts;
            mealData.Commit();
        }
Ejemplo n.º 2
0
        public IActionResult GetAll()
        {
            try
            {
                MealProducts = mealProductData.GetAll().ToList();

                if (!MealProducts.Any())
                {
                    return(NotFound());
                }

                MealProductDTOs = ApiRepository.MealProductsToDto(MealProducts);

                return(Ok(MealProductDTOs));
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
        }
Ejemplo n.º 3
0
        public async Task <Response <AddMealDto> > AddAsync(AddMealBindingModel bindingModel, int userId)
        {
            var response        = new Response <AddMealDto>();
            var mealProductsTmp = new List <MealProducts>();

            var user = await _userRepository.GetByAsync(x => x.Id == userId);

            if (user == null)
            {
                response.AddError(Key.User, Error.NotExist);
                return(response);
            }

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

            meal.User   = user;
            meal.UserId = userId;

            var mealAddSuccess = await _mealRepository.AddAsync(meal); //Dodanie posilku bez produktow

            if (!mealAddSuccess)
            {
                response.AddError(Key.Meal, Error.AddError);
                return(response);
            }

            //Dla kazdego podanego id produktu dodaj wpis w mealProducts
            foreach (var productId in bindingModel.ProductsIds)
            {
                var mealProduct = new MealProducts()
                {
                    MealId    = meal.Id,
                    ProductId = productId,
                    UserId    = userId
                };
                var addMealProductSuccess = await _mealProductsRepository.AddAsync(mealProduct);

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

                mealProductsTmp.Add(mealProduct);
            }

            meal.MealProducts = mealProductsTmp;

            var mealDto = _mapper.Map <AddMealDto>(meal);

            mealDto.Products = new List <DetailsProductDto>();

            //Pobranie produktów z tab Product na podstawie id zapisanych w junctionTable
            foreach (var mealProduct in meal.MealProducts)
            {
                var productResponse = await _productService.GetAsync(mealProduct.ProductId);

                if (productResponse.ErrorOccurred)
                {
                    response.AddError(Key.Product, Error.NotExist);
                    return(response);
                }

                mealDto.Products.Add(productResponse.SuccessResult);
            }

            //Zliczalnie kalorycznosci
            //foreach (var product in mealDto.Products)
            //{
            //    mealDto.Kcal += product.Kcal;
            //}

            //mealDto to posiłki z produktami do wyswietlenia na zwrotce
            response.SuccessResult = mealDto;

            return(response);
        }
Ejemplo n.º 4
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);
        }