public async Task RemoveMealFromDiet(int dietId, int mealId) { DietMeal dietMeal = await _ctx.DietMeals.Where(dm => dm.DietId == dietId && dm.MealId == mealId).FirstOrDefaultAsync(); _ctx.Remove(dietMeal); await _ctx.SaveChangesAsync(); }
public async Task <bool> RemoveMealFromDietAsync(int dietId, int mealId) { _logger.LogInformation("Entered in RemoveMealFromDiet with dietId {}, mealId {}", dietId, mealId); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); DietMeal dietMeal = await _ctx.DietMeals.Where(dm => dm.DietId == dietId && dm.MealId == mealId).FirstOrDefaultAsync(); if (dietMeal == null) { stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); _logger.LogError("No Meal with id {} is associated with diet {}", mealId, dietId); return(false); } _ctx.Remove(dietMeal); await _ctx.SaveChangesAsync(); stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(true); }
public async Task <bool> AddMealToDietAsync(int dietId, int mealId) { _logger.LogInformation("Entered in AddMealToDiet with dietId {}, mealId {}", dietId, mealId); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); bool exists = await _ctx.DietMeals.AnyAsync(dm => dm.DietId == dietId && dm.MealId == mealId); if (!exists) { DietMeal dietMeal = new DietMeal { MealId = mealId, DietId = dietId }; await _ctx.AddAsync(dietMeal); await _ctx.SaveChangesAsync(); stopWatch.Stop(); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(true); } _logger.LogWarning("Meal with id {} is already present in the diet {}", mealId, dietId); _logger.LogInformation("Elapsed time in {0} ms", stopWatch.ElapsedMilliseconds); return(false); }
public async Task <bool> AddMealToDiet(int dietId, int mealId) { bool exists = await _ctx.DietMeals.AnyAsync(dm => dm.DietId == dietId && dm.MealId == mealId); if (!exists) { DietMeal dietMeal = new DietMeal { MealId = mealId, DietId = dietId }; await _ctx.AddAsync(dietMeal); await _ctx.SaveChangesAsync(); return(true); } return(false); }