89 lines
2.6 KiB
Dart
89 lines
2.6 KiB
Dart
import '../../core/network/i_api_client.dart';
|
|
import 'models/friend_request.dart';
|
|
|
|
abstract class FriendRepository {
|
|
Future<List<FriendUser>> getFriends();
|
|
Future<FriendRequest> getRequestById(String friendshipId);
|
|
Future<Map<String, FriendRequest>> getRequestsByIds(
|
|
List<String> friendshipIds,
|
|
);
|
|
Future<FriendRequest> acceptRequest(String friendshipId);
|
|
Future<FriendRequest> declineRequest(String friendshipId);
|
|
}
|
|
|
|
class FriendRepositoryImpl implements FriendRepository {
|
|
final IApiClient _apiClient;
|
|
static const _prefix = '/api/v1/friends';
|
|
|
|
FriendRepositoryImpl(this._apiClient);
|
|
|
|
@override
|
|
Future<List<FriendUser>> getFriends() async {
|
|
final response = await _apiClient.get<List<dynamic>>(_prefix);
|
|
final data = response.data;
|
|
if (data == null) {
|
|
throw StateError('Invalid getFriends response: empty payload');
|
|
}
|
|
return data
|
|
.map((item) => item as Map<String, dynamic>)
|
|
.map(
|
|
(item) => FriendUser.fromJson(item['friend'] as Map<String, dynamic>),
|
|
)
|
|
.toList(growable: false);
|
|
}
|
|
|
|
@override
|
|
Future<FriendRequest> getRequestById(String friendshipId) async {
|
|
final response = await _apiClient.get<Map<String, dynamic>>(
|
|
'$_prefix/requests/$friendshipId',
|
|
);
|
|
final data = response.data;
|
|
if (data == null) {
|
|
throw StateError('Invalid getRequestById response: empty payload');
|
|
}
|
|
return FriendRequest.fromJson(data);
|
|
}
|
|
|
|
@override
|
|
Future<Map<String, FriendRequest>> getRequestsByIds(
|
|
List<String> friendshipIds,
|
|
) async {
|
|
if (friendshipIds.isEmpty) {
|
|
return const <String, FriendRequest>{};
|
|
}
|
|
|
|
final pairs = await Future.wait(
|
|
friendshipIds.map((id) async {
|
|
final request = await getRequestById(id);
|
|
return MapEntry(id, request);
|
|
}),
|
|
);
|
|
|
|
return Map<String, FriendRequest>.fromEntries(pairs);
|
|
}
|
|
|
|
@override
|
|
Future<FriendRequest> acceptRequest(String friendshipId) async {
|
|
final response = await _apiClient.post<Map<String, dynamic>>(
|
|
'$_prefix/requests/$friendshipId/accept',
|
|
);
|
|
final data = response.data;
|
|
if (data == null) {
|
|
throw StateError('Invalid acceptRequest response: empty payload');
|
|
}
|
|
return FriendRequest.fromJson(data);
|
|
}
|
|
|
|
@override
|
|
Future<FriendRequest> declineRequest(String friendshipId) async {
|
|
final response = await _apiClient.post<Map<String, dynamic>>(
|
|
'$_prefix/requests/$friendshipId/decline',
|
|
);
|
|
final data = response.data;
|
|
if (data == null) {
|
|
throw StateError('Invalid declineRequest response: empty payload');
|
|
}
|
|
return FriendRequest.fromJson(data);
|
|
}
|
|
}
|