runable
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../domain/entities/cart_item.dart';
|
||||
import '../../domain/repositories/cart_repository.dart';
|
||||
import '../datasources/cart_local_datasource.dart';
|
||||
import '../models/cart_item_model.dart';
|
||||
import '../../../../core/errors/failures.dart';
|
||||
import '../../../../core/errors/exceptions.dart';
|
||||
|
||||
class CartRepositoryImpl implements CartRepository {
|
||||
final CartLocalDataSource localDataSource;
|
||||
|
||||
CartRepositoryImpl({
|
||||
required this.localDataSource,
|
||||
});
|
||||
|
||||
@override
|
||||
Future<Either<Failure, List<CartItem>>> getCartItems() async {
|
||||
try {
|
||||
final items = await localDataSource.getCartItems();
|
||||
return Right(items.map((model) => model.toEntity()).toList());
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, void>> addToCart(CartItem item) async {
|
||||
try {
|
||||
final model = CartItemModel.fromEntity(item);
|
||||
await localDataSource.addToCart(model);
|
||||
return const Right(null);
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, void>> updateQuantity(String productId, int quantity) async {
|
||||
try {
|
||||
await localDataSource.updateQuantity(productId, quantity);
|
||||
return const Right(null);
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, void>> removeFromCart(String productId) async {
|
||||
try {
|
||||
await localDataSource.removeFromCart(productId);
|
||||
return const Right(null);
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Failure, void>> clearCart() async {
|
||||
try {
|
||||
await localDataSource.clearCart();
|
||||
return const Right(null);
|
||||
} on CacheException catch (e) {
|
||||
return Left(CacheFailure(e.message));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user