Initial commit
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/login_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/login_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_response.dart';
|
||||
|
||||
abstract class AuthRemoteDataSource {
|
||||
Future<LoginResponse> login(LoginRequest request);
|
||||
|
||||
Future<SignUpResponse> signUp(SignUpRequest request);
|
||||
|
||||
Future<AuthVerificationResponse> verify(AuthVerificationRequest request);
|
||||
|
||||
Future<FcmAddResponse> addFcm(FcmAddRequest request);
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/login_request.dart';
|
||||
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/login_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_response.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../../constants/constants.dart';
|
||||
import '../../../../../core/error/exceptions.dart';
|
||||
import '../../../../../core/local_source/local_source.dart';
|
||||
import '../../../../../injector_container.dart';
|
||||
import 'auth_remote_data_source.dart';
|
||||
|
||||
class AuthRemoteDataSourceImpl extends AuthRemoteDataSource {
|
||||
final Dio dio;
|
||||
|
||||
AuthRemoteDataSourceImpl(this.dio);
|
||||
|
||||
@override
|
||||
Future<LoginResponse> login(LoginRequest request) async {
|
||||
try {
|
||||
final Response response = await dio.post(
|
||||
Constants.baseUrl + Urls.login,
|
||||
options: DioConstants.options,
|
||||
data: request,
|
||||
);
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return LoginResponse.fromJson(response.data);
|
||||
}
|
||||
throw ServerException.fromJson(response.data);
|
||||
} on DioException catch (e) {
|
||||
throw ServerException.fromJson(e.response?.data);
|
||||
} on FormatException {
|
||||
throw ServerException(message: Validations.someThingWentWrong);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SignUpResponse> signUp(SignUpRequest request) async {
|
||||
try {
|
||||
final Response response = await dio.post(
|
||||
Constants.baseUrl + Urls.signUp,
|
||||
options: DioConstants.options,
|
||||
data: request,
|
||||
);
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return SignUpResponse.fromJson(response.data);
|
||||
}
|
||||
throw ServerException.fromJson(response.data);
|
||||
} on DioException catch (e) {
|
||||
throw ServerException.fromJson(e.response?.data);} on FormatException {
|
||||
throw ServerException(message: Validations.someThingWentWrong);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthVerificationResponse> verify(
|
||||
AuthVerificationRequest request,
|
||||
) async {
|
||||
try {
|
||||
final Response response = await dio.post(
|
||||
Constants.baseUrl + Urls.verify,
|
||||
options: DioConstants.options
|
||||
..headers = {
|
||||
"Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
|
||||
},
|
||||
data: request,
|
||||
);
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return AuthVerificationResponse.fromJson(response.data);
|
||||
}
|
||||
throw ServerException.fromJson(response.data);
|
||||
} on DioException catch (e) {
|
||||
throw ServerException.fromJson(e.response?.data);
|
||||
} on FormatException {
|
||||
throw ServerException(message: Validations.someThingWentWrong);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<FcmAddResponse> addFcm(FcmAddRequest request) async {
|
||||
try {
|
||||
final Response response = await dio.put(
|
||||
Constants.baseUrl + Urls.addFcm,
|
||||
options: DioConstants.options
|
||||
..headers = {
|
||||
"Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
|
||||
},
|
||||
data: request,
|
||||
);
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return FcmAddResponse.fromJson(response.data);
|
||||
}
|
||||
throw ServerException.fromJson(response.data);
|
||||
} on DioException catch (e) {
|
||||
throw ServerException.fromJson(e.response?.data);
|
||||
} on FormatException {
|
||||
throw ServerException(message: Validations.someThingWentWrong);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
lib/features/auth/data/model/auth_verification_request.dart
Normal file
21
lib/features/auth/data/model/auth_verification_request.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
class AuthVerificationRequest {
|
||||
AuthVerificationRequest({this.phoneNumber, this.smsCode, this.email});
|
||||
|
||||
AuthVerificationRequest.fromJson(dynamic json) {
|
||||
phoneNumber = json['phone_number'];
|
||||
smsCode = json['smsCode'];
|
||||
email = json['email'];
|
||||
}
|
||||
|
||||
String? phoneNumber;
|
||||
String? smsCode;
|
||||
String? email;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['phone_number'] = phoneNumber;
|
||||
map['smsCode'] = smsCode;
|
||||
map['email'] = email;
|
||||
return map;
|
||||
}
|
||||
}
|
||||
16
lib/features/auth/data/model/auth_verification_response.dart
Normal file
16
lib/features/auth/data/model/auth_verification_response.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
class AuthVerificationResponse {
|
||||
AuthVerificationResponse({
|
||||
this.accessToken,});
|
||||
|
||||
AuthVerificationResponse.fromJson(dynamic json) {
|
||||
accessToken = json['access_token'];
|
||||
}
|
||||
String? accessToken;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['access_token'] = accessToken;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
16
lib/features/auth/data/model/fcm_add_request.dart
Normal file
16
lib/features/auth/data/model/fcm_add_request.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
class FcmAddRequest {
|
||||
FcmAddRequest({
|
||||
this.sfmToken,});
|
||||
|
||||
FcmAddRequest.fromJson(dynamic json) {
|
||||
sfmToken = json['sfm_token'];
|
||||
}
|
||||
String? sfmToken;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['sfm_token'] = sfmToken;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
51
lib/features/auth/data/model/fcm_add_response.dart
Normal file
51
lib/features/auth/data/model/fcm_add_response.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
class FcmAddResponse {
|
||||
FcmAddResponse({
|
||||
this.message,
|
||||
this.user,});
|
||||
|
||||
FcmAddResponse.fromJson(dynamic json) {
|
||||
message = json['message'];
|
||||
user = json['user'] != null ? User.fromJson(json['user']) : null;
|
||||
}
|
||||
String? message;
|
||||
User? user;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['message'] = message;
|
||||
if (user != null) {
|
||||
map['user'] = user?.toJson();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class User {
|
||||
User({
|
||||
this.fullname,
|
||||
this.phoneNumber,
|
||||
this.email,
|
||||
this.sfmToken,});
|
||||
|
||||
User.fromJson(dynamic json) {
|
||||
fullname = json['fullname'];
|
||||
phoneNumber = json['phone_number'];
|
||||
email = json['email'];
|
||||
sfmToken = json['sfm_token'];
|
||||
}
|
||||
String? fullname;
|
||||
String? phoneNumber;
|
||||
String? email;
|
||||
String? sfmToken;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['fullname'] = fullname;
|
||||
map['phone_number'] = phoneNumber;
|
||||
map['email'] = email;
|
||||
map['sfm_token'] = sfmToken;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
24
lib/features/auth/data/model/login_request.dart
Normal file
24
lib/features/auth/data/model/login_request.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
class LoginRequest {
|
||||
LoginRequest({
|
||||
this.email,
|
||||
this.phone,
|
||||
this.ucode,});
|
||||
|
||||
LoginRequest.fromJson(dynamic json) {
|
||||
email = json['email'];
|
||||
phone = json['phone'];
|
||||
ucode = json['ucode'];
|
||||
}
|
||||
String? email;
|
||||
String? phone;
|
||||
String? ucode;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['email'] = email;
|
||||
map['phone'] = phone;
|
||||
map['ucode'] = ucode;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
20
lib/features/auth/data/model/login_response.dart
Normal file
20
lib/features/auth/data/model/login_response.dart
Normal file
@@ -0,0 +1,20 @@
|
||||
class LoginResponse {
|
||||
LoginResponse({
|
||||
this.token,
|
||||
this.refreshToken,});
|
||||
|
||||
LoginResponse.fromJson(dynamic json) {
|
||||
token = json['token'];
|
||||
refreshToken = json['refreshToken'];
|
||||
}
|
||||
String? token;
|
||||
String? refreshToken;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['token'] = token;
|
||||
map['refreshToken'] = refreshToken;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
25
lib/features/auth/data/model/sign_up_request.dart
Normal file
25
lib/features/auth/data/model/sign_up_request.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
class SignUpRequest {
|
||||
SignUpRequest({
|
||||
this.phoneNumber,
|
||||
this.email,
|
||||
this.fullname,});
|
||||
|
||||
SignUpRequest.fromJson(dynamic json) {
|
||||
phoneNumber = json['phone_number'];
|
||||
email = json['email'];
|
||||
fullname = json['fullname'];
|
||||
}
|
||||
String? phoneNumber;
|
||||
String? email;
|
||||
String? fullname;
|
||||
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['phone_number'] = phoneNumber;
|
||||
map['email'] = email;
|
||||
map['fullname'] = fullname;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
16
lib/features/auth/data/model/sign_up_response.dart
Normal file
16
lib/features/auth/data/model/sign_up_response.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
class SignUpResponse {
|
||||
SignUpResponse({
|
||||
this.token,});
|
||||
|
||||
SignUpResponse.fromJson(dynamic json) {
|
||||
token = json['token'];
|
||||
}
|
||||
String? token;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['token'] = token;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
73
lib/features/auth/data/repository/auth_repository_impl.dart
Normal file
73
lib/features/auth/data/repository/auth_repository_impl.dart
Normal file
@@ -0,0 +1,73 @@
|
||||
import 'package:cargocalculaterapp/core/error/failure.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/auth_verification_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/fcm_add_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/login_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/login_response.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_request.dart';
|
||||
import 'package:cargocalculaterapp/features/auth/data/model/sign_up_response.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/error/exceptions.dart';
|
||||
import '../../domain/repository/auth_repository.dart';
|
||||
import '../data_source/remote/auth_remote_data_source.dart';
|
||||
|
||||
class AuthRepositoryImpl extends AuthRepository {
|
||||
final AuthRemoteDataSource remoteDataSource;
|
||||
|
||||
AuthRepositoryImpl(this.remoteDataSource);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, LoginResponse>> login(LoginRequest request) async {
|
||||
try {
|
||||
final response = await remoteDataSource.login(request);
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
if (e is ServerException) {
|
||||
return Left(ServerFailure(message: e.message));
|
||||
}
|
||||
return Left(ServerFailure(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, SignUpResponse>> signUp(SignUpRequest request) async {
|
||||
try {
|
||||
final response = await remoteDataSource.signUp(request);
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
if (e is ServerException) {
|
||||
return Left(ServerFailure(message: e.message));
|
||||
}
|
||||
return Left(ServerFailure(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, AuthVerificationResponse>> verify(
|
||||
AuthVerificationRequest request,
|
||||
) async {
|
||||
try {
|
||||
final response = await remoteDataSource.verify(request);
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
if (e is ServerException) {
|
||||
return Left(ServerFailure(message: e.message));
|
||||
}
|
||||
return Left(ServerFailure(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, FcmAddResponse>> fcmAdd(FcmAddRequest request) async {
|
||||
try {
|
||||
final response = await remoteDataSource.addFcm(request);
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
if (e is ServerException) {
|
||||
return Left(ServerFailure(message: e.message));
|
||||
}
|
||||
return Left(ServerFailure(message: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user