creek

The AI Image Editor of 2030

lib/data/repos/project_repo.dart (3745B)


  1import 'dart:convert';
  2
  3import '../database.dart';
  4import '../models/project_model.dart';
  5
  6class ProjectRepo {
  7  Future<int> createProject(ProjectModel project) async {
  8    final db = await AppDatabase.db;
  9    return await db.insert('projects', project.toMap());
 10  }
 11
 12  Future<List<ProjectModel>> getRecentProjects(int lim) async {
 13    final db = await AppDatabase.db;
 14    final res = await db.query(
 15      'projects',
 16      where: 'parent_id IS NULL AND id != 0',
 17      orderBy: 'last_accessed_at DESC',
 18      limit: lim,
 19    );
 20    return res.map((e) => ProjectModel.fromMap(e)).toList();
 21  }
 22
 23  Future<List<ProjectModel>> getRecentProjectsAndEvents() async {
 24    final db = await AppDatabase.db;
 25    final res = await db.query(
 26      'projects',
 27      where: 'id != 0',
 28      orderBy: 'last_accessed_at DESC',
 29      limit: 10,
 30    );
 31    return res.map((e) => ProjectModel.fromMap(e)).toList();
 32  }
 33
 34  Future<List<ProjectModel>> getAllProjectsAndEvents() async {
 35    final db = await AppDatabase.db;
 36    final res = await db.query(
 37      'projects',
 38      where: 'id != 0',
 39      orderBy: 'title ASC',
 40    );
 41    return res.map((e) => ProjectModel.fromMap(e)).toList();
 42  }
 43
 44  Future<List<ProjectModel>> getAllProjects() async {
 45    final db = await AppDatabase.db;
 46    final res = await db.query(
 47      'projects',
 48      where: 'parent_id IS NULL AND id != 0',
 49      orderBy: 'last_accessed_at DESC',
 50    );
 51    return res.map((e) => ProjectModel.fromMap(e)).toList();
 52  }
 53
 54  Future<List<ProjectModel>> getEvents(int projectId) async {
 55    final db = await AppDatabase.db;
 56    final res = await db.query(
 57      'projects',
 58      where: 'parent_id = ?',
 59      whereArgs: [projectId],
 60      orderBy: 'created_at DESC',
 61    );
 62    return res.map((e) => ProjectModel.fromMap(e)).toList();
 63  }
 64
 65  Future<ProjectModel?> getProjectById(int id) async {
 66    final db = await AppDatabase.db;
 67    final res = await db.query('projects', where: 'id = ?', whereArgs: [id]);
 68    if (res.isNotEmpty) return ProjectModel.fromMap(res.first);
 69    return null;
 70  }
 71
 72  Future<void> updateProject(
 73    int id, {
 74    String? title,
 75    String? description,
 76  }) async {
 77    final db = await AppDatabase.db;
 78    final Map<String, dynamic> updates = {};
 79    if (title != null) updates['title'] = title;
 80    if (description != null) updates['description'] = description;
 81
 82    if (updates.isNotEmpty) {
 83      await db.update('projects', updates, where: 'id = ?', whereArgs: [id]);
 84    }
 85  }
 86
 87  Future<void> updateStylesheet(int id, String jsonStylesheet) async {
 88    final db = await AppDatabase.db;
 89    await db.update(
 90      'projects',
 91      {'global_stylesheet': jsonStylesheet},
 92      where: 'id = ?',
 93      whereArgs: [id],
 94    );
 95  }
 96
 97  Future<void> touchProject(int id) async {
 98    final db = await AppDatabase.db;
 99    await db.update(
100      'projects',
101      {'last_accessed_at': DateTime.now().toIso8601String()},
102      where: 'id = ?',
103      whereArgs: [id],
104    );
105  }
106
107  Future<List<int>> getAllSubEventIds(int projectId) async {
108    final db = await AppDatabase.db;
109    final res = await db.query(
110      'projects',
111      columns: ['id'],
112      where: 'parent_id = ?',
113      whereArgs: [projectId],
114    );
115    return res.map((e) => e['id'] as int).toList();
116  }
117
118  Future<void> deleteProject(int id) async {
119    final db = await AppDatabase.db;
120    await db.delete('projects', where: 'id = ?', whereArgs: [id]);
121  }
122
123  Future<void> updateAssets(int projectId, List<String> assets) async {
124    final db = await AppDatabase.db;
125
126    await db.update(
127      'projects',
128      {
129        'assets_path': jsonEncode(assets),
130        'last_accessed_at': DateTime.now().toIso8601String(),
131      },
132      where: 'id = ?',
133      whereArgs: [projectId],
134    );
135  }
136}