deleteMeal method
Deletes a meal and its corresponding day_meals entry.
userID: The ID of the user.year: The year for the specified day.month: The month for the specified day.day: The day for the specified day.mealType: The meal type (e.g., "breakfast", "lunch").
Returns true if the meal and its corresponding day_meal entry were successfully deleted, otherwise false.
Implementation
Future<bool> deleteMeal({required Meal meal, required int userID}) async {
final int? mealID = await getMealID(userID, meal.year, meal.month, meal.day, meal.mealType);
if (mealID == null) {
return false; // Meal or day not found
}
await _databaseWrapper.ensureDBIsInitialized();
final batch = _databaseWrapper.database.batch();
// Delete from day_meals
batch.delete(
'day_meals',
where: 'fk_user_id = ? AND fk_day_id = (SELECT ID FROM days WHERE year = ? AND month = ? AND day = ?) AND fk_meal_type_id = (SELECT ID FROM meal_types WHERE LOWER(name) = ?)',
whereArgs: [userID, meal.year, meal.month, meal.day, meal.mealType.toLowerCase()],
);
// Delete the meal from the meals table
batch.delete(
'meals',
where: 'ID = ?',
whereArgs: [mealID],
);
final List<dynamic> results = await batch.commit();
return results.every((result) => result > 0);
}