feat: alphabet list, change Layout etc

This commit is contained in:
2026-10-11 20:37:59 +02:00
parent 489896305d
commit 072f560989
31 changed files with 1847 additions and 832 deletions
+9 -1
View File
@@ -30,6 +30,10 @@ class Products extends Table {
IntColumn get lowStockThreshold => integer()();
/// Products like tap water that are never counted; their stock is ignored.
BoolColumn get infiniteStock =>
boolean().withDefault(const Constant(false))();
IntColumn get priceInCents => integer()();
TextColumn get imagePath => text().nullable()();
@@ -200,7 +204,7 @@ class AppDatabase extends _$AppDatabase {
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
@override
int get schemaVersion => 13;
int get schemaVersion => 14;
@override
MigrationStrategy get migration {
@@ -295,6 +299,10 @@ class AppDatabase extends _$AppDatabase {
await _backfillPurchaseRows();
await _deleteOrphanedPurchaseRows();
}
if (from < 14 && !await hasTableColumn('products', 'infinite_stock')) {
await migrator.addColumn(products, products.infiniteStock);
}
},
);
}
+77
View File
@@ -60,6 +60,21 @@ class $ProductsTable extends Products
type: DriftSqlType.int,
requiredDuringInsert: true,
);
static const VerificationMeta _infiniteStockMeta = const VerificationMeta(
'infiniteStock',
);
@override
late final GeneratedColumn<bool> infiniteStock = GeneratedColumn<bool>(
'infinite_stock',
aliasedName,
false,
type: DriftSqlType.bool,
requiredDuringInsert: false,
defaultConstraints: GeneratedColumn.constraintIsAlways(
'CHECK ("infinite_stock" IN (0, 1))',
),
defaultValue: const Constant(false),
);
static const VerificationMeta _priceInCentsMeta = const VerificationMeta(
'priceInCents',
);
@@ -102,6 +117,7 @@ class $ProductsTable extends Products
category,
stockQuantity,
lowStockThreshold,
infiniteStock,
priceInCents,
imagePath,
active,
@@ -161,6 +177,15 @@ class $ProductsTable extends Products
} else if (isInserting) {
context.missing(_lowStockThresholdMeta);
}
if (data.containsKey('infinite_stock')) {
context.handle(
_infiniteStockMeta,
infiniteStock.isAcceptableOrUnknown(
data['infinite_stock']!,
_infiniteStockMeta,
),
);
}
if (data.containsKey('price_in_cents')) {
context.handle(
_priceInCentsMeta,
@@ -213,6 +238,10 @@ class $ProductsTable extends Products
DriftSqlType.int,
data['${effectivePrefix}low_stock_threshold'],
)!,
infiniteStock: attachedDatabase.typeMapping.read(
DriftSqlType.bool,
data['${effectivePrefix}infinite_stock'],
)!,
priceInCents: attachedDatabase.typeMapping.read(
DriftSqlType.int,
data['${effectivePrefix}price_in_cents'],
@@ -240,6 +269,9 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
final String category;
final int stockQuantity;
final int lowStockThreshold;
/// Products like tap water that are never counted; their stock is ignored.
final bool infiniteStock;
final int priceInCents;
final String? imagePath;
final bool active;
@@ -249,6 +281,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
required this.category,
required this.stockQuantity,
required this.lowStockThreshold,
required this.infiniteStock,
required this.priceInCents,
this.imagePath,
required this.active,
@@ -261,6 +294,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
map['category'] = Variable<String>(category);
map['stock_quantity'] = Variable<int>(stockQuantity);
map['low_stock_threshold'] = Variable<int>(lowStockThreshold);
map['infinite_stock'] = Variable<bool>(infiniteStock);
map['price_in_cents'] = Variable<int>(priceInCents);
if (!nullToAbsent || imagePath != null) {
map['image_path'] = Variable<String>(imagePath);
@@ -276,6 +310,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
category: Value(category),
stockQuantity: Value(stockQuantity),
lowStockThreshold: Value(lowStockThreshold),
infiniteStock: Value(infiniteStock),
priceInCents: Value(priceInCents),
imagePath: imagePath == null && nullToAbsent
? const Value.absent()
@@ -295,6 +330,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
category: serializer.fromJson<String>(json['category']),
stockQuantity: serializer.fromJson<int>(json['stockQuantity']),
lowStockThreshold: serializer.fromJson<int>(json['lowStockThreshold']),
infiniteStock: serializer.fromJson<bool>(json['infiniteStock']),
priceInCents: serializer.fromJson<int>(json['priceInCents']),
imagePath: serializer.fromJson<String?>(json['imagePath']),
active: serializer.fromJson<bool>(json['active']),
@@ -309,6 +345,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
'category': serializer.toJson<String>(category),
'stockQuantity': serializer.toJson<int>(stockQuantity),
'lowStockThreshold': serializer.toJson<int>(lowStockThreshold),
'infiniteStock': serializer.toJson<bool>(infiniteStock),
'priceInCents': serializer.toJson<int>(priceInCents),
'imagePath': serializer.toJson<String?>(imagePath),
'active': serializer.toJson<bool>(active),
@@ -321,6 +358,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
String? category,
int? stockQuantity,
int? lowStockThreshold,
bool? infiniteStock,
int? priceInCents,
Value<String?> imagePath = const Value.absent(),
bool? active,
@@ -330,6 +368,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
category: category ?? this.category,
stockQuantity: stockQuantity ?? this.stockQuantity,
lowStockThreshold: lowStockThreshold ?? this.lowStockThreshold,
infiniteStock: infiniteStock ?? this.infiniteStock,
priceInCents: priceInCents ?? this.priceInCents,
imagePath: imagePath.present ? imagePath.value : this.imagePath,
active: active ?? this.active,
@@ -345,6 +384,9 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
lowStockThreshold: data.lowStockThreshold.present
? data.lowStockThreshold.value
: this.lowStockThreshold,
infiniteStock: data.infiniteStock.present
? data.infiniteStock.value
: this.infiniteStock,
priceInCents: data.priceInCents.present
? data.priceInCents.value
: this.priceInCents,
@@ -361,6 +403,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
..write('category: $category, ')
..write('stockQuantity: $stockQuantity, ')
..write('lowStockThreshold: $lowStockThreshold, ')
..write('infiniteStock: $infiniteStock, ')
..write('priceInCents: $priceInCents, ')
..write('imagePath: $imagePath, ')
..write('active: $active')
@@ -375,6 +418,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
category,
stockQuantity,
lowStockThreshold,
infiniteStock,
priceInCents,
imagePath,
active,
@@ -388,6 +432,7 @@ class ProductRow extends DataClass implements Insertable<ProductRow> {
other.category == this.category &&
other.stockQuantity == this.stockQuantity &&
other.lowStockThreshold == this.lowStockThreshold &&
other.infiniteStock == this.infiniteStock &&
other.priceInCents == this.priceInCents &&
other.imagePath == this.imagePath &&
other.active == this.active);
@@ -399,6 +444,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
final Value<String> category;
final Value<int> stockQuantity;
final Value<int> lowStockThreshold;
final Value<bool> infiniteStock;
final Value<int> priceInCents;
final Value<String?> imagePath;
final Value<bool> active;
@@ -409,6 +455,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
this.category = const Value.absent(),
this.stockQuantity = const Value.absent(),
this.lowStockThreshold = const Value.absent(),
this.infiniteStock = const Value.absent(),
this.priceInCents = const Value.absent(),
this.imagePath = const Value.absent(),
this.active = const Value.absent(),
@@ -420,6 +467,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
required String category,
required int stockQuantity,
required int lowStockThreshold,
this.infiniteStock = const Value.absent(),
required int priceInCents,
this.imagePath = const Value.absent(),
this.active = const Value.absent(),
@@ -436,6 +484,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
Expression<String>? category,
Expression<int>? stockQuantity,
Expression<int>? lowStockThreshold,
Expression<bool>? infiniteStock,
Expression<int>? priceInCents,
Expression<String>? imagePath,
Expression<bool>? active,
@@ -447,6 +496,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
if (category != null) 'category': category,
if (stockQuantity != null) 'stock_quantity': stockQuantity,
if (lowStockThreshold != null) 'low_stock_threshold': lowStockThreshold,
if (infiniteStock != null) 'infinite_stock': infiniteStock,
if (priceInCents != null) 'price_in_cents': priceInCents,
if (imagePath != null) 'image_path': imagePath,
if (active != null) 'active': active,
@@ -460,6 +510,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
Value<String>? category,
Value<int>? stockQuantity,
Value<int>? lowStockThreshold,
Value<bool>? infiniteStock,
Value<int>? priceInCents,
Value<String?>? imagePath,
Value<bool>? active,
@@ -471,6 +522,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
category: category ?? this.category,
stockQuantity: stockQuantity ?? this.stockQuantity,
lowStockThreshold: lowStockThreshold ?? this.lowStockThreshold,
infiniteStock: infiniteStock ?? this.infiniteStock,
priceInCents: priceInCents ?? this.priceInCents,
imagePath: imagePath ?? this.imagePath,
active: active ?? this.active,
@@ -496,6 +548,9 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
if (lowStockThreshold.present) {
map['low_stock_threshold'] = Variable<int>(lowStockThreshold.value);
}
if (infiniteStock.present) {
map['infinite_stock'] = Variable<bool>(infiniteStock.value);
}
if (priceInCents.present) {
map['price_in_cents'] = Variable<int>(priceInCents.value);
}
@@ -519,6 +574,7 @@ class ProductsCompanion extends UpdateCompanion<ProductRow> {
..write('category: $category, ')
..write('stockQuantity: $stockQuantity, ')
..write('lowStockThreshold: $lowStockThreshold, ')
..write('infiniteStock: $infiniteStock, ')
..write('priceInCents: $priceInCents, ')
..write('imagePath: $imagePath, ')
..write('active: $active, ')
@@ -3800,6 +3856,7 @@ typedef $$ProductsTableCreateCompanionBuilder =
required String category,
required int stockQuantity,
required int lowStockThreshold,
Value<bool> infiniteStock,
required int priceInCents,
Value<String?> imagePath,
Value<bool> active,
@@ -3812,6 +3869,7 @@ typedef $$ProductsTableUpdateCompanionBuilder =
Value<String> category,
Value<int> stockQuantity,
Value<int> lowStockThreshold,
Value<bool> infiniteStock,
Value<int> priceInCents,
Value<String?> imagePath,
Value<bool> active,
@@ -3875,6 +3933,11 @@ class $$ProductsTableFilterComposer
builder: (column) => ColumnFilters(column),
);
ColumnFilters<bool> get infiniteStock => $composableBuilder(
column: $table.infiniteStock,
builder: (column) => ColumnFilters(column),
);
ColumnFilters<int> get priceInCents => $composableBuilder(
column: $table.priceInCents,
builder: (column) => ColumnFilters(column),
@@ -3950,6 +4013,11 @@ class $$ProductsTableOrderingComposer
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<bool> get infiniteStock => $composableBuilder(
column: $table.infiniteStock,
builder: (column) => ColumnOrderings(column),
);
ColumnOrderings<int> get priceInCents => $composableBuilder(
column: $table.priceInCents,
builder: (column) => ColumnOrderings(column),
@@ -3994,6 +4062,11 @@ class $$ProductsTableAnnotationComposer
builder: (column) => column,
);
GeneratedColumn<bool> get infiniteStock => $composableBuilder(
column: $table.infiniteStock,
builder: (column) => column,
);
GeneratedColumn<int> get priceInCents => $composableBuilder(
column: $table.priceInCents,
builder: (column) => column,
@@ -4064,6 +4137,7 @@ class $$ProductsTableTableManager
Value<String> category = const Value.absent(),
Value<int> stockQuantity = const Value.absent(),
Value<int> lowStockThreshold = const Value.absent(),
Value<bool> infiniteStock = const Value.absent(),
Value<int> priceInCents = const Value.absent(),
Value<String?> imagePath = const Value.absent(),
Value<bool> active = const Value.absent(),
@@ -4074,6 +4148,7 @@ class $$ProductsTableTableManager
category: category,
stockQuantity: stockQuantity,
lowStockThreshold: lowStockThreshold,
infiniteStock: infiniteStock,
priceInCents: priceInCents,
imagePath: imagePath,
active: active,
@@ -4086,6 +4161,7 @@ class $$ProductsTableTableManager
required String category,
required int stockQuantity,
required int lowStockThreshold,
Value<bool> infiniteStock = const Value.absent(),
required int priceInCents,
Value<String?> imagePath = const Value.absent(),
Value<bool> active = const Value.absent(),
@@ -4096,6 +4172,7 @@ class $$ProductsTableTableManager
category: category,
stockQuantity: stockQuantity,
lowStockThreshold: lowStockThreshold,
infiniteStock: infiniteStock,
priceInCents: priceInCents,
imagePath: imagePath,
active: active,
+23 -15
View File
@@ -21,6 +21,7 @@ class ProductNotFoundException implements Exception {
/// Atomic stock updates shared by the product and tab services, so they can
/// run inside either one's transaction.
extension StockQueries on AppDatabase {
/// Products with infinite stock are only checked for existence.
Future<void> decreaseStock(String productId, int amount) async {
_validateStockAmount(amount);
@@ -28,6 +29,7 @@ extension StockQueries on AppDatabase {
await (update(products)..where(
(row) =>
row.id.equals(productId) &
row.infiniteStock.not() &
row.stockQuantity.isBiggerOrEqualValue(amount),
))
.write(
@@ -38,28 +40,34 @@ extension StockQueries on AppDatabase {
if (updatedRows == 1) return;
final product = await (select(
products,
)..where((row) => row.id.equals(productId))).getSingleOrNull();
if (product == null) throw ProductNotFoundException(productId);
throw const InsufficientStockException();
final product = await _findProduct(productId);
if (!product.infiniteStock) throw const InsufficientStockException();
}
Future<void> increaseStock(String productId, int amount) async {
_validateStockAmount(amount);
final updatedRows =
await (update(
products,
)..where((row) => row.id.equals(productId))).write(
ProductsCompanion.custom(
stockQuantity: products.stockQuantity + Variable(amount),
),
);
await (update(products)..where(
(row) => row.id.equals(productId) & row.infiniteStock.not(),
))
.write(
ProductsCompanion.custom(
stockQuantity: products.stockQuantity + Variable(amount),
),
);
if (updatedRows != 1) throw ProductNotFoundException(productId);
if (updatedRows != 1) await _findProduct(productId);
}
Future<ProductRow> _findProduct(String productId) async {
final product = await (select(
products,
)..where((row) => row.id.equals(productId))).getSingleOrNull();
if (product == null) throw ProductNotFoundException(productId);
return product;
}
void _validateStockAmount(int amount) {