Files
mobile-app/lib/features/agents/data/agents_repository.dart

133 lines
4.2 KiB
Dart
Raw Normal View History

import 'dart:convert';
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';
import 'package:real_estate_mobile/features/agents/data/models/filterable_field.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,
});
2026-02-24 22:35:16 +05:30
/// Parses: { data: [...], meta: { total, page, limit, totalPages } }
factory SearchAgentsResponse.fromJson(Map<String, dynamic> json) {
2026-02-24 22:35:16 +05:30
final meta = json['meta'] as Map<String, dynamic>? ?? {};
return SearchAgentsResponse(
2026-02-24 22:35:16 +05:30
data: (json['data'] as List<dynamic>?)
?.map(
(e) => AgentProfile.fromJson(e as Map<String, dynamic>))
.toList() ??
[],
total: meta['total'] as int? ?? 0,
page: meta['page'] as int? ?? 1,
limit: meta['limit'] as int? ?? 10,
totalPages: meta['totalPages'] as int? ?? 1,
);
}
}
class AgentsRepository {
final Dio _dio = ApiClient.instance.dio;
2026-02-24 22:35:16 +05:30
/// Search agents: GET /agents
/// Response: { success, data: { data: [...], meta: {...} } }
Future<SearchAgentsResponse> searchAgents({
String? agentTypeId,
String? search,
int page = 1,
int limit = 10,
String? sortBy,
String? sortOrder,
Map<String, List<String>>? filters,
}) async {
try {
final queryParams = <String, dynamic>{
'page': page,
'limit': limit,
};
if (agentTypeId != null) queryParams['agentTypeId'] = agentTypeId;
if (search != null && search.isNotEmpty) queryParams['search'] = search;
if (sortBy != null) queryParams['sortBy'] = sortBy;
if (sortOrder != null) queryParams['sortOrder'] = sortOrder;
if (filters != null && filters.isNotEmpty) {
queryParams['filters'] = jsonEncode(filters);
}
final response = await _dio.get(
ApiConstants.agents,
queryParameters: queryParams,
);
2026-02-24 22:35:16 +05:30
// Backend wraps: { success: true, data: { data: [...], meta: {...} } }
final responseData = response.data;
final payload = responseData['data'] as Map<String, dynamic>;
return SearchAgentsResponse.fromJson(payload);
} 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,
);
}
}
/// Get filterable profile fields: GET /profile-fields/filterable
/// Response: { success, data: [...] }
Future<List<FilterableField>> getFilterableFields() async {
try {
final response = await _dio.get(ApiConstants.filterableFields);
final data = response.data['data'] as List<dynamic>;
return data
.map((e) => FilterableField.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 filterable fields',
statusCode: e.response?.statusCode,
);
}
}
2026-02-24 22:35:16 +05:30
/// Get agent types: GET /agent-types
/// Response: { success, data: [...] }
Future<List<AgentType>> getAgentTypes() async {
try {
final response = await _dio.get(ApiConstants.agentTypes);
2026-02-24 22:35:16 +05:30
// Backend wraps: { success: true, data: [...] }
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,
);
}
}
}