creek

The AI Image Editor of 2030

lib/data/repos/file_repo.dart (3736B)


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