public IHttpActionResult AddFoodToList([FromBody] FoodItemViewModel viewModel)
        {
            try
            {
                if (viewModel == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                FoodList singleFoodList = _foodListRepository.GetSingle(x => x.Id == viewModel.FoodListId, "Foods");
                FoodItem item           = Mapper.Map <FoodItem>(viewModel);
                item.Created     = DateTime.Now;
                item.ImageString = CurrentAppSettings.DummyImageName;
                singleFoodList.Foods.Add(item);
                _foodListRepository.Update(singleFoodList);

                int save = _foodListRepository.Save();

                if (save > 0)
                {
                    return(CreatedAtRoute("GetSingleFood", new { foodItemId = item.Id }, Mapper.Map <FoodItemViewModel>(item)));
                }

                return(BadRequest());
            }
            catch (Exception exception)
            {
                return(InternalServerError(exception));
            }
        }
Ejemplo n.º 2
0
        public IActionResult AddFoodToList([FromBody] FoodItemDto viewModel)
        {
            if (viewModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            FoodList singleFoodList = _foodListRepository.GetSingle(viewModel.FoodListId, true);
            FoodItem item           = Mapper.Map <FoodItem>(viewModel);

            item.Created     = DateTime.Now;
            item.ImageString = _appSettingsAccessor.DummyImageName;
            singleFoodList.Foods.Add(item);
            _foodListRepository.Update(singleFoodList);

            if (_foodListRepository.Save())
            {
                return(CreatedAtRoute("GetSingleFood", new { foodItemId = item.Id }, Mapper.Map <FoodItemDto>(item)));
            }

            return(BadRequest());
        }