WT1.1.0+40 Premium feature settings

This commit is contained in:
bossanyit
2020-12-28 14:39:54 +01:00
parent 5c7a3d67e8
commit 9755cc764a
84 changed files with 2069 additions and 1192 deletions
+14 -12
View File
@@ -1,20 +1,22 @@
import 'dart:convert';
import 'package:aitrainer_app/service/logging.dart';
import 'package:aitrainer_app/util/common.dart';
import 'package:aitrainer_app/util/not_found_exception.dart';
import 'package:http/http.dart' as http;
import 'package:aitrainer_app/model/cache.dart';
class APIClient with Common {
class APIClient with Common, Logging {
Future<String> get(String endPoint, String param) async {
final url = Cache.getBaseUrl() + endPoint + param;
print("-------- API get " + url);
trace("-------- API get " + url);
String authToken = Cache().getAuthToken();
if (authToken.length == 0) {
var responseJson = await APIClient.authenticateUser(Cache.username, Cache.password);
var responseJson = await this.authenticateUser(Cache.username, Cache.password);
authToken = responseJson['token'];
Cache().authToken = authToken;
}
final response = await http.get(url, headers: {'Content-Type': 'application/json', 'Authorization': "Bearer " + authToken});
print(" ------------get response code: " + response.statusCode.toString());
trace(" ------------get response code: " + response.statusCode.toString());
if (response.statusCode == 200) {
return utf8.decode(response.bodyBytes);
} else if (response.statusCode == 404) {
@@ -26,10 +28,10 @@ class APIClient with Common {
Future<String> post(String endPoint, String body) async {
final url = Cache.getBaseUrl() + endPoint;
print(" ------------ http/post endpoint $endPoint body $body - url: $url ");
trace(" ------------ http/post body $body - url: $url ");
String authToken = Cache().getAuthToken();
if (authToken.length == 0) {
var responseJson = await APIClient.authenticateUser(Cache.username, Cache.password);
var responseJson = await this.authenticateUser(Cache.username, Cache.password);
authToken = responseJson['token'];
}
@@ -38,18 +40,18 @@ class APIClient with Common {
headers: {'Content-Type': 'application/json; charset=UTF-8', 'Authorization': "Bearer " + authToken},
body: body,
);
print(" ------------post response code: " + response.statusCode.toString());
trace(" ------------post response code: " + response.statusCode.toString());
final String decodedResponse = utf8convert(response.body);
print(" ------------ response: $decodedResponse");
trace(" ------------ response: $decodedResponse");
return decodedResponse;
}
static dynamic authenticateUser(String email, String password) async {
dynamic authenticateUser(String email, String password) async {
var uri = Cache.getBaseUrl() + "authenticate";
try {
final body = '{"username":"$email", "password":"$password"}';
print("authentication with $email");
trace("authentication with $email");
final response = await http.post(uri, headers: {'Authorization': '1', 'Content-Type': 'application/json'}, body: body);
final responseCode = response.statusCode;
if (responseCode != 200) {
@@ -65,7 +67,7 @@ class APIClient with Common {
}
}
static fetch(var authToken, var endPoint) async {
Future<void> fetch(var authToken, var endPoint) async {
var uri = Cache.getBaseUrl() + endPoint;
try {
@@ -77,7 +79,7 @@ class APIClient with Common {
final responseJson = json.decode(response.body);
return responseJson;
} catch (exception) {
print(exception);
log(exception);
if (exception.toString().contains('SocketException')) {
return 'NetworkError';
} else {
@@ -1,10 +1,11 @@
import 'package:aitrainer_app/model/customer_exercise_device.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'package:aitrainer_app/util/not_found_exception.dart';
import 'dart:convert';
import 'api.dart';
class CustomerExerciseDeviceApi {
class CustomerExerciseDeviceApi with Logging {
final APIClient _client = new APIClient();
Future<List<CustomerExerciseDevice>> getDevices(int customerId) async {
@@ -14,7 +15,7 @@ class CustomerExerciseDeviceApi {
final Iterable json = jsonDecode(body);
devices = json.map((device) => CustomerExerciseDevice.fromJson(device)).toList();
} on NotFoundException catch (e) {
print("No devices found");
log("No devices found");
}
return devices;
}
@@ -23,7 +24,7 @@ class CustomerExerciseDeviceApi {
CustomerExerciseDevice savedDevice;
try {
final String body = JsonEncoder().convert(device.toJson());
print(" --- add customer_exercise_device: " + body);
log(" --- add customer_exercise_device: " + body);
final String responseBody = await _client.post("customer_exercise_device", body);
savedDevice = CustomerExerciseDevice.fromJson(jsonDecode(responseBody));
} on Exception catch (e) {
@@ -34,7 +35,7 @@ class CustomerExerciseDeviceApi {
Future<void> removeDevice(int id) async {
try {
print(" --- delete customer_exercise_device: " + id.toString());
log(" --- delete customer_exercise_device: " + id.toString());
await _client.post("customer_exercise_device/delete/" + id.toString(), "");
} on Exception catch (e) {
throw new Exception(e.toString());
+23 -21
View File
@@ -6,8 +6,9 @@ import 'package:aitrainer_app/model/property.dart';
import 'package:aitrainer_app/model/user.dart';
import 'package:aitrainer_app/service/api.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/service/logging.dart';
class CustomerApi {
class CustomerApi with Logging {
final APIClient _client = new APIClient();
Future<List<Customer>> getRealCustomers(String param) async {
@@ -20,24 +21,24 @@ class CustomerApi {
Future<void> saveCustomer(Customer customer) async {
String body = JsonEncoder().convert(customer.toJson());
print(" ===== saving customer id: " + customer.customerId.toString() + ":" + body);
log(" ===== saving customer id: " + customer.customerId.toString() + ":" + body);
await _client.post("customers/" + customer.customerId.toString(), body);
}
Future<void> updateFirebaseUid(int customerId, String uid) async {
print(" ===== update Firebase uid : " + customerId.toString() + ": " + uid);
log(" ===== update Firebase uid : " + customerId.toString() + ": " + uid);
await _client.post("customers/update_firebase_uid/" + customerId.toString(), uid);
}
Future<void> addCustomer(Customer customer) async {
String body = JsonEncoder().convert(customer.toJson());
print(" ===== add new customer: " + body);
log(" ===== add new customer: " + body);
await _client.post("customers", body);
}
Future<void> addUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== add new user: " + body);
log(" ===== add new user: " + body);
final String responseBody = await _client.post("registration", body);
Customer customer;
try {
@@ -55,7 +56,7 @@ class CustomerApi {
Future<void> getUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== login the user: " + body);
log(" ===== login the user: " + body);
final String responseBody = await _client.post("login", body);
Customer customer;
try {
@@ -67,7 +68,7 @@ class CustomerApi {
}
Future<void> getUserByEmail(String email) async {
print(" ===== User getByEmail : " + email);
log(" ===== User getByEmail : " + email);
final String responseBody = await _client.get("customers/find_by_email/" + email, "");
Customer customer;
try {
@@ -87,20 +88,21 @@ class CustomerApi {
Future<void> getCustomer(int customerId) async {
String body = "";
print(" ===== get the customer by id: " + customerId.toString());
log(" ===== get the customer by id: " + customerId.toString());
try {
final String responseBody = await _client.get("customers/" + customerId.toString(), body);
Customer customer = Customer.fromJson(jsonDecode(responseBody));
print(" --- Customer: " + customer.toJson().toString());
log(" --- Customer: " + customer.toJson().toString());
Cache().userLoggedIn = customer;
final List properties = await this.getActualProperties(customerId);
print(" ---- Props: " + properties.toString());
Cache().afterRegistration(customer);
//log(" ---- Props: " + properties.toJson().toString());
//await Cache().initCustomer(customerId);
if (properties != null) {
this._initProperties(properties);
}
} on Exception catch (exception) {
print("Exception: " + exception.toString());
print(" === go to registration ");
log("Exception: " + exception.toString());
log(" === go to registration ");
Cache().logout();
Cache().startPage = "registration";
}
@@ -130,13 +132,13 @@ class CustomerApi {
Future<Customer> getTrainee(int customerId) async {
String body = "";
Customer customer;
print(" ===== get Trainee customer by id: " + customerId.toString());
log(" ===== get Trainee customer by id: " + customerId.toString());
try {
final String responseBody = await _client.get("customers/" + customerId.toString(), body);
customer = Customer.fromJson(jsonDecode(responseBody));
print(" --- Trainee: " + customer.toJson().toString());
log(" --- Trainee: " + customer.toJson().toString());
} catch (exception) {
print("Exception: " + exception.toString());
log("Exception: " + exception.toString());
throw Exception(exception);
}
return customer;
@@ -144,14 +146,14 @@ class CustomerApi {
Future<List<Customer>> getTrainees(int trainerId) async {
List<Customer> trainees = List<Customer>();
print("Get trainees list");
log("Get trainees list");
try {
String body = "";
final String responseBody = await _client.get("customers/trainees/" + trainerId.toString(), body);
final Iterable json = jsonDecode(responseBody);
trainees = json.map((customer) => Customer.fromJson(customer)).toList();
} catch (exception) {
print("Exception: " + exception.toString());
log("Exception: " + exception.toString());
throw Exception(exception);
}
return trainees;
@@ -174,18 +176,18 @@ class CustomerApi {
if (properties != null) {
properties.forEach((element) {
print("Property " + element.toString());
//log("Property " + element.toString());
});
}
} on Exception catch (ex) {
print(ex.toString());
log(ex.toString());
}
return properties;
}
Future<void> addProperty(CustomerProperty property) async {
String body = JsonEncoder().convert(property.toJson());
print(" ===== add new customer property: " + body);
log(" ===== add new customer property: " + body);
final String responseBody = await _client.post("customer_property", body);
try {
int status = jsonDecode(responseBody)['status'];
+33 -59
View File
@@ -1,41 +1,34 @@
import 'package:aitrainer_app/model/exercise_plan.dart';
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'package:aitrainer_app/util/not_found_exception.dart';
import 'dart:convert';
import 'api.dart';
class ExercisePlanApi {
class ExercisePlanApi with Logging {
final APIClient _client = new APIClient();
Future<ExercisePlan> saveExercisePlan(ExercisePlan exercisePlan) async {
String body = JsonEncoder().convert(exercisePlan.toJson());
print(" ===== saving exercisePlan $exercisePlan");
log(" ===== saving exercisePlan $exercisePlan");
ExercisePlan savedExercisePlan;
try {
final String responseBody = await _client.post(
"exercise_plan",
body);
final String responseBody = await _client.post("exercise_plan", body);
savedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));
} on Exception catch(e) {
} on Exception catch (e) {
throw new Exception(e.toString());
}
return savedExercisePlan;
}
Future<ExercisePlan> updateExercisePlan(
ExercisePlan exercisePlan,
int exercisePlanId) async {
Future<ExercisePlan> updateExercisePlan(ExercisePlan exercisePlan, int exercisePlanId) async {
String body = JsonEncoder().convert(exercisePlan.toJson());
print(" ===== update exercisePlan $exercisePlan");
log(" ===== update exercisePlan $exercisePlan");
ExercisePlan updatedExercisePlan;
try {
final String responseBody = await _client.post(
"exercise_plan/" + exercisePlanId.toString(),
body);
final String responseBody = await _client.post("exercise_plan/" + exercisePlanId.toString(), body);
updatedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));
} on Exception catch(e) {
} on Exception catch (e) {
throw new Exception(e.toString());
}
return updatedExercisePlan;
@@ -43,44 +36,36 @@ class ExercisePlanApi {
Future<ExercisePlanDetail> saveExercisePlanDetail(ExercisePlanDetail exercisePlanDetail) async {
String body = JsonEncoder().convert(exercisePlanDetail.toJson());
print(" ===== save exercisePlanDetail $exercisePlanDetail");
log(" ===== save exercisePlanDetail $exercisePlanDetail");
ExercisePlanDetail savedExercisePlanDetail;
try {
final String responseBody = await _client.post(
"exercise_plan_detail",
body);
final String responseBody = await _client.post("exercise_plan_detail", body);
savedExercisePlanDetail = ExercisePlanDetail.fromJson(jsonDecode(responseBody));
} on Exception catch(e) {
} on Exception catch (e) {
throw new Exception(e.toString());
}
return savedExercisePlanDetail;
}
Future<ExercisePlanDetail> updateExercisePlanDetail(ExercisePlanDetail exercisePlanDetail,
int exercisePlanDetailId) async {
Future<ExercisePlanDetail> updateExercisePlanDetail(ExercisePlanDetail exercisePlanDetail, int exercisePlanDetailId) async {
String body = JsonEncoder().convert(exercisePlanDetail.toJson());
print(" ===== update exercisePlanDetail $exercisePlanDetail");
log(" ===== update exercisePlanDetail $exercisePlanDetail");
ExercisePlanDetail savedExercisePlanDetail;
try {
final String responseBody = await _client.post(
"exercise_plan_detail/" + exercisePlanDetailId.toString(),
body);
final String responseBody = await _client.post("exercise_plan_detail/" + exercisePlanDetailId.toString(), body);
savedExercisePlanDetail = ExercisePlanDetail.fromJson(jsonDecode(responseBody));
} on Exception catch(e) {
} on Exception catch (e) {
throw new Exception(e.toString());
}
return savedExercisePlanDetail;
}
Future<void> deleteExercisePlanDetail(int exercisePlanDetailId) async {
print(" ===== delete exercisePlanDetail $exercisePlanDetailId");
log(" ===== delete exercisePlanDetail $exercisePlanDetailId");
String body = "";
try {
await _client.post(
"exercise_plan_detail/delete/" + exercisePlanDetailId.toString(),
body);
} on Exception catch(e) {
await _client.post("exercise_plan_detail/delete/" + exercisePlanDetailId.toString(), body);
} on Exception catch (e) {
throw new Exception(e.toString());
}
return;
@@ -88,14 +73,11 @@ class ExercisePlanApi {
Future<void> deleteExercisePlan(int exercisePlanId) async {
String body = "";
print(" ===== delete exercisePlan $exercisePlanId");
log(" ===== delete exercisePlan $exercisePlanId");
try {
await _client.post(
"exercise_plan/delete/" + exercisePlanId.toString(),
body);
} on Exception catch(e) {
await _client.post("exercise_plan/delete/" + exercisePlanId.toString(), body);
} on Exception catch (e) {
throw new Exception(e.toString());
}
return;
@@ -103,16 +85,14 @@ class ExercisePlanApi {
Future<ExercisePlan> getLastExercisePlan(int customerId) async {
String body = "";
print(" ===== get last exercisePlan $customerId");
log(" ===== get last exercisePlan $customerId");
ExercisePlan exercisePlan;
try {
final String responseBody = await _client.get(
"exercise_plan/last/" + customerId.toString(),
body);
final String responseBody = await _client.get("exercise_plan/last/" + customerId.toString(), body);
exercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));
} on Exception catch(e) {
if ( e is NotFoundException) {
print("ExercisePlan not found for " + customerId.toString());
} on Exception catch (e) {
if (e is NotFoundException) {
log("ExercisePlan not found for " + customerId.toString());
return exercisePlan;
} else {
throw new Exception(e.toString());
@@ -123,22 +103,16 @@ class ExercisePlanApi {
Future<List<ExercisePlanDetail>> getExercisePlanDetail(int exercisePlanId) async {
String body = "";
print(" ===== get exercisePlanDetail $exercisePlanId");
log(" ===== get exercisePlanDetail $exercisePlanId");
List<ExercisePlanDetail> listExercisePlanDetail;
try {
final String responseBody = await _client.get(
"exercise_plan_detail/" + exercisePlanId.toString(),
body);
print("response body:" + responseBody);
final String responseBody = await _client.get("exercise_plan_detail/" + exercisePlanId.toString(), body);
log("response body:" + responseBody);
final Iterable json = jsonDecode(responseBody);
listExercisePlanDetail = json.map( (planDetail) => ExercisePlanDetail.fromJson(planDetail) ) .toList();
} on Exception catch(e) {
listExercisePlanDetail = json.map((planDetail) => ExercisePlanDetail.fromJson(planDetail)).toList();
} on Exception catch (e) {
throw new Exception(e.toString());
}
return listExercisePlanDetail;
}
}
}
+28
View File
@@ -0,0 +1,28 @@
import 'dart:convert';
import 'package:aitrainer_app/model/exercise_result.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'api.dart';
class ExerciseResultApi with Logging {
final APIClient _client = new APIClient();
Future<void> saveExerciseResult(ExerciseResult exerciseResult) async {
String body = JsonEncoder().convert(exerciseResult.toJson());
log(" ===== saving exercise result:" + body);
await _client.post("exercise_result", body);
}
Future<List<ExerciseResult>> getExerciseResultsByCustomer(int customerId) async {
final body = await _client.get("exercise_result/", customerId.toString());
final Iterable json = jsonDecode(body);
final List<ExerciseResult> exerciseResults = json.map((exerciseResult) {
ExerciseResult item = ExerciseResult.fromJson(exerciseResult);
return item;
}).toList();
//exercises.sort( (a, b) => b.dateAdd.compareTo(a.dateAdd) );
return exerciseResults;
}
}
+5 -4
View File
@@ -1,8 +1,9 @@
import 'dart:convert';
import 'package:aitrainer_app/model/exercise.dart';
import 'package:aitrainer_app/service/api.dart';
import 'package:aitrainer_app/service/logging.dart';
class ExerciseApi {
class ExerciseApi with Logging {
final APIClient _client = new APIClient();
Future<List<Exercise>> getExerciseTypes(String param) async {
@@ -15,7 +16,7 @@ class ExerciseApi {
Future<void> saveExercise(Exercise exercise) async {
String body = JsonEncoder().convert(exercise.toJson());
print(" ===== saving exercise id: " + exercise.exerciseId.toString() + ":" + body);
log(" ===== saving exercise id: " + exercise.exerciseId.toString() + ":" + body);
await _client.post("exercises/" + exercise.exerciseId.toString(), body);
}
@@ -33,7 +34,7 @@ class ExerciseApi {
Future<Exercise> addExercise(Exercise exercise) async {
String body = JsonEncoder().convert(exercise.toJson());
print(" ===== add new exercise: " + body);
log(" ===== add new exercise: " + body);
final String response = await _client.post("exercises", body);
final Exercise savedExercise = Exercise.fromJson(jsonDecode(response));
return savedExercise;
@@ -41,7 +42,7 @@ class ExerciseApi {
Future<void> deleteExercise(Exercise exercise) async {
int exerciseId = exercise.exerciseId;
print(" ===== delete exercise: " + exerciseId.toString());
log(" ===== delete exercise: " + exerciseId.toString());
await _client.post("exercises/" + exerciseId.toString(), "");
return;
}
+1 -1
View File
@@ -43,7 +43,7 @@ class ExerciseTreeApi {
index++;
}
});
print("ExerciseTree " + element.toJson().toString());
//print("ExerciseTree " + element.toJson().toString());
treeIndex++;
});
+4 -3
View File
@@ -2,8 +2,9 @@ import 'dart:convert';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/exercise_type.dart';
import 'package:aitrainer_app/service/api.dart';
import 'package:aitrainer_app/service/logging.dart';
class ExerciseTypeApi {
class ExerciseTypeApi with Logging {
final APIClient _client = new APIClient();
Future<List<ExerciseType>> getExerciseTypes() async {
@@ -16,13 +17,13 @@ class ExerciseTypeApi {
Future<void> saveExerciseType(ExerciseType exerciseType) async {
String body = JsonEncoder().convert(exerciseType.toJson());
print(" ===== saving exerciseType id: " + exerciseType.exerciseTypeId.toString() + ":" + body);
log(" ===== saving exerciseType id: " + exerciseType.exerciseTypeId.toString() + ":" + body);
await _client.post("exercise_type/" + exerciseType.exerciseTypeId.toString(), body);
}
Future<void> addExerciseType(ExerciseType exerciseType) async {
String body = JsonEncoder().convert(exerciseType.toJson());
print(" ===== add new exerciseType: " + body);
log(" ===== add new exerciseType: " + body);
await _client.post("exercise_type", body);
}
}
+8 -7
View File
@@ -1,9 +1,10 @@
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
//import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
class FirebaseApi {
class FirebaseApi with Logging {
static FirebaseApi _instance;
static final FirebaseAuth auth = FirebaseAuth.instance;
@@ -29,7 +30,7 @@ class FirebaseApi {
await Firebase.initializeApp();
} catch (e) {
// Set `_error` state to true if Firebase initialization fails
print("Error initializing Firebase");
log("Error initializing Firebase");
}
}
@@ -40,10 +41,10 @@ class FirebaseApi {
Cache().firebaseUid = userCredential.user.uid;
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('No user found for that email.');
log('No user found for that email.');
rc = SIGN_IN_NOT_FOUND;
} else if (e.code == 'wrong-password') {
print('Wrong password provided for that user.');
log('Wrong password provided for that user.');
rc = SIGN_IN_WRONG_PWD;
throw Exception("Customer does not exist or the password is wrong");
}
@@ -59,16 +60,16 @@ class FirebaseApi {
Cache().firebaseUid = userCredential.user.uid;
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
log('The password provided is too weak.');
rc = REGISTER_WEAK_PWD;
throw Exception("Password too short");
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
log('The account already exists for that email.');
rc = REGISTER_EMAIL_IN_USE;
throw Exception("The email address has been registered already");
}
} catch (e) {
print(e);
log(e);
throw Exception(e.toString());
}
return rc;
+16
View File
@@ -0,0 +1,16 @@
import 'package:aitrainer_app/util/env.dart';
import 'package:intl/intl.dart';
mixin Logging {
void log(String message) {
DateTime time = DateTime.now();
print(DateFormat('yyyy-MM-dd HH:mm:ss ').format(time) + message);
}
void trace(String message) {
String testEnv = EnvironmentConfig.test_env;
if (testEnv == "1") {
log(message);
}
}
}
+17
View File
@@ -0,0 +1,17 @@
import 'dart:convert';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/product.dart';
import 'package:aitrainer_app/service/api.dart';
class ProductApi {
final APIClient _client = new APIClient();
Future<List<Product>> getProducts() async {
final body = await _client.get("product/", "");
final Iterable json = jsonDecode(body);
final List<Product> products = json.map((product) => Product.fromJson(product)).toList();
Cache().setProducts(products);
return products;
}
}
+23
View File
@@ -0,0 +1,23 @@
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/product_test.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'dart:convert';
import 'api.dart';
class ProductTestApi with Logging {
final APIClient _client = new APIClient();
Future<List<ProductTest>> getProductTestByCustomer(int customerId) async {
final body = await _client.get("product_test/customer/" + customerId.toString(), "");
final Iterable json = jsonDecode(body);
final List<ProductTest> productTests = json.map((productTest) => ProductTest.fromJson(productTest)).toList();
Cache().productTests = productTests;
return productTests;
}
Future<void> saveProductTest(ProductTest productTest) async {
String body = JsonEncoder().convert(productTest.toJson());
log(" ===== saving productTest:" + body);
await _client.post("product_test/", body);
}
}
+23
View File
@@ -0,0 +1,23 @@
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/purchase.dart';
import 'package:aitrainer_app/service/logging.dart';
import 'dart:convert';
import 'api.dart';
class PurchaseApi with Logging {
final APIClient _client = new APIClient();
Future<List<Purchase>> getPurchasesByCustomer(int customerId) async {
final body = await _client.get("purchase/customer/" + customerId.toString(), "");
final Iterable json = jsonDecode(body);
final List<Purchase> purchases = json.map((purchase) => Purchase.fromJson(purchase)).toList();
Cache().setPurchases(purchases);
return purchases;
}
Future<void> savePurchase(Purchase purchase) async {
String body = JsonEncoder().convert(purchase.toJson());
log(" ===== saving purchase:" + body);
await _client.post("purchase/", body);
}
}