project_repo.dart (3745B)
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 127 128 129 130 131 132 133 134 135 136 | import 'dart:convert'; import '../database.dart'; import '../models/project_model.dart'; class ProjectRepo { Future<int> createProject(ProjectModel project) async { final db = await AppDatabase.db; return await db.insert('projects', project.toMap()); } Future<List<ProjectModel>> getRecentProjects(int lim) async { final db = await AppDatabase.db; final res = await db.query( 'projects', where: 'parent_id IS NULL AND id != 0', orderBy: 'last_accessed_at DESC', limit: lim, ); return res.map((e) => ProjectModel.fromMap(e)).toList(); } Future<List<ProjectModel>> getRecentProjectsAndEvents() async { final db = await AppDatabase.db; final res = await db.query( 'projects', where: 'id != 0', orderBy: 'last_accessed_at DESC', limit: 10, ); return res.map((e) => ProjectModel.fromMap(e)).toList(); } Future<List<ProjectModel>> getAllProjectsAndEvents() async { final db = await AppDatabase.db; final res = await db.query( 'projects', where: 'id != 0', orderBy: 'title ASC', ); return res.map((e) => ProjectModel.fromMap(e)).toList(); } Future<List<ProjectModel>> getAllProjects() async { final db = await AppDatabase.db; final res = await db.query( 'projects', where: 'parent_id IS NULL AND id != 0', orderBy: 'last_accessed_at DESC', ); return res.map((e) => ProjectModel.fromMap(e)).toList(); } Future<List<ProjectModel>> getEvents(int projectId) async { final db = await AppDatabase.db; final res = await db.query( 'projects', where: 'parent_id = ?', whereArgs: [projectId], orderBy: 'created_at DESC', ); return res.map((e) => ProjectModel.fromMap(e)).toList(); } Future<ProjectModel?> getProjectById(int id) async { final db = await AppDatabase.db; final res = await db.query('projects', where: 'id = ?', whereArgs: [id]); if (res.isNotEmpty) return ProjectModel.fromMap(res.first); return null; } Future<void> updateProject( int id, { String? title, String? description, }) async { final db = await AppDatabase.db; final Map<String, dynamic> updates = {}; if (title != null) updates['title'] = title; if (description != null) updates['description'] = description; if (updates.isNotEmpty) { await db.update('projects', updates, where: 'id = ?', whereArgs: [id]); } } Future<void> updateStylesheet(int id, String jsonStylesheet) async { final db = await AppDatabase.db; await db.update( 'projects', {'global_stylesheet': jsonStylesheet}, where: 'id = ?', whereArgs: [id], ); } Future<void> touchProject(int id) async { final db = await AppDatabase.db; await db.update( 'projects', {'last_accessed_at': DateTime.now().toIso8601String()}, where: 'id = ?', whereArgs: [id], ); } Future<List<int>> getAllSubEventIds(int projectId) async { final db = await AppDatabase.db; final res = await db.query( 'projects', columns: ['id'], where: 'parent_id = ?', whereArgs: [projectId], ); return res.map((e) => e['id'] as int).toList(); } Future<void> deleteProject(int id) async { final db = await AppDatabase.db; await db.delete('projects', where: 'id = ?', whereArgs: [id]); } Future<void> updateAssets(int projectId, List<String> assets) async { final db = await AppDatabase.db; await db.update( 'projects', { 'assets_path': jsonEncode(assets), 'last_accessed_at': DateTime.now().toIso8601String(), }, where: 'id = ?', whereArgs: [projectId], ); } } |