import '../../core/exceptions.dart'; import '../api/milestones_api.dart'; import '../local/database.dart'; import '../models/milestone.dart'; /// Milestones repository with read-through caching for Tier 2 offline support. /// Mirrors the pattern in NotesRepository — see that file for full notes. class MilestonesRepository { final MilestonesApi _api; final FabledDatabase _db; const MilestonesRepository(this._api, this._db); Future> getAll(int projectId, {String? status}) async { try { final milestones = await _api.getAll(projectId, status: status); // Only mirror the full per-project list to cache on the unfiltered // fetch; a status filter would otherwise drop entries from cache. if (status == null) { await _db.replaceMilestonesForProject(projectId, milestones); } else { for (final m in milestones) { await _db.upsertMilestone(m); } } return milestones; } on NetworkException { final cached = await _db.getMilestonesForProject(projectId); if (cached.isEmpty) rethrow; if (status != null) { return cached.where((m) => m.status == status).toList(); } return cached; } } Future create( int projectId, { required String title, String? description, int orderIndex = 0, }) async { final milestone = await _api.create( projectId, title: title, description: description, orderIndex: orderIndex, ); await _db.upsertMilestone(milestone); return milestone; } Future update( int projectId, int milestoneId, Map fields, ) async { final updated = await _api.update(projectId, milestoneId, fields); await _db.upsertMilestone(updated); return updated; } Future delete(int projectId, int milestoneId) async { await _api.delete(projectId, milestoneId); await _db.deleteMilestone(milestoneId); } }