Initial and done prolly
This commit is contained in:
commit
6f88b9966f
175 changed files with 15445 additions and 0 deletions
52
lib/db/crud/product_category_crud.dart
Normal file
52
lib/db/crud/product_category_crud.dart
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
part of '../database.dart';
|
||||
|
||||
extension ProductCategoryCrud on AppDatabase {
|
||||
Stream<List<ProductCategoryData>> get productsCategoriesSubscription =>
|
||||
managers.productCategory.watch();
|
||||
|
||||
Future<void> addProductCategory({
|
||||
required String name,
|
||||
required String icon,
|
||||
}) async {
|
||||
await managers.productCategory.create((c) => c(
|
||||
name: name,
|
||||
icon: icon,
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> updateProductCategory({
|
||||
required int id,
|
||||
String? name,
|
||||
String? icon,
|
||||
}) async {
|
||||
await managers.productCategory.filter((f) => f.id(id)).update((u) => u(
|
||||
id: Value(id),
|
||||
name: Value.absentIfNull(name),
|
||||
icon: Value.absentIfNull(icon),
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> deleteProductCategory(ProductCategoryData item) async {
|
||||
await managers.productCategory.filter((f) => f.id(item.id)).delete();
|
||||
}
|
||||
|
||||
Future<List<ProductCategoryData>> getProductCategories() async {
|
||||
final categories = await managers.productCategory.get();
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
Future<List<(ProductCategoryData, int)>>
|
||||
getProductCategoriesWithCounts() async {
|
||||
final categories = await managers.productCategory.get();
|
||||
return [
|
||||
for (final category in categories)
|
||||
(
|
||||
category,
|
||||
await managers.product
|
||||
.filter((f) => f.category.id(category.id))
|
||||
.count()
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
137
lib/db/crud/product_crud.dart
Normal file
137
lib/db/crud/product_crud.dart
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
part of '../database.dart';
|
||||
|
||||
extension ProductCrud on AppDatabase {
|
||||
Stream<List<(ProductData, ProductCategoryData, StorageLocationData)>>
|
||||
get productsSubscription => managers.product
|
||||
.orderBy((o) => o.expiryDate.asc())
|
||||
.withReferences((prefetch) => prefetch(
|
||||
category: true,
|
||||
storage: true,
|
||||
))
|
||||
.watch()
|
||||
.asyncMap(
|
||||
(products) async => [
|
||||
for (final (item, refs) in products)
|
||||
(
|
||||
item,
|
||||
await refs.category.getSingle(),
|
||||
await refs.storage.getSingle(),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
Stream<List<(ProductData, ProductCategoryData, StorageLocationData)>>
|
||||
get soonExpirySubscription => managers.product
|
||||
.filter((f) =>
|
||||
f.expiryDate.isAfter(DateTime.now().add(const Duration(days: 3))))
|
||||
.orderBy((o) => o.expiryDate.asc())
|
||||
.withReferences((prefetch) => prefetch(
|
||||
category: true,
|
||||
storage: true,
|
||||
))
|
||||
.watch()
|
||||
.asyncMap(
|
||||
(products) async => [
|
||||
for (final (item, refs) in products)
|
||||
(
|
||||
item,
|
||||
await refs.category.getSingle(),
|
||||
await refs.storage.getSingle(),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
Future<void> addProduct({
|
||||
required String name,
|
||||
required ProductCategoryData category,
|
||||
required StorageLocationData storage,
|
||||
required double quantity,
|
||||
required String unit,
|
||||
DateTime? purchaseDate,
|
||||
required DateTime expiryDate,
|
||||
required String barcode,
|
||||
}) async {
|
||||
await managers.product.create((c) => c(
|
||||
name: name,
|
||||
category: category.id,
|
||||
storage: storage.id,
|
||||
quantity: quantity,
|
||||
unit: unit,
|
||||
barcode: barcode,
|
||||
purchaseDate: Value(purchaseDate ?? DateTime.now()),
|
||||
expiryDate: Value(expiryDate),
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> updateProduct({
|
||||
required int id,
|
||||
String? name,
|
||||
ProductCategoryData? category,
|
||||
StorageLocationData? storage,
|
||||
double? quantity,
|
||||
String? unit,
|
||||
DateTime? purchaseDate,
|
||||
DateTime? expiryDate,
|
||||
String? barcode,
|
||||
}) async {
|
||||
await managers.product.filter((f) => f.id(id)).update((u) => u(
|
||||
name: Value.absentIfNull(name),
|
||||
category: Value.absentIfNull(category?.id),
|
||||
storage: Value.absentIfNull(storage?.id),
|
||||
quantity: Value.absentIfNull(quantity),
|
||||
unit: Value.absentIfNull(unit),
|
||||
barcode: Value.absentIfNull(barcode),
|
||||
purchaseDate: Value.absentIfNull(purchaseDate),
|
||||
expiryDate: Value.absentIfNull(expiryDate),
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> deleteProduct(ProductData item) async {
|
||||
await managers.product.filter((f) => f.id(item.id)).delete();
|
||||
}
|
||||
|
||||
Future<List<(ProductData, ProductCategoryData, StorageLocationData)>>
|
||||
getProducts() async {
|
||||
final products = await managers.product
|
||||
.orderBy((o) => o.purchaseDate.asc())
|
||||
.withReferences((prefetch) => prefetch(
|
||||
category: true,
|
||||
storage: true,
|
||||
))
|
||||
.get();
|
||||
|
||||
return [
|
||||
for (final (item, refs) in products)
|
||||
(
|
||||
item,
|
||||
await refs.category.getSingle(),
|
||||
await refs.storage.getSingle(),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
Future<List<(ProductData, ProductCategoryData, StorageLocationData)>>
|
||||
getSoonExpiryProducts() async {
|
||||
final now = DateTime.now();
|
||||
final today = DateTime(now.year, now.month, now.day);
|
||||
final products = await managers.product
|
||||
.filter(
|
||||
(f) => f.expiryDate.isBefore(today.add(const Duration(days: 3))),
|
||||
)
|
||||
.orderBy((o) => o.expiryDate.desc())
|
||||
.withReferences((prefetch) => prefetch(
|
||||
category: true,
|
||||
storage: true,
|
||||
))
|
||||
.get();
|
||||
|
||||
return [
|
||||
for (final (item, refs) in products)
|
||||
(
|
||||
item,
|
||||
await refs.category.getSingle(),
|
||||
await refs.storage.getSingle(),
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
84
lib/db/crud/shopping_list_crud.dart
Normal file
84
lib/db/crud/shopping_list_crud.dart
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
part of '../database.dart';
|
||||
|
||||
extension ShoppingListItemCrud on AppDatabase {
|
||||
Stream<List<(ShoppingListItemData, ProductCategoryData, StorageLocationData)>>
|
||||
get shoppingListSubscription => managers.shoppingListItem
|
||||
.withReferences(
|
||||
(prefetch) => prefetch(
|
||||
category: true,
|
||||
storage: true,
|
||||
),
|
||||
)
|
||||
.watch()
|
||||
.asyncMap(
|
||||
(shoppingList) async => [
|
||||
for (final (item, refs) in shoppingList)
|
||||
(
|
||||
item,
|
||||
await refs.category.getSingle(),
|
||||
await refs.storage.getSingle(),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
Future<void> addShoppingListItem({
|
||||
required double quantity,
|
||||
required String name,
|
||||
required ProductCategoryData category,
|
||||
required StorageLocationData storage,
|
||||
required String unit,
|
||||
}) async {
|
||||
await managers.shoppingListItem.create((c) => c(
|
||||
quantity: quantity,
|
||||
name: name,
|
||||
category: category.id,
|
||||
storage: storage.id,
|
||||
unit: unit,
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> updateShoppingListItem({
|
||||
required int id,
|
||||
double? quantity,
|
||||
String? name,
|
||||
ProductCategoryData? category,
|
||||
StorageLocationData? storage,
|
||||
String? unit,
|
||||
bool? isPurchased,
|
||||
}) async {
|
||||
await managers.shoppingListItem.filter((f) => f.id(id)).update((u) => u(
|
||||
id: Value(id),
|
||||
quantity: Value.absentIfNull(quantity),
|
||||
isPurchased: Value.absentIfNull(isPurchased),
|
||||
name: Value.absentIfNull(name),
|
||||
category: Value.absentIfNull(category?.id),
|
||||
storage: Value.absentIfNull(storage?.id),
|
||||
unit: Value.absentIfNull(unit),
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> deleteShoppingListItem(ShoppingListItemData item) async {
|
||||
await managers.shoppingListItem.filter((f) => f.id(item.id)).delete();
|
||||
}
|
||||
|
||||
Future<List<(ShoppingListItemData, ProductCategoryData, StorageLocationData)>>
|
||||
getShoppingList() async {
|
||||
final shoppingList = await managers.shoppingListItem
|
||||
.withReferences(
|
||||
(prefetch) => prefetch(
|
||||
category: true,
|
||||
storage: true,
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
return [
|
||||
for (final (item, refs) in shoppingList)
|
||||
(
|
||||
item,
|
||||
await refs.category.getSingle(),
|
||||
await refs.storage.getSingle(),
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
88
lib/db/crud/storage_locations_crud.dart
Normal file
88
lib/db/crud/storage_locations_crud.dart
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
part of '../database.dart';
|
||||
|
||||
extension StorageLocationsCrud on AppDatabase {
|
||||
Stream<List<StorageLocationData>> get storageLocationsSubscription =>
|
||||
managers.storageLocation.watch();
|
||||
|
||||
Future<int> addStorageLocation({
|
||||
required String name,
|
||||
required String description,
|
||||
required TemperatureMode temperatureMode,
|
||||
required String icon,
|
||||
}) async {
|
||||
return await managers.storageLocation.create((c) => c(
|
||||
name: name,
|
||||
description: description,
|
||||
temperatureMode: temperatureMode.name,
|
||||
icon: icon,
|
||||
));
|
||||
}
|
||||
|
||||
Future<int> updateStorageLocation({
|
||||
required int id,
|
||||
String? name,
|
||||
String? description,
|
||||
TemperatureMode? temperatureMode,
|
||||
double? capacity,
|
||||
String? icon,
|
||||
}) async {
|
||||
return await managers.storageLocation
|
||||
.filter((f) => f.id(id))
|
||||
.update((u) => u(
|
||||
name: Value.absentIfNull(name),
|
||||
description: Value.absentIfNull(description),
|
||||
temperatureMode: Value.absentIfNull(temperatureMode?.name),
|
||||
icon: Value.absentIfNull(icon),
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> deleteStorageLocation(StorageLocationData item) async {
|
||||
await managers.storageLocation.filter((f) => f.id(item.id)).delete();
|
||||
}
|
||||
|
||||
Future<List<(StorageLocationData, List<(ProductData, ProductCategoryData)>)>>
|
||||
getStorageLocations() async {
|
||||
final storageLocations = await managers.storageLocation
|
||||
.withReferences((prefetch) => prefetch(productRefs: true))
|
||||
.get();
|
||||
|
||||
final result =
|
||||
<(StorageLocationData, List<(ProductData, ProductCategoryData)>)>[];
|
||||
|
||||
for (final (storage, refs) in storageLocations) {
|
||||
final products = await refs.productRefs
|
||||
.withReferences((prefetch) => prefetch(category: true))
|
||||
.get();
|
||||
final productsWithCategories = <(ProductData, ProductCategoryData)>[];
|
||||
for (final (product, refs) in products) {
|
||||
productsWithCategories.add((
|
||||
product,
|
||||
await refs.category.getSingle(),
|
||||
));
|
||||
}
|
||||
result.add((
|
||||
storage,
|
||||
productsWithCategories,
|
||||
));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<bool> defaultExists() async {
|
||||
final exists = await managers.storageLocation
|
||||
.filter((f) => f.isDefault(true))
|
||||
.exists();
|
||||
|
||||
return exists;
|
||||
}
|
||||
|
||||
Future<void> switchDefault(int newDefaultId) async {
|
||||
await managers.storageLocation
|
||||
.filter((f) => f.isDefault(true))
|
||||
.update((u) => u(isDefault: const Value(false)));
|
||||
await managers.storageLocation
|
||||
.filter((f) => f.id(newDefaultId))
|
||||
.update((u) => u(isDefault: const Value(true)));
|
||||
}
|
||||
}
|
||||
84
lib/db/crud/user_crud.dart
Normal file
84
lib/db/crud/user_crud.dart
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
part of '../database.dart';
|
||||
|
||||
extension UserCrud on AppDatabase {
|
||||
String hashPassword(String password) {
|
||||
final passwordBytes = utf8.encode(password);
|
||||
final hashedPassword = sha512224.convert(passwordBytes);
|
||||
return hashedPassword.toString();
|
||||
}
|
||||
|
||||
Future<UserData?> addUser({
|
||||
required String login,
|
||||
required String password,
|
||||
}) async {
|
||||
return await managers.user.createReturningOrNull((c) => c(
|
||||
login: login,
|
||||
password: hashPassword(password),
|
||||
));
|
||||
}
|
||||
|
||||
Future<List<UserData>> getUsers() async {
|
||||
return await managers.user.get();
|
||||
}
|
||||
|
||||
Future<bool> anyUserExists() async {
|
||||
return await managers.user.exists();
|
||||
}
|
||||
|
||||
Future<UserData?> findUser({
|
||||
required String login,
|
||||
required String password,
|
||||
}) async {
|
||||
return await managers.user
|
||||
.filter((f) => f.login(login))
|
||||
.filter((f) => f.password(hashPassword(password)))
|
||||
.getSingleOrNull();
|
||||
}
|
||||
|
||||
Future<UserData> getUser(UserData user) async {
|
||||
return await managers.user.filter((f) => f.id(user.id)).getSingle();
|
||||
}
|
||||
|
||||
Future<bool> updateUser(
|
||||
UserData currentUser,
|
||||
UserData updatedUser, {
|
||||
String? login,
|
||||
String? password,
|
||||
}) async {
|
||||
if (currentUser.id == updatedUser.id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
await managers.user.filter((f) => f.id(updatedUser.id)).update((u) => u(
|
||||
login: Value.absentIfNull(login),
|
||||
password: Value.absentIfNull(
|
||||
password != null ? hashPassword(password) : null),
|
||||
));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<bool> updateUserPassword(
|
||||
UserData user, {
|
||||
required String password,
|
||||
}) async {
|
||||
await managers.user.filter((f) => f.id(user.id)).update((u) => u(
|
||||
password: Value(hashPassword(password)),
|
||||
));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<bool> deleteUser(UserData user) async {
|
||||
final usersCount = await managers.user.count();
|
||||
if (usersCount == 0) {
|
||||
return false;
|
||||
}
|
||||
final deletedCount = await managers.user
|
||||
.filter(
|
||||
(f) => f.id(user.id),
|
||||
)
|
||||
.delete();
|
||||
return deletedCount != 0;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue