creek

The AI Image Editor of 2030

lib/data/repos/note_repo.dart (2636B)


 1import '../database.dart';
 2import '../models/note_model.dart';
 3
 4class NoteRepo {
 5  Future<int> addNote(NoteModel note) async {
 6    final db = await AppDatabase.db;
 7    return await db.insert('notes', note.toMap());
 8  }
 9
10  Future<List<NoteModel>> getNotesForImage(dynamic imageId) async {
11    final db = await AppDatabase.db;
12    final res = await db.query(
13      'notes',
14      where: 'image_id = ?',
15      whereArgs: [imageId],
16      orderBy: 'created_at DESC',
17    );
18
19    return res.map((e) => NoteModel.fromMap(e)).toList();
20  }
21
22  Future<List<NoteModel>> getPendingNotes() async {
23    final db = await AppDatabase.db;
24    final res = await db.query(
25      'notes',
26      where: 'status = ?',
27      whereArgs: ['pending'],
28    );
29    return res.map((e) => NoteModel.fromMap(e)).toList();
30  }
31
32  Future<List<NoteModel>> getNotesByProjectId(int projectId) async {
33    final db = await AppDatabase.db;
34    final res = await db.rawQuery(
35      '''
36      SELECT notes.* FROM notes
37      INNER JOIN images ON notes.image_id = images.id
38      WHERE images.project_id = ?
39    ''',
40      [projectId],
41    );
42    return res.map((e) => NoteModel.fromMap(e)).toList();
43  }
44
45  Future<void> updateNote(
46    int id, {
47    String? content,
48    String? category,
49    double? normX,
50    double? normY,
51    double? normWidth,
52    double? normHeight,
53    String? analysisData,
54    String? status,
55    String? cropFilePath,
56  }) async {
57    final db = await AppDatabase.db;
58    final Map<String, dynamic> updates = {};
59
60    if (content != null) updates['content'] = content;
61    if (category != null) updates['category'] = category;
62
63    if (normX != null) updates['norm_x'] = normX;
64    if (normY != null) updates['norm_y'] = normY;
65    if (normWidth != null) updates['norm_width'] = normWidth;
66    if (normHeight != null) updates['norm_height'] = normHeight;
67
68    if (analysisData != null) updates['analysis_data'] = analysisData;
69    if (cropFilePath != null) updates['crop_file_path'] = cropFilePath;
70    if (status != null) updates['status'] = status;
71
72    if (updates.isNotEmpty) {
73      await db.update('notes', updates, where: 'id = ?', whereArgs: [id]);
74    }
75  }
76
77  Future<void> deleteNote(int id) async {
78    final db = await AppDatabase.db;
79    await db.delete('notes', where: 'id = ?', whereArgs: [id]);
80  }
81
82  // Inside NoteRepo
83
84  Future<NoteModel?> getByCropPath(String path) async {
85    final db = await AppDatabase.db;
86    final maps = await db.query(
87      'notes',
88      where: 'crop_file_path = ?',
89      whereArgs: [path],
90    );
91
92    if (maps.isNotEmpty) {
93      return NoteModel.fromMap(maps.first);
94    }
95    return null;
96  }
97}