fetchLogs method
Implementation
Future<List<Map<String, dynamic>>> fetchLogs({
DateTime? startDate,
DateTime? endDate,
String? url,
String? method,
String? message,
}) async {
final db = await instance.database;
// Initialize whereClause as an empty string to avoid null issues
String whereClause = '';
List<dynamic> whereArgs = [];
if (startDate != null) {
whereClause +=
whereClause.isNotEmpty ? ' AND timestamp >= ?' : 'timestamp >= ?';
whereArgs.add(startDate.toIso8601String());
}
if (endDate != null) {
whereClause +=
whereClause.isNotEmpty ? ' AND timestamp <= ?' : 'timestamp <= ?';
whereArgs.add(endDate.toIso8601String());
}
if (url != null) {
whereClause += whereClause.isNotEmpty ? ' AND url LIKE ?' : 'url LIKE ?';
whereArgs.add('%$url%');
}
if (method != null) {
whereClause += whereClause.isNotEmpty ? ' AND method = ?' : 'method = ?';
whereArgs.add(method);
}
if (message != null) {
whereClause +=
whereClause.isNotEmpty ? ' AND message LIKE ?' : 'message LIKE ?';
whereArgs.add('%$message%');
}
// If whereClause is empty, pass null for "where"
return await db.query(
'api_logs',
where: whereClause.isNotEmpty ? whereClause : null,
whereArgs: whereArgs.isNotEmpty ? whereArgs : null,
orderBy: 'timestamp DESC',
);
}