Files
recipe-app/flutter/lib/features/pantry/data/pantry_repository.dart
T
Nils-Johan Gynther 84ccabe2fe
Test Suite / test (24.15.0) (push) Has been cancelled
feat: Add functionality to move inventory items to pantry and enhance pantry management
- Implemented moveInventoryItemToPantry method in InventoryRepository to facilitate moving items from inventory to pantry.
- Enhanced InventoryScreen with a new header section providing context about the inventory.
- Added a button in SwipeableInventoryTile to move items to pantry with appropriate error handling.
- Introduced movePantryItemToInventory method in PantryRepository to support moving items back to inventory.
- Refactored PantryScreen to rename _addToInventory to _moveToInventory for clarity and updated UI to reflect changes.
- Added AdminPantryItem model to represent pantry items in the admin panel.
- Created AdminPantryPanel for managing pantry items, including moving items to inventory and listing users.
- Developed AdminPrivateProductsPanel for managing private products, allowing promotion to global products.
2026-05-11 09:06:30 +02:00

88 lines
2.7 KiB
Dart

import 'package:logging/logging.dart';
import '../../../core/api/api_client.dart';
import '../../../core/api/api_paths.dart';
import '../domain/pantry_item.dart';
import '../domain/pantry_product.dart';
final _logger = Logger('PantryRepository');
class PantryRepository {
final ApiClient _api;
const PantryRepository(this._api);
Future<List<PantryItem>> fetchPantry({String? token}) async {
try {
final data = await _api.getJson(PantryApiPaths.list, token: token);
final list = data as List<dynamic>;
_logger.info('Fetched ${list.length} pantry items');
return list
.map((e) => PantryItem.fromJson(e as Map<String, dynamic>))
.toList();
} catch (error) {
_logger.severe('Failed to fetch pantry items: $error');
rethrow;
}
}
Future<List<PantryProduct>> fetchProducts({String? token}) async {
try {
final data = await _api.getJson(ProductApiPaths.mine, token: token);
final list = data as List<dynamic>;
_logger.info('Fetched ${list.length} products');
return list
.map((e) => PantryProduct.fromJson(e as Map<String, dynamic>))
.toList();
} catch (error) {
_logger.severe('Failed to fetch products: $error');
rethrow;
}
}
Future<PantryItem> createPantryItem(
int productId, {
String? token,
String? location,
}) async {
try {
final data = await _api.postJson(
PantryApiPaths.list,
body: {
'productId': productId,
if (location != null && location.trim().isNotEmpty)
'location': location.trim(),
},
token: token,
);
_logger.info('Created pantry item for product ID: $productId');
return PantryItem.fromJson(data as Map<String, dynamic>);
} catch (error) {
_logger.severe('Failed to create pantry item: $error');
rethrow;
}
}
Future<void> deletePantryItem(int id, {String? token}) async {
try {
await _api.deleteJson(PantryApiPaths.remove(id), token: token);
_logger.info('Deleted pantry item with ID: $id');
} catch (error) {
_logger.severe('Failed to delete pantry item: $error');
rethrow;
}
}
Future<void> movePantryItemToInventory(
int id, {
required Map<String, dynamic> body,
String? token,
}) async {
try {
await _api.postJson(PantryApiPaths.moveToInventory(id), body: body, token: token);
_logger.info('Moved pantry item with ID: $id to inventory');
} catch (error) {
_logger.severe('Failed to move pantry item to inventory: $error');
rethrow;
}
}
}