getDiaryCardsFromLocalDb method

Future<List<DiaryCard>> getDiaryCardsFromLocalDb({
  1. required String userId,
  2. required int limit,
  3. required int offset,
})

Retrieves a paginated list of DiaryCards for preview display.

Each card includes id, title, date, location, and the first image (if any).

Implementation

Future<List<DiaryCard>> getDiaryCardsFromLocalDb({
  required String userId,
  required int limit,
  required int offset,
}) async {
  final db = await database;

  final maps = await db.query(
    'diary_entries',
    columns: ['id', 'title', 'date', 'location', 'photoUrls'],
    where: 'userId = ?',
    whereArgs: [userId],
    orderBy: 'date DESC',
    limit: limit,
    offset: offset,
  );

  return maps.map((map) {
    final photoUrls =
        (map['photoUrls'] as String?)
            ?.split(',')
            .where((url) => url.trim().isNotEmpty)
            .toList() ??
        [];

    return DiaryCard(
      id: map['id'] as String,
      title: map['title'] as String,
      date: DateTime.parse(map['date'] as String),
      place: map['location'] as String? ?? '',
      imageUrl: photoUrls.isNotEmpty ? photoUrls.first : '',
    );
  }).toList();
}