runable
This commit is contained in:
50
lib/features/home/domain/entities/cart_item.dart
Normal file
50
lib/features/home/domain/entities/cart_item.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
/// Cart item domain entity
|
||||
class CartItem extends Equatable {
|
||||
final String productId;
|
||||
final String productName;
|
||||
final double price;
|
||||
final int quantity;
|
||||
final String? imageUrl;
|
||||
final DateTime addedAt;
|
||||
|
||||
const CartItem({
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
required this.price,
|
||||
required this.quantity,
|
||||
this.imageUrl,
|
||||
required this.addedAt,
|
||||
});
|
||||
|
||||
double get total => price * quantity;
|
||||
|
||||
CartItem copyWith({
|
||||
String? productId,
|
||||
String? productName,
|
||||
double? price,
|
||||
int? quantity,
|
||||
String? imageUrl,
|
||||
DateTime? addedAt,
|
||||
}) {
|
||||
return CartItem(
|
||||
productId: productId ?? this.productId,
|
||||
productName: productName ?? this.productName,
|
||||
price: price ?? this.price,
|
||||
quantity: quantity ?? this.quantity,
|
||||
imageUrl: imageUrl ?? this.imageUrl,
|
||||
addedAt: addedAt ?? this.addedAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
productId,
|
||||
productName,
|
||||
price,
|
||||
quantity,
|
||||
imageUrl,
|
||||
addedAt,
|
||||
];
|
||||
}
|
||||
21
lib/features/home/domain/repositories/cart_repository.dart
Normal file
21
lib/features/home/domain/repositories/cart_repository.dart
Normal file
@@ -0,0 +1,21 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/errors/failures.dart';
|
||||
import '../entities/cart_item.dart';
|
||||
|
||||
/// Cart repository interface
|
||||
abstract class CartRepository {
|
||||
/// Get all cart items
|
||||
Future<Either<Failure, List<CartItem>>> getCartItems();
|
||||
|
||||
/// Add item to cart
|
||||
Future<Either<Failure, void>> addToCart(CartItem item);
|
||||
|
||||
/// Update cart item quantity
|
||||
Future<Either<Failure, void>> updateQuantity(String productId, int quantity);
|
||||
|
||||
/// Remove item from cart
|
||||
Future<Either<Failure, void>> removeFromCart(String productId);
|
||||
|
||||
/// Clear all cart items
|
||||
Future<Either<Failure, void>> clearCart();
|
||||
}
|
||||
15
lib/features/home/domain/usecases/add_to_cart.dart
Normal file
15
lib/features/home/domain/usecases/add_to_cart.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/errors/failures.dart';
|
||||
import '../entities/cart_item.dart';
|
||||
import '../repositories/cart_repository.dart';
|
||||
|
||||
/// Use case to add item to cart
|
||||
class AddToCart {
|
||||
final CartRepository repository;
|
||||
|
||||
AddToCart(this.repository);
|
||||
|
||||
Future<Either<Failure, void>> call(CartItem item) async {
|
||||
return await repository.addToCart(item);
|
||||
}
|
||||
}
|
||||
8
lib/features/home/domain/usecases/calculate_total.dart
Normal file
8
lib/features/home/domain/usecases/calculate_total.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
import '../entities/cart_item.dart';
|
||||
|
||||
/// Use case to calculate cart total
|
||||
class CalculateTotal {
|
||||
double call(List<CartItem> items) {
|
||||
return items.fold(0.0, (sum, item) => sum + item.total);
|
||||
}
|
||||
}
|
||||
14
lib/features/home/domain/usecases/clear_cart.dart
Normal file
14
lib/features/home/domain/usecases/clear_cart.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/errors/failures.dart';
|
||||
import '../repositories/cart_repository.dart';
|
||||
|
||||
/// Use case to clear cart
|
||||
class ClearCart {
|
||||
final CartRepository repository;
|
||||
|
||||
ClearCart(this.repository);
|
||||
|
||||
Future<Either<Failure, void>> call() async {
|
||||
return await repository.clearCart();
|
||||
}
|
||||
}
|
||||
14
lib/features/home/domain/usecases/remove_from_cart.dart
Normal file
14
lib/features/home/domain/usecases/remove_from_cart.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../../../../core/errors/failures.dart';
|
||||
import '../repositories/cart_repository.dart';
|
||||
|
||||
/// Use case to remove item from cart
|
||||
class RemoveFromCart {
|
||||
final CartRepository repository;
|
||||
|
||||
RemoveFromCart(this.repository);
|
||||
|
||||
Future<Either<Failure, void>> call(String productId) async {
|
||||
return await repository.removeFromCart(productId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user