92 lines
2.7 KiB
Dart
92 lines
2.7 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:real_estate_mobile/core/constants/api_constants.dart';
|
|
import 'package:real_estate_mobile/core/network/api_client.dart';
|
|
import 'package:real_estate_mobile/core/network/api_exceptions.dart';
|
|
import 'package:real_estate_mobile/features/agents/data/models/agent_profile.dart';
|
|
import 'package:real_estate_mobile/features/agents/data/models/agent_type.dart';
|
|
|
|
class SearchAgentsResponse {
|
|
final List<AgentProfile> data;
|
|
final int total;
|
|
final int page;
|
|
final int limit;
|
|
final int totalPages;
|
|
|
|
const SearchAgentsResponse({
|
|
required this.data,
|
|
required this.total,
|
|
required this.page,
|
|
required this.limit,
|
|
required this.totalPages,
|
|
});
|
|
|
|
factory SearchAgentsResponse.fromJson(Map<String, dynamic> json) {
|
|
return SearchAgentsResponse(
|
|
data: (json['data'] as List<dynamic>)
|
|
.map((e) => AgentProfile.fromJson(e as Map<String, dynamic>))
|
|
.toList(),
|
|
total: json['total'] as int? ?? 0,
|
|
page: json['page'] as int? ?? 1,
|
|
limit: json['limit'] as int? ?? 10,
|
|
totalPages: json['totalPages'] as int? ?? 1,
|
|
);
|
|
}
|
|
}
|
|
|
|
class AgentsRepository {
|
|
final Dio _dio = ApiClient.instance.dio;
|
|
|
|
Future<SearchAgentsResponse> searchAgents({
|
|
String? agentTypeId,
|
|
int page = 1,
|
|
int limit = 10,
|
|
String? sortBy,
|
|
String? sortOrder,
|
|
}) async {
|
|
try {
|
|
final queryParams = <String, dynamic>{
|
|
'page': page,
|
|
'limit': limit,
|
|
};
|
|
|
|
if (agentTypeId != null) queryParams['agentTypeId'] = agentTypeId;
|
|
if (sortBy != null) queryParams['sortBy'] = sortBy;
|
|
if (sortOrder != null) queryParams['sortOrder'] = sortOrder;
|
|
|
|
final response = await _dio.get(
|
|
ApiConstants.agents,
|
|
queryParameters: queryParams,
|
|
);
|
|
|
|
final data = response.data['data'] as Map<String, dynamic>;
|
|
return SearchAgentsResponse.fromJson(data);
|
|
} on DioException catch (e) {
|
|
if (e.error is ApiException) {
|
|
throw e.error as ApiException;
|
|
}
|
|
throw ApiException(
|
|
message: e.message ?? 'Failed to fetch agents',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<List<AgentType>> getAgentTypes() async {
|
|
try {
|
|
final response = await _dio.get(ApiConstants.agentTypes);
|
|
final data = response.data['data'] as List<dynamic>;
|
|
return data
|
|
.map((e) => AgentType.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
} on DioException catch (e) {
|
|
if (e.error is ApiException) {
|
|
throw e.error as ApiException;
|
|
}
|
|
throw ApiException(
|
|
message: e.message ?? 'Failed to fetch agent types',
|
|
statusCode: e.response?.statusCode,
|
|
);
|
|
}
|
|
}
|
|
}
|