Initial commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/profile_response.dart';
|
||||
|
||||
abstract class ProfileLocalDataSource {
|
||||
Future<ProfileResponse> getProfile();
|
||||
|
||||
Future<void> setProfile(ProfileResponse response);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:cargocalculaterapp/features/profile/data/data_source/local/profile_local_data_source.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/profile_response.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import '../../../../../constants/constants.dart';
|
||||
import '../../../../../core/error/exceptions.dart';
|
||||
|
||||
class ProfileLocalDataSourceImpl extends ProfileLocalDataSource {
|
||||
final Box<dynamic> box;
|
||||
|
||||
ProfileLocalDataSourceImpl(this.box);
|
||||
|
||||
@override
|
||||
Future<ProfileResponse> getProfile() async {
|
||||
final json = await box.get(CacheKeys.profile, defaultValue: null);
|
||||
if (json != null) {
|
||||
return ProfileResponse.fromJson(jsonDecode(json));
|
||||
} else {
|
||||
throw CacheException(message: Validations.someThingWentWrong);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> setProfile(ProfileResponse response) async {
|
||||
final json = jsonEncode(response.toJson());
|
||||
await box.put(CacheKeys.profile, json);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/delete_account_response.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/profile_response.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/profile_update_request.dart';
|
||||
|
||||
abstract class ProfileRemoteDataSource {
|
||||
Future<ProfileResponse> getProfile();
|
||||
|
||||
Future<ProfileResponse> updateProfile(ProfileUpdateRequest request);
|
||||
|
||||
Future<DeleteAccountResponse> deleteProfile();
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import 'package:cargocalculaterapp/core/local_source/local_source.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/data_source/remote/profile_remote_data_source.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/delete_account_response.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/profile_response.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/profile_update_request.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../../../constants/constants.dart';
|
||||
import '../../../../../core/error/exceptions.dart';
|
||||
import '../../../../../injector_container.dart';
|
||||
|
||||
class ProfileRemoteDataSourceImpl extends ProfileRemoteDataSource {
|
||||
final Dio dio;
|
||||
|
||||
ProfileRemoteDataSourceImpl(this.dio);
|
||||
|
||||
@override
|
||||
Future<ProfileResponse> getProfile() async {
|
||||
try {
|
||||
final Response response = await dio.get(
|
||||
Constants.baseUrl + Urls.getProfile,
|
||||
options: DioConstants.options
|
||||
..headers = {
|
||||
"Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return ProfileResponse.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<ProfileResponse> updateProfile(ProfileUpdateRequest request) async {
|
||||
try {
|
||||
final Response response = await dio.put(
|
||||
Constants.baseUrl + Urls.updateProfile,
|
||||
options: DioConstants.options
|
||||
..headers = {
|
||||
"Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
|
||||
},
|
||||
data: request,
|
||||
);
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return ProfileResponse.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<DeleteAccountResponse> deleteProfile() async {
|
||||
try {
|
||||
final Response response = await dio.delete(
|
||||
Constants.baseUrl + Urls.delete,
|
||||
options: DioConstants.options
|
||||
..headers = {
|
||||
"Authorization": "Bearer ${sl<LocalSource>().getAccessToken()}",
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||||
return DeleteAccountResponse.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
class DeleteAccountResponse {
|
||||
DeleteAccountResponse({
|
||||
this.message,});
|
||||
|
||||
DeleteAccountResponse.fromJson(dynamic json) {
|
||||
message = json['message'];
|
||||
}
|
||||
String? message;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['message'] = message;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
36
lib/features/profile/data/models/profile_response.dart
Normal file
36
lib/features/profile/data/models/profile_response.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class ProfileResponse extends Equatable {
|
||||
const ProfileResponse({
|
||||
this.fullname,
|
||||
this.phoneNumber,
|
||||
this.email,
|
||||
this.ucode,
|
||||
});
|
||||
|
||||
factory ProfileResponse.fromJson(dynamic json) {
|
||||
return ProfileResponse(
|
||||
fullname: json['fullname'],
|
||||
phoneNumber: json['phone_number'],
|
||||
email: json['email'],
|
||||
ucode: json['ucode'],
|
||||
);
|
||||
}
|
||||
|
||||
final String? fullname;
|
||||
final String? phoneNumber;
|
||||
final String? email;
|
||||
final String? ucode;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['fullname'] = fullname;
|
||||
map['phone_number'] = phoneNumber;
|
||||
map['email'] = email;
|
||||
map['ucode'] = ucode;
|
||||
return map;
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [fullname, phoneNumber, email, ucode];
|
||||
}
|
||||
24
lib/features/profile/data/models/profile_update_request.dart
Normal file
24
lib/features/profile/data/models/profile_update_request.dart
Normal file
@@ -0,0 +1,24 @@
|
||||
class ProfileUpdateRequest {
|
||||
ProfileUpdateRequest({
|
||||
this.fullname,
|
||||
this.phoneNumber,
|
||||
this.email,});
|
||||
|
||||
ProfileUpdateRequest.fromJson(dynamic json) {
|
||||
fullname = json['fullname'];
|
||||
phoneNumber = json['phone_number'];
|
||||
email = json['email'];
|
||||
}
|
||||
String? fullname;
|
||||
String? phoneNumber;
|
||||
String? email;
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map['fullname'] = fullname;
|
||||
map['phone_number'] = phoneNumber;
|
||||
map['email'] = email;
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import 'package:cargocalculaterapp/core/error/failure.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/delete_account_response.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/data/models/profile_update_request.dart';
|
||||
import 'package:cargocalculaterapp/features/profile/domain/repository/profile_repository.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../constants/constants.dart';
|
||||
import '../../../../core/error/exceptions.dart';
|
||||
import '../data_source/local/profile_local_data_source.dart';
|
||||
import '../data_source/remote/profile_remote_data_source.dart';
|
||||
import '../models/profile_response.dart';
|
||||
|
||||
class ProfileRepositoryImpl extends ProfileRepository {
|
||||
final ProfileRemoteDataSource remoteDataSource;
|
||||
final ProfileLocalDataSource localDataSource;
|
||||
|
||||
ProfileRepositoryImpl(this.remoteDataSource, this.localDataSource);
|
||||
|
||||
@override
|
||||
Future<Either<Failure, ProfileResponse>> getProfile(bool isCache) async {
|
||||
if (!isCache) {
|
||||
try {
|
||||
final response = await remoteDataSource.getProfile();
|
||||
localDataSource.setProfile(response);
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
return Left(ServerFailure(message: e.toString()));
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
final response = await localDataSource.getProfile();
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
return Left(
|
||||
CacheFailure(
|
||||
message: (e is CacheException)
|
||||
? e.message
|
||||
: Warnings.someThingWentWrong,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, ProfileResponse>> updateProfile(
|
||||
ProfileUpdateRequest request,
|
||||
) async {
|
||||
try {
|
||||
final response = await remoteDataSource.updateProfile(request);
|
||||
localDataSource.setProfile(response);
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
return Left(ServerFailure(message: e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, DeleteAccountResponse>> deleteProfile() async {
|
||||
try {
|
||||
final response = await remoteDataSource.deleteProfile();
|
||||
return Right(response);
|
||||
} catch (e) {
|
||||
return Left(ServerFailure(message: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user