Files
social-app/apps/lib/features/friends/data/friends_api.dart
T

97 lines
2.8 KiB
Dart
Raw Normal View History

import 'package:social_app/core/api/i_api_client.dart';
class FriendsApi {
final IApiClient _client;
static const _prefix = '/api/v1/friends';
FriendsApi(this._client);
Future<List<FriendResponse>> getFriends() async {
final response = await _client.get(_prefix);
final List<dynamic> data = response.data;
return data.map((json) => FriendResponse.fromJson(json)).toList();
}
Future<List<FriendResponse>> getIncomingRequests() async {
final response = await _client.get('$_prefix/requests/inbox');
final List<dynamic> data = response.data;
return data.map((json) => FriendResponse.fromJson(json)).toList();
}
Future<List<FriendResponse>> getOutgoingRequests() async {
final response = await _client.get('$_prefix/requests/outgoing');
final List<dynamic> data = response.data;
return data.map((json) => FriendResponse.fromJson(json)).toList();
}
Future<FriendResponse> sendRequest(String targetUserId) async {
final response = await _client.post(
'$_prefix/requests',
data: {'target_user_id': targetUserId},
);
return FriendResponse.fromJson(response.data);
}
Future<FriendResponse> acceptRequest(String friendshipId) async {
final response = await _client.post(
'$_prefix/requests/$friendshipId/accept',
);
return FriendResponse.fromJson(response.data);
}
Future<FriendResponse> declineRequest(String friendshipId) async {
final response = await _client.post(
'$_prefix/requests/$friendshipId/decline',
);
return FriendResponse.fromJson(response.data);
}
Future<void> removeFriend(String friendshipId) async {
await _client.delete('$_prefix/$friendshipId');
}
}
class FriendResponse {
final String id;
final UserBasicInfo friend;
final String status;
final DateTime createdAt;
final DateTime? acceptedAt;
FriendResponse({
required this.id,
required this.friend,
required this.status,
required this.createdAt,
this.acceptedAt,
});
factory FriendResponse.fromJson(Map<String, dynamic> json) {
return FriendResponse(
id: json['id'] as String,
friend: UserBasicInfo.fromJson(json['friend'] as Map<String, dynamic>),
status: json['status'] as String,
createdAt: DateTime.parse(json['created_at'] as String),
acceptedAt: json['accepted_at'] != null
? DateTime.parse(json['accepted_at'] as String)
: null,
);
}
}
class UserBasicInfo {
final String id;
final String username;
final String? avatarUrl;
UserBasicInfo({required this.id, required this.username, this.avatarUrl});
factory UserBasicInfo.fromJson(Map<String, dynamic> json) {
return UserBasicInfo(
id: json['id'] as String,
username: json['username'] as String,
avatarUrl: json['avatar_url'] as String?,
);
}
}