146 lines
4.5 KiB
Dart
146 lines
4.5 KiB
Dart
import 'package:dio/dio.dart';
|
|
|
|
import '../../resources/api_constants.dart';
|
|
|
|
class SignUpApiService {
|
|
final String baseUrl = ApiConstants.baseUrl;
|
|
late final Dio dio;
|
|
|
|
SignUpApiService() {
|
|
dio = Dio(BaseOptions(
|
|
connectTimeout: const Duration(seconds: 10),
|
|
receiveTimeout: const Duration(seconds: 10),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
));
|
|
}
|
|
|
|
String _formatError(dynamic e, String defaultMessage) {
|
|
if (e is DioException) {
|
|
if (e.response != null && e.response?.data != null) {
|
|
final data = e.response!.data;
|
|
if (data is Map) {
|
|
if (data['message'] != null) return data['message'].toString();
|
|
if (data['operationMessage'] != null) return data['operationMessage'].toString();
|
|
if (data['error'] != null) return data['error'].toString();
|
|
} else if (data is String && data.isNotEmpty) {
|
|
return data;
|
|
}
|
|
}
|
|
if (e.type == DioExceptionType.connectionTimeout ||
|
|
e.type == DioExceptionType.sendTimeout ||
|
|
e.type == DioExceptionType.receiveTimeout) {
|
|
return 'Connection timed out. Please check your network and backend.';
|
|
}
|
|
if (e.type == DioExceptionType.connectionError) {
|
|
return 'Cannot connect to server at $baseUrl. Please verify the backend is running.';
|
|
}
|
|
return e.message ?? defaultMessage;
|
|
}
|
|
return defaultMessage.isNotEmpty ? '$defaultMessage: $e' : e.toString();
|
|
}
|
|
|
|
// get all account
|
|
Future<List<Map<String, dynamic>>> getallAccount(String token) async {
|
|
try {
|
|
dio.options.headers['Authorization'] = 'Bearer $token';
|
|
final response = await dio.get('$baseUrl/users/sysaccount/sysaccount');
|
|
|
|
final responseData = response.data;
|
|
|
|
print('response data is ... $responseData');
|
|
|
|
if (responseData is List) {
|
|
final entities = responseData.cast<Map<String, dynamic>>();
|
|
return entities;
|
|
} else if (responseData is Map<String, dynamic>) {
|
|
return [responseData];
|
|
} else {
|
|
throw Exception('Unexpected response type');
|
|
}
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to fetch Account'));
|
|
}
|
|
}
|
|
|
|
// Create account
|
|
Future<Map<String, dynamic>> createAccount(
|
|
Map<String, dynamic> entity) async {
|
|
try {
|
|
final response = await dio
|
|
.post('$baseUrl/token/users/sysaccount/savesysaccount', data: entity);
|
|
|
|
print('created account is $response');
|
|
if (response.data is Map<String, dynamic>) {
|
|
return response.data;
|
|
} else if (response.data is Map) {
|
|
return Map<String, dynamic>.from(response.data);
|
|
}
|
|
return {'account_id': response.data.toString()};
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to Create Account'));
|
|
}
|
|
}
|
|
|
|
// SEND EMAIL FOR OTP
|
|
Future<void> sendEmail(Map<String, dynamic> entity) async {
|
|
try {
|
|
print("in post api...$entity");
|
|
await dio.post('$baseUrl/token/user/send_email', data: entity);
|
|
print(entity);
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to Send Email'));
|
|
}
|
|
}
|
|
|
|
// RESEND EMAIL FOR OTP
|
|
Future<void> resendEmail(String email) async {
|
|
try {
|
|
await dio.post('$baseUrl/token/user/resend_otp?email=$email');
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to Resend OTP'));
|
|
}
|
|
}
|
|
|
|
// OTP VERIFICATION
|
|
Future<void> otpverification(String email, String otp) async {
|
|
try {
|
|
await dio
|
|
.post('$baseUrl/token/user/otp_verification?email=$email&otp=$otp');
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to Verify OTP'));
|
|
}
|
|
}
|
|
|
|
Future<void> createuser(Map<String, dynamic> entity) async {
|
|
try {
|
|
print("in post api...$entity");
|
|
await dio.post('$baseUrl/token/addOneAppUser', data: entity);
|
|
print(entity);
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to Create User'));
|
|
}
|
|
}
|
|
|
|
Future<void> updateUser(
|
|
String token, int entityId, Map<String, dynamic> entity) async {
|
|
try {
|
|
dio.options.headers['Authorization'] = 'Bearer $token';
|
|
await dio.put('$baseUrl/api/updateAppUserDto/$entityId', data: entity);
|
|
print(entity);
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to update User'));
|
|
}
|
|
}
|
|
|
|
Future<void> deleteUser(String token, int entityId) async {
|
|
try {
|
|
dio.options.headers['Authorization'] = 'Bearer $token';
|
|
await dio.delete('$baseUrl/api/delete_usr/$entityId');
|
|
} catch (e) {
|
|
throw Exception(_formatError(e, 'Failed to delete User'));
|
|
}
|
|
}
|
|
}
|