attendence login/logout has been implemented
This commit is contained in:
58
lib/data/datasources/attendance_remote_data_source.dart
Normal file
58
lib/data/datasources/attendance_remote_data_source.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
import 'dart:io';
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../core/network/api_client.dart';
|
||||
import '../dto/attendance_response_dto.dart';
|
||||
|
||||
abstract class AttendanceRemoteDataSource {
|
||||
Future<AttendanceResponseDto> login({
|
||||
required String employeeId,
|
||||
required File faceImage,
|
||||
});
|
||||
|
||||
Future<AttendanceResponseDto> logout({
|
||||
required String employeeId,
|
||||
required File faceImage,
|
||||
});
|
||||
}
|
||||
|
||||
class AttendanceRemoteDataSourceImpl implements AttendanceRemoteDataSource {
|
||||
final ApiClient apiClient;
|
||||
|
||||
AttendanceRemoteDataSourceImpl({required this.apiClient});
|
||||
@override
|
||||
Future<AttendanceResponseDto> login({
|
||||
required String employeeId,
|
||||
required File faceImage,
|
||||
}) async {
|
||||
final formData = FormData.fromMap({
|
||||
'EmployeeId': employeeId,
|
||||
'FaceImage': await MultipartFile.fromFile(faceImage.path),
|
||||
});
|
||||
|
||||
final response = await apiClient.post(
|
||||
'/Attendance/login',
|
||||
data: formData,
|
||||
options: Options(contentType: 'multipart/form-data'),
|
||||
);
|
||||
return AttendanceResponseDto.fromJson(response.data);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AttendanceResponseDto> logout({
|
||||
required String employeeId,
|
||||
required File faceImage,
|
||||
}) async {
|
||||
final formData = FormData.fromMap({
|
||||
'EmployeeId': employeeId,
|
||||
'FaceImage': await MultipartFile.fromFile(faceImage.path),
|
||||
});
|
||||
|
||||
final response = await apiClient.post(
|
||||
'/Attendance/logout',
|
||||
data: formData,
|
||||
options: Options(contentType: 'multipart/form-data'),
|
||||
);
|
||||
|
||||
return AttendanceResponseDto.fromJson(response.data);
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,7 @@ class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
|
||||
@override
|
||||
Future<LoginResponseDto> login(LoginDto dto) async {
|
||||
try {
|
||||
final response = await apiClient.post(
|
||||
'/Auth/login',
|
||||
data: dto.toJson(),
|
||||
);
|
||||
final response = await apiClient.post('/Auth/login', data: dto.toJson());
|
||||
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
final responseData = response.data;
|
||||
@@ -47,7 +44,8 @@ class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
|
||||
} else if (e.response?.statusCode == 500) {
|
||||
throw ServerException(message: 'خطأ في الخادم يرجى المحاولة لاحقا');
|
||||
} else if (e.response != null) {
|
||||
final message = e.response?.data?['message'] ??
|
||||
final message =
|
||||
e.response?.data?['message'] ??
|
||||
e.response?.data?['error'] ??
|
||||
'فشل تسجيل الدخول';
|
||||
|
||||
@@ -57,8 +55,8 @@ class AuthRemoteDataSourceImpl implements AuthRemoteDataSource {
|
||||
message.toString().toLowerCase().contains('incorrect')
|
||||
? 'رقم الهاتف أو كلمة المرور غير صحيحة'
|
||||
: message.toString().toLowerCase().contains('not found')
|
||||
? 'المستخدم غير موجود'
|
||||
: message;
|
||||
? 'المستخدم غير موجود'
|
||||
: message;
|
||||
|
||||
throw ServerException(
|
||||
message: customMessage,
|
||||
|
||||
@@ -4,11 +4,14 @@ abstract class UserLocalDataSource {
|
||||
Future<void> cacheUserToken(String token);
|
||||
Future<String?> getCachedUserToken();
|
||||
Future<void> clearCache();
|
||||
Future<void> cacheEmployeeId(String id);
|
||||
Future<String?> getCachedEmployeeId();
|
||||
}
|
||||
|
||||
class UserLocalDataSourceImpl implements UserLocalDataSource {
|
||||
final SharedPreferences sharedPreferences;
|
||||
static const String _tokenKey = 'user_token';
|
||||
static const String _employeeIdKey = 'employee_id';
|
||||
|
||||
UserLocalDataSourceImpl({required this.sharedPreferences});
|
||||
|
||||
@@ -25,5 +28,16 @@ class UserLocalDataSourceImpl implements UserLocalDataSource {
|
||||
@override
|
||||
Future<void> clearCache() async {
|
||||
await sharedPreferences.remove(_tokenKey);
|
||||
await sharedPreferences.remove(_employeeIdKey);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> cacheEmployeeId(String id) async {
|
||||
await sharedPreferences.setString(_employeeIdKey, id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getCachedEmployeeId() async {
|
||||
return sharedPreferences.getString(_employeeIdKey);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user