getMealID method

Future<int?> getMealID(
  1. int userID,
  2. int year,
  3. int month,
  4. int day,
  5. String mealType,
)

Retrieves the meal ID based on user ID, day, and meal type.

  • 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 the meal ID if found, otherwise null.

Implementation

Future<int?> getMealID(int userID, int year, int month, int day, String mealType) async {
  await _databaseWrapper.ensureDBIsInitialized();

  // Get the day ID
  final List<Map<String, dynamic>> dayRecords = await _databaseWrapper.database.query(
    'days',
    where: 'year = ? AND month = ? AND day = ?',
    whereArgs: [year, month, day],
  );
  if (dayRecords.isEmpty) return null;
  final int dayID = dayRecords.first['ID'];

  // Get the meal type ID
  final List<Map<String, dynamic>> mealTypeRecords = await _databaseWrapper.database.query(
    'meal_types',
    where: 'LOWER(name) = ?',
    whereArgs: [mealType.toLowerCase()],
  );
  if (mealTypeRecords.isEmpty) return null;
  final int mealTypeID = mealTypeRecords.first['ID'];

  // Get the existing meal from day_meals
  final List<Map<String, dynamic>> dayMealRecords = await _databaseWrapper.database.query(
    'day_meals',
    where: 'fk_user_id = ? AND fk_day_id = ? AND fk_meal_type_id = ?',
    whereArgs: [userID, dayID, mealTypeID],
  );
  if (dayMealRecords.isEmpty) return null;

  // Return the meal ID
  return dayMealRecords.first['fk_meal_id'];
}