154 lines
3.9 KiB
Dart
154 lines
3.9 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import '../database/app_database.dart';
|
|
import '../models/product.dart';
|
|
|
|
abstract class ProductService {
|
|
Future<List<Product>> getProducts();
|
|
|
|
Future<Product?> getProductById(String id);
|
|
|
|
Future<void> createProduct({
|
|
required String name,
|
|
required String category,
|
|
required int stockQuantity,
|
|
required int lowStockThreshold,
|
|
required int priceInCents,
|
|
required String? imagePath,
|
|
});
|
|
|
|
Future<void> updateProduct(Product product);
|
|
|
|
Future<void> deleteProduct(String id);
|
|
|
|
Future<void> decreaseStock(String productId, int amount);
|
|
|
|
Future<void> increaseStock(String productId, int amount);
|
|
}
|
|
|
|
class DriftProductService implements ProductService {
|
|
final AppDatabase database;
|
|
final _uuid = const Uuid();
|
|
|
|
DriftProductService({required this.database});
|
|
|
|
Product _mapRowToProduct(ProductRow row) {
|
|
return Product(
|
|
id: row.id,
|
|
name: row.name,
|
|
category: row.category,
|
|
stockQuantity: row.stockQuantity,
|
|
lowStockThreshold: row.lowStockThreshold,
|
|
priceInCents: row.priceInCents,
|
|
imagePath: row.imagePath,
|
|
active: row.active,
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<List<Product>> getProducts() async {
|
|
final query = database.select(database.products)
|
|
..where((product) => product.active.equals(true))
|
|
..orderBy([(product) => OrderingTerm.asc(product.name)]);
|
|
|
|
final rows = await query.get();
|
|
|
|
return rows.map(_mapRowToProduct).toList();
|
|
}
|
|
|
|
@override
|
|
Future<Product?> getProductById(String id) async {
|
|
final query = database.select(database.products)
|
|
..where((product) => product.id.equals(id));
|
|
|
|
final row = await query.getSingleOrNull();
|
|
|
|
if (row == null) {
|
|
return null;
|
|
}
|
|
|
|
return _mapRowToProduct(row);
|
|
}
|
|
|
|
@override
|
|
Future<void> createProduct({
|
|
required String name,
|
|
required String category,
|
|
required int stockQuantity,
|
|
required int lowStockThreshold,
|
|
required int priceInCents,
|
|
required String? imagePath,
|
|
}) async {
|
|
await database
|
|
.into(database.products)
|
|
.insert(
|
|
ProductsCompanion.insert(
|
|
id: _uuid.v4(),
|
|
name: name,
|
|
category: category,
|
|
stockQuantity: stockQuantity,
|
|
lowStockThreshold: lowStockThreshold,
|
|
priceInCents: priceInCents,
|
|
imagePath: Value(imagePath),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<void> updateProduct(Product product) async {
|
|
final query = database.update(database.products)
|
|
..where((row) => row.id.equals(product.id));
|
|
|
|
await query.write(
|
|
ProductsCompanion(
|
|
name: Value(product.name),
|
|
category: Value(product.category),
|
|
stockQuantity: Value(product.stockQuantity),
|
|
lowStockThreshold: Value(product.lowStockThreshold),
|
|
priceInCents: Value(product.priceInCents),
|
|
imagePath: Value(product.imagePath),
|
|
active: Value(product.active),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<void> deleteProduct(String id) async {
|
|
final query = database.delete(database.products)
|
|
..where((product) => product.id.equals(id));
|
|
|
|
await query.go();
|
|
}
|
|
|
|
@override
|
|
Future<void> decreaseStock(String productId, int amount) async {
|
|
final product = await getProductById(productId);
|
|
|
|
if (product == null) {
|
|
throw Exception('Product not found');
|
|
}
|
|
|
|
if (product.stockQuantity < amount) {
|
|
throw Exception('Not enough stock');
|
|
}
|
|
|
|
await updateProduct(
|
|
product.copyWith(stockQuantity: product.stockQuantity - amount),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Future<void> increaseStock(String productId, int amount) async {
|
|
final product = await getProductById(productId);
|
|
|
|
if (product == null) {
|
|
throw Exception('Product not found');
|
|
}
|
|
|
|
await updateProduct(
|
|
product.copyWith(stockQuantity: product.stockQuantity + amount),
|
|
);
|
|
}
|
|
}
|