feat: refactor API paths for authentication, inventory, and recipes; add contract tests for repositories
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_paths.dart';
|
||||
import '../domain/inventory_item.dart';
|
||||
import '../domain/inventory_consumption.dart';
|
||||
|
||||
@@ -20,21 +21,21 @@ class InventoryRepository {
|
||||
? ''
|
||||
: '?${params.entries.map((e) => '${e.key}=${Uri.encodeComponent(e.value)}').join('&')}';
|
||||
|
||||
final data = await _api.getJson('/inventory$query', token: token);
|
||||
final data = await _api.getJson('${InventoryApiPaths.list}$query', token: token);
|
||||
final list = data as List<dynamic>;
|
||||
return list.map((e) => InventoryItem.fromJson(e as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
Future<InventoryItem> fetchInventoryItem(int id, {String? token}) async {
|
||||
final data = await _api.getJson('/inventory/$id', token: token);
|
||||
return InventoryItem.fromJson(data as Map<String, dynamic>);
|
||||
final items = await fetchInventory(token: token);
|
||||
return items.firstWhere((item) => item.id == id);
|
||||
}
|
||||
|
||||
Future<InventoryItem> createInventoryItem(
|
||||
Map<String, dynamic> body, {
|
||||
String? token,
|
||||
}) async {
|
||||
final data = await _api.postJson('/inventory', body: body, token: token);
|
||||
final data = await _api.postJson(InventoryApiPaths.list, body: body, token: token);
|
||||
return InventoryItem.fromJson(data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
@@ -43,12 +44,12 @@ class InventoryRepository {
|
||||
Map<String, dynamic> body, {
|
||||
String? token,
|
||||
}) async {
|
||||
final data = await _api.patchJson('/inventory/$id', body: body, token: token);
|
||||
final data = await _api.patchJson(InventoryApiPaths.update(id), body: body, token: token);
|
||||
return InventoryItem.fromJson(data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
Future<void> deleteInventoryItem(int id, {String? token}) async {
|
||||
await _api.deleteJson('/inventory/$id', token: token);
|
||||
await _api.deleteJson(InventoryApiPaths.remove(id), token: token);
|
||||
}
|
||||
|
||||
Future<InventoryItem> consumeInventoryItem(
|
||||
@@ -59,7 +60,7 @@ class InventoryRepository {
|
||||
}) async {
|
||||
final body = <String, dynamic>{'amountUsed': amountUsed};
|
||||
if (comment != null && comment.isNotEmpty) body['comment'] = comment;
|
||||
final data = await _api.postJson('/inventory/$id/consume', body: body, token: token);
|
||||
final data = await _api.postJson(InventoryApiPaths.consume(id), body: body, token: token);
|
||||
return InventoryItem.fromJson(data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ class InventoryRepository {
|
||||
int id, {
|
||||
String? token,
|
||||
}) async {
|
||||
final data = await _api.getJson('/inventory/$id/consumption-history', token: token);
|
||||
final data = await _api.getJson(InventoryApiPaths.consumptionHistory(id), token: token);
|
||||
final list = data as List<dynamic>;
|
||||
return list
|
||||
.map((e) => InventoryConsumption.fromJson(e as Map<String, dynamic>))
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../../core/api/api_error_mapper.dart';
|
||||
import '../../../core/api/api_paths.dart';
|
||||
import '../../../core/api/api_providers.dart';
|
||||
import '../../../core/forms/form_options.dart';
|
||||
import '../../auth/data/auth_providers.dart';
|
||||
@@ -54,7 +55,7 @@ class _CreateInventoryScreenState
|
||||
try {
|
||||
final token = await ref.read(authStateProvider.future);
|
||||
final api = ref.read(apiClientProvider);
|
||||
final data = await api.getJson('/products', token: token);
|
||||
final data = await api.getJson(ProductApiPaths.list, token: token);
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_products = (data as List<dynamic>)
|
||||
|
||||
Reference in New Issue
Block a user