コード例 #1
0
        public async Task <ActionResult <FoodItemDto> > CreateFoodItemForRestaurantAsync(Guid restaurantId, FoodItemCreationDto foodItemCreateDto)
        {
            var restaurant = await _restaurantService.GetRestaurantAsync(restaurantId);

            if (restaurant == null)
            {
                return(BadRequest("The given restaurant doesn't exists"));
            }
            var foodItemCreate = _mapper.Map <FoodItem>(foodItemCreateDto);

            _foodItemsService.AddFoodItemToRestaurantAsync(restaurantId, foodItemCreate);
            if (!await _foodItemsService.SaveAsync())
            {
                throw new Exception("Something went wrong");
            }
            var foodItemDto = _mapper.Map <FoodItemDto>(foodItemCreate);

            return(CreatedAtRoute("GetFoodItem", new { restaurantId = restaurantId, foodItemId = foodItemCreate.Id }, foodItemDto));
        }