import 'package:dio/dio.dart'; import '../models/project.dart'; import 'api_client.dart'; class ProjectsApi { final Dio _dio; const ProjectsApi(this._dio); Future> getAll({String? status}) async { try { final response = await _dio.get( '/api/projects', queryParameters: status != null ? {'status': status} : null, ); final data = response.data as Map; final list = data['projects'] as List; return list .map((e) => Project.fromJson(e as Map)) .toList(); } on DioException catch (e) { throw dioToApp(e); } } Future getOne(int id) async { try { final response = await _dio.get('/api/projects/$id'); return Project.fromJson(response.data as Map); } on DioException catch (e) { throw dioToApp(e); } } Future create({ required String title, String? description, String? goal, String? color, }) async { try { final response = await _dio.post('/api/projects', data: { 'title': title, if (description != null && description.isNotEmpty) 'description': description, if (goal != null && goal.isNotEmpty) 'goal': goal, ?'color': color, }); return Project.fromJson(response.data as Map); } on DioException catch (e) { throw dioToApp(e); } } Future update(int id, Map fields) async { try { final response = await _dio.patch('/api/projects/$id', data: fields); return Project.fromJson(response.data as Map); } on DioException catch (e) { throw dioToApp(e); } } Future delete(int id) async { try { await _dio.delete('/api/projects/$id'); } on DioException catch (e) { throw dioToApp(e); } } }