import 'package:dio/dio.dart'; import '../models/note.dart'; import 'api_client.dart'; class NotesApi { final Dio _dio; const NotesApi(this._dio); Future> getAll() async { try { final response = await _dio.get('/api/notes'); final data = response.data as Map; final list = data['notes'] as List; return list.map((e) => Note.fromJson(e as Map)).toList(); } on DioException catch (e) { throw dioToApp(e); } } Future getOne(int id) async { try { final response = await _dio.get('/api/notes/$id'); return Note.fromJson(response.data as Map); } on DioException catch (e) { throw dioToApp(e); } } Future create( String title, String body, { List tags = const [], int? projectId, }) async { try { final response = await _dio.post('/api/notes', data: { 'title': title, 'body': body, 'tags': tags, if (projectId != null) 'project_id': projectId, }); return Note.fromJson(response.data as Map); } on DioException catch (e) { throw dioToApp(e); } } Future update( int id, String title, String body, { List tags = const [], int? projectId, bool clearProject = false, }) async { try { final response = await _dio.put('/api/notes/$id', data: { 'title': title, 'body': body, 'tags': tags, if (clearProject) 'project_id': null else if (projectId != null) 'project_id': projectId, }); return Note.fromJson(response.data as Map); } on DioException catch (e) { throw dioToApp(e); } } Future delete(int id) async { try { await _dio.delete('/api/notes/$id'); } on DioException catch (e) { throw dioToApp(e); } } }