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

140 lines
3.9 KiB
Dart

import 'package:social_app/data/network/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<FriendRequestResponse>> getOutgoingRequests() async {
final response = await _client.get('$_prefix/requests/outgoing');
final List<dynamic> data = response.data;
return data.map((json) => FriendRequestResponse.fromJson(json)).toList();
}
Future<FriendRequestResponse> sendRequest(
String targetUserId, {
String? content,
}) async {
final data = {'target_user_id': targetUserId, 'content': content};
final response = await _client.post('$_prefix/requests', data: data);
return FriendRequestResponse.fromJson(response.data);
}
Future<FriendRequestResponse> acceptRequest(String friendshipId) async {
final response = await _client.post(
'$_prefix/requests/$friendshipId/accept',
);
return FriendRequestResponse.fromJson(response.data);
}
Future<FriendRequestResponse> declineRequest(String friendshipId) async {
final response = await _client.post(
'$_prefix/requests/$friendshipId/decline',
);
return FriendRequestResponse.fromJson(response.data);
}
Future<void> removeFriend(String friendshipId) async {
await _client.delete('$_prefix/$friendshipId');
}
Future<FriendRequestResponse> getRequestById(String friendshipId) async {
final response = await _client.get('$_prefix/requests/$friendshipId');
return FriendRequestResponse.fromJson(response.data);
}
}
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;
final String? phone;
final String? bio;
UserBasicInfo({
required this.id,
required this.username,
this.avatarUrl,
this.phone,
this.bio,
});
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?,
phone: json['phone'] as String?,
bio: json['bio'] as String?,
);
}
}
class FriendRequestResponse {
final String id;
final UserBasicInfo sender;
final UserBasicInfo recipient;
final String? content;
final String status;
final DateTime createdAt;
FriendRequestResponse({
required this.id,
required this.sender,
required this.recipient,
this.content,
required this.status,
required this.createdAt,
});
factory FriendRequestResponse.fromJson(Map<String, dynamic> json) {
final rawContent = json['content'] as Map<String, dynamic>?;
final content = rawContent?['message'] as String?;
return FriendRequestResponse(
id: json['id'] as String,
sender: UserBasicInfo.fromJson(json['sender'] as Map<String, dynamic>),
recipient: UserBasicInfo.fromJson(
json['recipient'] as Map<String, dynamic>,
),
content: content,
status: json['status'] as String,
createdAt: DateTime.parse(json['created_at'] as String),
);
}
}