30 lines
1.0 KiB
Dart
30 lines
1.0 KiB
Dart
import 'package:workouttest_util/model/cache.dart';
|
|
import 'package:workouttest_util/model/purchase.dart';
|
|
import 'package:workouttest_util/util/logging.dart';
|
|
import 'package:workouttest_util/util/not_found_exception.dart';
|
|
import 'dart:convert';
|
|
import 'api.dart';
|
|
|
|
class PurchaseApi with Logging {
|
|
final APIClient _client = APIClient();
|
|
|
|
Future<List<Purchase>> getPurchasesByCustomer(int customerId) async {
|
|
List<Purchase> purchases = [];
|
|
try {
|
|
final body = await _client.get("purchase/customer/$customerId", "");
|
|
final Iterable json = jsonDecode(body);
|
|
final List<Purchase> purchases = json.map((purchase) => Purchase.fromJson(purchase)).toList();
|
|
Cache().setPurchases(purchases);
|
|
} on NotFoundException catch (_) {
|
|
log("No purchases found");
|
|
}
|
|
return purchases;
|
|
}
|
|
|
|
Future<void> savePurchase(Purchase purchase) async {
|
|
String body = const JsonEncoder().convert(purchase.toJson());
|
|
log(" ===== saving purchase:$body");
|
|
await _client.post("purchase", body);
|
|
}
|
|
}
|