67 lines
2.2 KiB
Dart
67 lines
2.2 KiB
Dart
|
|
import 'package:dio/dio.dart';
|
||
|
|
import 'package:real_estate_mobile/core/network/api_client.dart';
|
||
|
|
import 'package:real_estate_mobile/core/network/api_exceptions.dart';
|
||
|
|
|
||
|
|
class ProfileRepository {
|
||
|
|
final Dio _dio = ApiClient.instance.dio;
|
||
|
|
|
||
|
|
/// Fetch profile based on role.
|
||
|
|
/// Agent: GET /agents/profile/me
|
||
|
|
/// User: GET /users/profile/me
|
||
|
|
Future<Map<String, dynamic>> getMyProfile(String role) async {
|
||
|
|
try {
|
||
|
|
final endpoint = role == 'AGENT'
|
||
|
|
? '/agents/profile/me'
|
||
|
|
: '/users/profile/me';
|
||
|
|
final response = await _dio.get(endpoint);
|
||
|
|
return response.data['data'] as Map<String, dynamic>;
|
||
|
|
} on DioException catch (e) {
|
||
|
|
if (e.error is ApiException) throw e.error as ApiException;
|
||
|
|
throw ApiException(
|
||
|
|
message: e.message ?? 'Failed to fetch profile',
|
||
|
|
statusCode: e.response?.statusCode,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Update profile based on role.
|
||
|
|
/// Agent: PUT /agents/profile
|
||
|
|
/// User: PATCH /users/profile/me
|
||
|
|
Future<Map<String, dynamic>> updateProfile(
|
||
|
|
String role,
|
||
|
|
Map<String, dynamic> data,
|
||
|
|
) async {
|
||
|
|
try {
|
||
|
|
final Response response;
|
||
|
|
if (role == 'AGENT') {
|
||
|
|
response = await _dio.put('/agents/profile', data: data);
|
||
|
|
} else {
|
||
|
|
response = await _dio.patch('/users/profile/me', data: data);
|
||
|
|
}
|
||
|
|
return response.data['data'] as Map<String, dynamic>;
|
||
|
|
} on DioException catch (e) {
|
||
|
|
if (e.error is ApiException) throw e.error as ApiException;
|
||
|
|
throw ApiException(
|
||
|
|
message: e.message ?? 'Failed to update profile',
|
||
|
|
statusCode: e.response?.statusCode,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Future<String> getAvatarUploadUrl(String role, String fileName) async {
|
||
|
|
try {
|
||
|
|
final endpoint = role == 'AGENT'
|
||
|
|
? '/upload/avatar-presigned-url'
|
||
|
|
: '/upload/user-avatar-presigned-url';
|
||
|
|
final response = await _dio.post(endpoint, data: {'fileName': fileName});
|
||
|
|
return response.data['data']['url'] as String;
|
||
|
|
} on DioException catch (e) {
|
||
|
|
if (e.error is ApiException) throw e.error as ApiException;
|
||
|
|
throw ApiException(
|
||
|
|
message: e.message ?? 'Failed to get upload URL',
|
||
|
|
statusCode: e.response?.statusCode,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|