Files
recipe-app/flutter/lib/features/pantry/data/pantry_repository.dart
T
Nils-Johan Gynther 68476142c1
Test Suite / test (24.15.0) (push) Has been cancelled
refactor: Remove PantryProduct class and simplify category resolution in PantryScreen
2026-05-11 20:01:00 +02:00

73 lines
2.1 KiB
Dart

import 'package:logging/logging.dart';
import '../../../core/api/api_client.dart';
import '../../../core/api/api_paths.dart';
import '../domain/pantry_item.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<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;
}
}
}