feat: add alot

This commit is contained in:
2026-10-08 04:12:36 +02:00
parent ccf5165417
commit fc607cb7bd
99 changed files with 4981 additions and 3655 deletions
+20 -52
View File
@@ -2,16 +2,16 @@ import 'package:drift/drift.dart';
import 'package:uuid/uuid.dart';
import '../database/app_database.dart';
import '../database/stock_queries.dart';
import '../models/product.dart';
class InsufficientStockException implements Exception {
const InsufficientStockException();
@override
String toString() => 'Not enough stock';
}
export '../database/stock_queries.dart'
show InsufficientStockException, ProductNotFoundException;
abstract class ProductService {
/// Active products, re-emitted whenever the products table changes.
Stream<List<Product>> watchProducts();
Future<List<Product>> getProducts();
Future<Product?> getProductById(String id);
@@ -53,15 +53,20 @@ class DriftProductService implements ProductService {
);
}
@override
Future<List<Product>> getProducts() async {
final query = database.select(database.products)
SimpleSelectStatement<$ProductsTable, ProductRow> _activeProductsQuery() {
return database.select(database.products)
..where((product) => product.active.equals(true))
..orderBy([(product) => OrderingTerm.asc(product.name)]);
}
final rows = await query.get();
@override
Stream<List<Product>> watchProducts() {
return _activeProductsQuery().map(_mapRowToProduct).watch();
}
return rows.map(_mapRowToProduct).toList();
@override
Future<List<Product>> getProducts() {
return _activeProductsQuery().map(_mapRowToProduct).get();
}
@override
@@ -129,49 +134,12 @@ class DriftProductService implements ProductService {
}
@override
Future<void> decreaseStock(String productId, int amount) async {
_validateStockAmount(amount);
final updatedRows = await database.customUpdate(
'UPDATE products '
'SET stock_quantity = stock_quantity - ? '
'WHERE id = ? AND stock_quantity >= ?',
variables: [
Variable.withInt(amount),
Variable.withString(productId),
Variable.withInt(amount),
],
updates: {database.products},
);
if (updatedRows == 1) return;
final product = await getProductById(productId);
if (product == null) throw Exception('Product not found');
throw const InsufficientStockException();
Future<void> decreaseStock(String productId, int amount) {
return database.decreaseStock(productId, amount);
}
@override
Future<void> increaseStock(String productId, int amount) async {
_validateStockAmount(amount);
final updatedRows = await database.customUpdate(
'UPDATE products '
'SET stock_quantity = stock_quantity + ? '
'WHERE id = ?',
variables: [Variable.withInt(amount), Variable.withString(productId)],
updates: {database.products},
);
if (updatedRows == 1) return;
throw Exception('Product not found');
}
void _validateStockAmount(int amount) {
if (amount <= 0) {
throw ArgumentError.value(amount, 'amount', 'Must be greater than zero');
}
Future<void> increaseStock(String productId, int amount) {
return database.increaseStock(productId, amount);
}
}