49 lines
1.4 KiB
Dart
49 lines
1.4 KiB
Dart
import "../config/api_config.dart";
|
|
import "../models/photo.dart";
|
|
import "api_client.dart";
|
|
import "mock_data.dart";
|
|
|
|
class PhotoService {
|
|
final ApiClient _client;
|
|
|
|
PhotoService(this._client);
|
|
|
|
Future<List<Photo>> fetchPhotos({bool activeOnly = false}) async {
|
|
if (ApiConfig.useMockData) {
|
|
final items = List<Photo>.from(MockDataStore.photos);
|
|
if (activeOnly) {
|
|
return items.where((item) => item.active).toList();
|
|
}
|
|
return items;
|
|
}
|
|
final query = activeOnly ? {"active": "true"} : null;
|
|
final data = await _client.getList(ApiConfig.photos, query: query);
|
|
return data
|
|
.map((item) => Photo.fromJson(item as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
|
|
Future<Photo> createPhoto(Photo photo) async {
|
|
if (ApiConfig.useMockData) {
|
|
final created = Photo(
|
|
id: "photo-${DateTime.now().millisecondsSinceEpoch}",
|
|
url: photo.url,
|
|
caption: photo.caption,
|
|
active: photo.active,
|
|
);
|
|
MockDataStore.photos.add(created);
|
|
return created;
|
|
}
|
|
final data = await _client.post(ApiConfig.photos, photo.toJson());
|
|
return Photo.fromJson(data);
|
|
}
|
|
|
|
Future<void> deletePhoto(String id) async {
|
|
if (ApiConfig.useMockData) {
|
|
MockDataStore.photos.removeWhere((item) => item.id == id);
|
|
return;
|
|
}
|
|
await _client.delete("${ApiConfig.photos}/$id");
|
|
}
|
|
}
|