getAllEntries method

Future<List<DiaryEntry>> getAllEntries({
  1. required String userId,
})

Retrieves all DiaryEntrys for a given user, ordered by most recent date.

Implementation

Future<List<DiaryEntry>> getAllEntries({required String userId}) async {
  final db = await database;

  final maps = await db.query(
    'diary_entries',
    where: 'userId = ?',
    whereArgs: [userId],
    orderBy: 'date DESC',
  );

  return maps.map((map) {
    final raw = map['photoUrls'] as String?;
    final list =
        (raw == null || raw.trim().isEmpty)
            ? <String>[]
            : raw.split(',').where((e) => e.trim().isNotEmpty).toList();

    return DiaryEntry.fromJson({...map, 'photoUrls': list});
  }).toList();
}