Exemple #1
0
        public async Task UpdateFoodAsync(Guid foodId, FoodRequestModel foodRequestModel)
        {
            var food = await foodAppDbContext.Foods.FirstOrDefaultAsync(x => x.Id == foodId);

            FoodMapper.Map(foodRequestModel, food);
            foodAppDbContext.Update(food);
            await foodAppDbContext.SaveChangesAsync();
        }
Exemple #2
0
 public static Food Map(FoodRequestModel foodRequestModel)
 {
     return(new Food()
     {
         FoodCategoryId = foodRequestModel.FoodCategoryId,
         Name = foodRequestModel.Name,
         UnitPrice = foodRequestModel.UnitPrice
     });
 }
Exemple #3
0
        public async Task <Guid> CreateFoodAsync(FoodRequestModel foodRequestModel)
        {
            var food = FoodMapper.Map(foodRequestModel);

            food.Id = Guid.NewGuid();

            await foodAppDbContext.AddAsync(food);

            await foodAppDbContext.SaveChangesAsync();

            return(food.Id);
        }
Exemple #4
0
        public async Task <IActionResult> CreateFood([FromBody] FoodRequestModel foodRequestModel)
        {
            logger.LogInformation("CreateFood request started");
            var foodCategory = await this.foodService.GetFoodCategoryAsync(foodRequestModel.FoodCategoryId);

            if (foodCategory == null)
            {
                logger.LogWarning($"Invalid foodCateogoryId : {foodRequestModel.FoodCategoryId}");
                return(this.BadRequest(new BadRequestResponseModel {
                    ErrorMessage = "Invalid foodCategoryId"
                }));
            }
            var foodId = await this.foodService.CreateFoodAsync(foodRequestModel);

            logger.LogInformation("CreateFood completed successfully");
            return(CreatedAtAction("GetFood", new { id = foodId }, new { id = foodId }));
        }
Exemple #5
0
 public static void Map(FoodRequestModel srcFoodRequestModel, Food dstFood)
 {
     dstFood.FoodCategoryId = srcFoodRequestModel.FoodCategoryId;
     dstFood.Name           = srcFoodRequestModel.Name;
     dstFood.UnitPrice      = srcFoodRequestModel.UnitPrice;
 }
Exemple #6
0
        public async Task <IActionResult> UpdateFood([FromRoute(Name = "id")] Guid foodId, [FromBody] FoodRequestModel foodRequestModel)
        {
            logger.LogInformation("UpdateFood request started");
            var food = await this.foodService.GetFoodAsync(foodId);

            if (food == null)
            {
                logger.LogWarning($"Invalid foodId : {foodId}");
                return(this.BadRequest(new BadRequestResponseModel {
                    ErrorMessage = "Invalid foodId"
                }));
            }

            var foodCategory = await this.foodService.GetFoodCategoryAsync(foodRequestModel.FoodCategoryId);

            if (foodCategory == null)
            {
                logger.LogWarning($"Invalid foodCateogoryId : {foodRequestModel.FoodCategoryId}");
                return(this.BadRequest(new BadRequestResponseModel {
                    ErrorMessage = "Invalid foodCategoryId"
                }));
            }
            await this.foodService.UpdateFoodAsync(foodId, foodRequestModel);

            logger.LogInformation("UpdateFood completed successfully");
            return(this.NoContent());
        }