forked from eatvio/eatvio_app
98 lines
3.1 KiB
Dart
98 lines
3.1 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/core/locator_service.dart';
|
|
import 'package:eatvio_app/features/registration/models/user_registration_model.dart';
|
|
import 'package:eatvio_app/features/registration/models/location_model.dart';
|
|
import 'package:eatvio_app/features/registration/services/registration_service.dart';
|
|
import 'package:eatvio_app/core/env_config.dart';
|
|
|
|
class RegistrationRepository {
|
|
final RegistrationService registrationService;
|
|
|
|
RegistrationRepository({required this.registrationService});
|
|
|
|
Future<String> tryRegister(UserRegistrationSheet sheet) async {
|
|
try {
|
|
final response = await registrationService.register(sheet: sheet.toJson());
|
|
if (response.containsKey('token')) {
|
|
return response['token']!;
|
|
} else {
|
|
// Если нет токена, но есть message или errors
|
|
String errorMsg = '';
|
|
if (response.containsKey('message')) {
|
|
errorMsg += '${response['message']!}\n';
|
|
}
|
|
if (response.containsKey('errors')) {
|
|
final errors = response['errors'];
|
|
if (errors != null && errors is Map) {
|
|
(errors as Map).forEach((key, value) {
|
|
if (value is List && value.isNotEmpty) {
|
|
errorMsg += '${value.join('\n')}\n';
|
|
}
|
|
});
|
|
}
|
|
}
|
|
if (errorMsg.isNotEmpty) {
|
|
throw errorMsg.trim();
|
|
} else {
|
|
throw 'Ошибка регистрации: $response';
|
|
}
|
|
}
|
|
} on DioException catch (e) {
|
|
if (e.response != null && e.response?.statusCode == 422) {
|
|
final data = e.response?.data;
|
|
String errorMsg = '';
|
|
if (data is Map<String, dynamic>) {
|
|
if (data.containsKey('message')) {
|
|
errorMsg += data['message'] + '\n';
|
|
}
|
|
if (data.containsKey('errors')) {
|
|
final errors = data['errors'];
|
|
if (errors != null && errors is Map) {
|
|
(errors).forEach((key, value) {
|
|
if (value is List && value.isNotEmpty) {
|
|
errorMsg += '${value.join('\n')}\n';
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
if (errorMsg.isNotEmpty) {
|
|
throw errorMsg.trim();
|
|
} else {
|
|
throw 'Ошибка регистрации: $data';
|
|
}
|
|
}
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<LocationModel> getLocationsListByQuery(String query) async {
|
|
try {
|
|
return (await registrationService.getLocationsByAddress(query: query));
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<LocationModel> getLocationsListByCoordinates(String latitude, String longitude) async {
|
|
try {
|
|
return (await registrationService.getLocationsByCoordinate(latitude: latitude, longitude: longitude));
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<bool> emailIsReachable(String email) async {
|
|
try {
|
|
final response = await sl<Dio>().getUri(Uri.parse('${EnvConfig.emailValidationApiUrl}?email=$email'));
|
|
if (response.data['result']['reachable'] != 'invalid') {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|