コード例 #1
0
        public async Task <IActionResult> CreateIngredient(IngredientHeaderForDetailDto IngredientHeaderForDetailDto)
        {
            if (IngredientHeaderForDetailDto == null)
            {
                return(BadRequest(new ErrorModel(1, 400, "Empty Body")));
            }

            var item = await _context.IngredientHeaders.FirstOrDefaultAsync(a => a.ItemId == IngredientHeaderForDetailDto.ItemId);

            if (item != null)
            {
                return(BadRequest(new ErrorModel(4, 400, "Recipe For Item Already exist")));
            }

            var IngredientHeaderToCreate = _mapper.Map <IngredientHeader>(IngredientHeaderForDetailDto);

            await _repository.CreateIngredient(IngredientHeaderToCreate);

            if (await _repository.SaveAll())
            {
                var IngredientHeaderToReturn = _mapper.Map <IngredientHeaderForDetailDto>(IngredientHeaderToCreate);
                return(CreatedAtRoute(nameof(GetIngredient), new { IngredientHeaderToCreate.Id }, IngredientHeaderToReturn));
            }

            return(BadRequest(new ErrorModel(2, 400, "Could not create Ingredient")));
        }
コード例 #2
0
        public async Task <IActionResult> UpdateIngredient(int id, IngredientHeaderForDetailDto ingredientHeaderForDetailDto)
        {
            if (ingredientHeaderForDetailDto == null)
            {
                return(BadRequest(new ErrorModel(1, 400, "empty body")));
            }

            var ingredientHeaderFromRepository = await _repository.GetIngredient(id);

            if (ingredientHeaderFromRepository == null)
            {
                return(BadRequest(new ErrorModel(3, 400, "Ingredient not available")));
            }

            ingredientHeaderFromRepository.ItemId = ingredientHeaderForDetailDto.ItemId;

            ingredientHeaderFromRepository.Description = ingredientHeaderForDetailDto.Description;
            ingredientHeaderFromRepository.Method      = ingredientHeaderForDetailDto.Method;
            ingredientHeaderFromRepository.ServingSize = ingredientHeaderForDetailDto.ServingSize;

            foreach (var pod in ingredientHeaderFromRepository.IngredientsDetail)
            {
                _repository.Delete(pod);
            }

            ingredientHeaderFromRepository.IngredientsDetail.Clear();

            foreach (var pod in ingredientHeaderForDetailDto.IngredientDetails)
            {
                ingredientHeaderFromRepository.IngredientsDetail.Add(new IngredientDetail
                {
                    ItemId   = pod.ItemId,
                    Quantity = (decimal)pod.Quantity
                });
            }

            if (await _repository.SaveAll())
            {
                return(NoContent());
            }


            throw new System.Exception($"Updating Ingredient {id} failed on save");
        }