file_repo.dart (3736B)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | import 'dart:convert'; import '../database.dart'; import '../models/file_model.dart'; class FileRepo { Future<void> addFile(FileModel file) async { final db = await AppDatabase.db; await db.insert('files', file.toMap()); } // Gets all files, regardless of project ID, ordered by last_updated Future<List<FileModel>> getAllFiles() async { final db = await AppDatabase.db; final res = await db.query('files', orderBy: 'last_updated DESC'); return res.map((e) => FileModel.fromMap(e)).toList(); } // Gets the most recent files across all projects, capped by limit Future<List<FileModel>> getRecentFiles({int limit = 10}) async { final db = await AppDatabase.db; final res = await db.query( 'files', orderBy: 'last_updated DESC', limit: limit, ); return res.map((e) => FileModel.fromMap(e)).toList(); } // Retained original function for fetching files by a specific project ID Future<List<FileModel>> getFiles(int projectId) async { final db = await AppDatabase.db; final res = await db.query( 'files', where: 'project_id = ?', whereArgs: [projectId], orderBy: 'last_updated DESC', ); return res.map((e) => FileModel.fromMap(e)).toList(); } Future<FileModel?> getById(String id) async { final db = await AppDatabase.db; final res = await db.query('files', where: 'id = ?', whereArgs: [id]); if (res.isNotEmpty) return FileModel.fromMap(res.first); return null; } Future<void> updateDetails( String id, { String? name, String? description, List<String>? tags, }) async { final db = await AppDatabase.db; final Map<String, dynamic> updates = { 'last_updated': DateTime.now().toIso8601String(), }; if (name != null) updates['name'] = name; if (description != null) updates['description'] = description; if (tags != null) updates['tags'] = jsonEncode(tags); await db.update('files', updates, where: 'id = ?', whereArgs: [id]); } Future<void> touchFile(String id) async { final db = await AppDatabase.db; await db.update( 'files', {'last_updated': DateTime.now().toIso8601String()}, where: 'id = ?', whereArgs: [id], ); } Future<void> deleteFile(String id) async { final db = await AppDatabase.db; await db.delete('files', where: 'id = ?', whereArgs: [id]); } Future<List<String>> getAllFilePathsForProjectIds( List<int> projectIds, ) async { if (projectIds.isEmpty) return []; final db = await AppDatabase.db; final idList = projectIds.join(','); final res = await db.rawQuery( 'SELECT file_path FROM files WHERE project_id IN ($idList)', ); return res.map((e) => e['file_path'] as String).toList(); } Future<List<FileModel>> getFilesForProjectAndEvents(int projectId) async { final db = await AppDatabase.db; // Fetch events of this project final eventRows = await db.query( 'projects', where: 'parent_id = ?', whereArgs: [projectId], ); final eventIds = eventRows.map((e) => e['id'] as int).toList(); final allIds = [projectId, ...eventIds]; final res = await db.query( 'files', where: 'project_id IN (${List.filled(allIds.length, '?').join(',')})', whereArgs: allIds, orderBy: 'last_updated DESC', ); return res.map((e) => FileModel.fromMap(e)).toList(); } Future<FileModel?> getByFilePath(String path) async { final db = await AppDatabase.db; final res = await db.query( 'files', where: 'file_path = ?', whereArgs: [path], limit: 1, ); if (res.isNotEmpty) return FileModel.fromMap(res.first); return null; } } |