363 lines
9.8 KiB
Dart
363 lines
9.8 KiB
Dart
import 'package:dio/dio.dart';
|
|
import 'package:yimaru_app/models/assessment.dart';
|
|
import 'package:yimaru_app/models/user_model.dart';
|
|
import 'package:yimaru_app/services/dio_service.dart';
|
|
import 'package:yimaru_app/ui/common/app_constants.dart';
|
|
|
|
import '../app/app.locator.dart';
|
|
import '../ui/common/enmus.dart';
|
|
|
|
class ApiService {
|
|
final _service = locator<DioService>();
|
|
|
|
// Register
|
|
Future<Map<String, dynamic>> register(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kUserUrl/$kRegisterUrl',
|
|
data: data,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Otp sent successfully'
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': 'Unknown Error Occurred'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Email Login
|
|
Future<Map<String, dynamic>> emailLogin(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kLoginUrl',
|
|
data: data,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Logged in successfully',
|
|
'data': UserModel.fromJson(response.data['data']),
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Google login
|
|
Future<Map<String, dynamic>> googleLogin(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kGoogleLoginUrl',
|
|
data: data,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Logged in successfully',
|
|
'data': UserModel.fromJson(response.data['data']),
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Verify otp
|
|
Future<Map<String, dynamic>> verifyOtp(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kUserUrl/$kVerifyOtpUrl',
|
|
data: data,
|
|
);
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Otp verified successfully',
|
|
'data': UserModel.fromJson(response.data['data']),
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Resend otp
|
|
Future<Map<String, dynamic>> resendOtp(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kUserUrl/$kResendOtpUrl',
|
|
data: data,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Otp resend successfully'
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': 'Unknown Error Occurred'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Request reset code
|
|
Future<Map<String, dynamic>> requestResetCode(
|
|
Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kUserUrl/$kRequestResetCode',
|
|
data: data,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Reset code sent successfully',
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Reset password
|
|
Future<Map<String, dynamic>> resetPassword(Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kUserUrl/$kResetPassword',
|
|
data: data,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Password reset successfully',
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Profile completed
|
|
Future<Map<String, dynamic>> getProfileStatus(UserModel? user) async {
|
|
try {
|
|
Response response = await _service.dio.get(
|
|
'$kBaseUrl/$kUserUrl/${user?.userId}/$kProfileStatusUrl',
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Profile completion status fetched successfully',
|
|
'data': response.data['data']['is_profile_completed'] as bool,
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': '${response.data['message']}, ${response.data['error']}'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Get profile
|
|
Future<Map<String, dynamic>> getProfileData(int? userId) async {
|
|
try {
|
|
Response response = await _service.dio.get(
|
|
'$kBaseUrl/$kUserUrl/$kGetUserUrl/$userId',
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Profile fetched successfully',
|
|
'data': UserModel.fromJson(response.data['data']),
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': 'Unknown Error Occurred'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Complete profile
|
|
Future<Map<String, dynamic>> completeProfile(
|
|
Map<String, dynamic> data) async {
|
|
try {
|
|
Response response = await _service.dio.put(
|
|
'$kBaseUrl/$kUserUrl',
|
|
data: data,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Profile updated successfully'
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': 'Unknown Error Occurred'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Update profile image
|
|
Future<Map<String, dynamic>> updateProfileImage(
|
|
{required int? userId, required Map<String, dynamic> data}) async {
|
|
try {
|
|
late FormData formData;
|
|
if (data['profile_picture_url']
|
|
.toString()
|
|
.contains('com.example.yimaru_app/')) {
|
|
formData = FormData.fromMap({
|
|
'file': data['profile_picture_url'].toString().isNotEmpty
|
|
? MultipartFile.fromFileSync(
|
|
data['profile_picture_url'],
|
|
filename:
|
|
data['profile_picture_url'].toString().split('/').last,
|
|
)
|
|
: null,
|
|
});
|
|
} else {
|
|
formData = FormData.fromMap({
|
|
'file': data['profile_picture_url'].toString().isNotEmpty
|
|
? MultipartFile.fromFileSync(
|
|
data['profile_picture_url'],
|
|
filename:
|
|
data['profile_picture_url'].toString().split('/').last,
|
|
)
|
|
: null,
|
|
});
|
|
}
|
|
Response response = await _service.dio.post(
|
|
'$kBaseUrl/$kUserUrl/$userId/$kUpdateProfileImage',
|
|
data: formData,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return {
|
|
'status': ResponseStatus.success,
|
|
'message': 'Profile updated successfully'
|
|
};
|
|
} else {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': 'Unknown Error Occurred'
|
|
};
|
|
}
|
|
} on DioException catch (e) {
|
|
return {
|
|
'status': ResponseStatus.failure,
|
|
'message': e.response?.data.toString(),
|
|
};
|
|
}
|
|
}
|
|
|
|
// Assessments
|
|
Future<List<Assessment>> getAssessments() async {
|
|
try {
|
|
List<Assessment> assessments = [];
|
|
|
|
final Response response =
|
|
await _service.dio.get('$kBaseUrl/$kAssessmentsUrl');
|
|
|
|
if (response.statusCode == 200) {
|
|
var data = response.data;
|
|
var decodedData = data['data'] as List;
|
|
assessments = decodedData.map(
|
|
(e) {
|
|
return Assessment.fromJson(e);
|
|
},
|
|
).toList();
|
|
return assessments;
|
|
}
|
|
return [];
|
|
} catch (e) {
|
|
return [];
|
|
}
|
|
}
|
|
}
|