/// <summary> /// Updates <see cref="Food"/> info in database, if food doesn't exist creates new one /// </summary> /// <param name="restaurant">Restaurant object</param> /// <param name="sheetFoods">List of foods to add and update</param> private void AddAndUpdateFood(Restaurant restaurant, List <Food> sheetFoods) { foreach (var food in sheetFoods) { var dbFood = restaurant.Foods.SingleOrDefault(f => f.Name == food.Name); if (dbFood != null) { var d = _mapper.Map <FoodUpdateModel>(dbFood); d.Price = food.Price; d.Description = food.Description; _foodCommandRepository.Update(dbFood.Id, d); } else { var d = _mapper.Map <FoodInsertModel>(food); d.RestaurantId = restaurant.Id; _foodCommandRepository.Insert(d); } } foreach (var food in restaurant.Foods) { var sheetFood = sheetFoods.SingleOrDefault(f => f.Name == food.Name); if (sheetFood == null) { var udpateModel = _mapper.Map <FoodUpdateModel>(food); udpateModel.IsInactive = true; _foodCommandRepository.Update(food.Id, udpateModel); } } }
public IActionResult Put(int id, [FromBody] UpdateFoodDto model) => _mapper.Map <FoodUpdateModel>(model) .Map(x => _foodCommandRepository.Update(id, x)) .Map(x => AllOk(new { result = x })) .Reduce(_ => BadRequest(), error => error is ArgumentNotSet, x => _logger.LogError(x.ToString())) .Reduce(_ => InternalServerError(), x => _logger.LogError(x.ToString()));