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> getFriends() async { final response = await _client.get(_prefix); final List data = response.data; return data.map((json) => FriendResponse.fromJson(json)).toList(); } Future> getOutgoingRequests() async { final response = await _client.get('$_prefix/requests/outgoing'); final List data = response.data; return data.map((json) => FriendRequestResponse.fromJson(json)).toList(); } Future 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 acceptRequest(String friendshipId) async { final response = await _client.post( '$_prefix/requests/$friendshipId/accept', ); return FriendRequestResponse.fromJson(response.data); } Future declineRequest(String friendshipId) async { final response = await _client.post( '$_prefix/requests/$friendshipId/decline', ); return FriendRequestResponse.fromJson(response.data); } Future removeFriend(String friendshipId) async { await _client.delete('$_prefix/$friendshipId'); } Future 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 json) { return FriendResponse( id: json['id'] as String, friend: UserBasicInfo.fromJson(json['friend'] as Map), 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 json) { return UserBasicInfo( id: json['id'] as String, username: json['username'] as String, avatarUrl: json['avatar_url'] 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 json) { return FriendRequestResponse( id: json['id'] as String, sender: UserBasicInfo.fromJson(json['sender'] as Map), recipient: UserBasicInfo.fromJson( json['recipient'] as Map, ), content: json['content'] as String?, status: json['status'] as String, createdAt: DateTime.parse(json['created_at'] as String), ); } }