82 lines
2.1 KiB
Dart
82 lines
2.1 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,
|
|
String sort = 'updated_at',
|
|
String order = 'desc',
|
|
}) async {
|
|
try {
|
|
final response = await _dio.get(
|
|
'/api/projects',
|
|
queryParameters: {
|
|
'sort': sort,
|
|
'order': order,
|
|
if (status != null) 'status': status,
|
|
},
|
|
);
|
|
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,
|
|
String status = 'active',
|
|
}) async {
|
|
try {
|
|
final response = await _dio.post('/api/projects', data: {
|
|
'title': title,
|
|
'status': status,
|
|
if (description != null && description.isNotEmpty)
|
|
'description': description,
|
|
if (goal != null && goal.isNotEmpty) 'goal': goal,
|
|
if (color != null) '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);
|
|
}
|
|
}
|
|
}
|