|
1 | 1 | import 'package:dartz/dartz.dart';
|
| 2 | +import 'package:eshop/core/usecases/usecase.dart'; |
2 | 3 |
|
3 | 4 | import '../../../../core/error/failures.dart';
|
4 | 5 | import '../../core/network/network_info.dart';
|
@@ -85,17 +86,44 @@ class CartRepositoryImpl implements CartRepository {
|
85 | 86 | }
|
86 | 87 |
|
87 | 88 | @override
|
88 |
| - Future<Either<Failure, bool>> deleteCartItem(CartItem params) { |
89 |
| - throw UnimplementedError(); |
| 89 | + Future<Either<Failure, CartItemModel>> deleteCartItem(CartItem params) async { |
| 90 | + final token = await userLocalDataSource.getToken(); |
| 91 | + final cartItem = CartItemModel.fromParent(params); |
| 92 | + // Check if the token is empty or if the network is not connected |
| 93 | + // If so, delete the cart item locally and return true |
| 94 | + // Otherwise, proceed with the remote data source |
| 95 | + // and delete the cart item locally after receiving the response |
| 96 | + if (token.isEmpty || !await networkInfo.isConnected) { |
| 97 | + localDataSource.deleteCartItem(cartItem); |
| 98 | + return Right(cartItem); |
| 99 | + } |
| 100 | + |
| 101 | + // Proceed with the remote data source |
| 102 | + // and delete the cart item locally after receiving the response |
| 103 | + final remoteResponse = await remoteDataSource.deleteCartItem( |
| 104 | + cartItem, |
| 105 | + token, |
| 106 | + ); |
| 107 | + await localDataSource.deleteCartItem(remoteResponse); |
| 108 | + return Right(remoteResponse); |
90 | 109 | }
|
91 | 110 |
|
92 | 111 | @override
|
93 |
| - Future<Either<Failure, bool>> deleteCart() async { |
94 |
| - bool result = await localDataSource.clearCart(); |
95 |
| - if (result) { |
96 |
| - return Right(result); |
97 |
| - } else { |
98 |
| - return Left(CacheFailure()); |
| 112 | + Future<Either<Failure, NoParams>> deleteCart() async { |
| 113 | + final token = await userLocalDataSource.getToken(); |
| 114 | + // Check if the token is empty or if the network is not connected |
| 115 | + // If so, delete the cart locally and return true |
| 116 | + // Otherwise, proceed with the remote data source |
| 117 | + // and delete the cart locally after receiving the response |
| 118 | + if (token.isEmpty || !await networkInfo.isConnected) { |
| 119 | + localDataSource.deleteCart(); |
| 120 | + return Right(NoParams()); |
99 | 121 | }
|
| 122 | + |
| 123 | + // Proceed with the remote data source |
| 124 | + // and delete the cart locally after receiving the response |
| 125 | + final remoteResponse = await remoteDataSource.deleteCart(token); |
| 126 | + await localDataSource.deleteCart(); |
| 127 | + return Right(remoteResponse); |
100 | 128 | }
|
101 | 129 | }
|
0 commit comments