def7519feb
- app.dart: add braces to single-statement if body - briefing_api.dart: escape <id> in doc comment (unintended HTML) - notes_api.dart, tasks_api.dart, projects_api.dart: use null-aware map elements (?'key': value) instead of if-null guards - task_edit_screen.dart: remove unused tasks_api.dart import - task_edit_screen.dart, project_selector.dart: suppress deprecated_member_use on DropdownButtonFormField.value (value is the controlled-widget param; switching to initialValue would break current-selection display) - project_selector.dart: use _ instead of __ for ignored error params
72 lines
1.9 KiB
Dart
72 lines
1.9 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../models/project.dart';
|
|
import 'api_client.dart';
|
|
|
|
class ProjectsApi {
|
|
final Dio _dio;
|
|
const ProjectsApi(this._dio);
|
|
|
|
Future<List<Project>> getAll({String? status}) async {
|
|
try {
|
|
final response = await _dio.get(
|
|
'/api/projects',
|
|
queryParameters: status != null ? {'status': status} : null,
|
|
);
|
|
final data = response.data as Map<String, dynamic>;
|
|
final list = data['projects'] as List<dynamic>;
|
|
return list
|
|
.map((e) => Project.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
Future<Project> getOne(int id) async {
|
|
try {
|
|
final response = await _dio.get('/api/projects/$id');
|
|
return Project.fromJson(response.data as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
Future<Project> 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<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
Future<Project> update(int id, Map<String, dynamic> fields) async {
|
|
try {
|
|
final response = await _dio.patch('/api/projects/$id', data: fields);
|
|
return Project.fromJson(response.data as Map<String, dynamic>);
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
|
|
Future<void> delete(int id) async {
|
|
try {
|
|
await _dio.delete('/api/projects/$id');
|
|
} on DioException catch (e) {
|
|
throw dioToApp(e);
|
|
}
|
|
}
|
|
}
|