WT1.1.2f Purchase, Product, Property, BMR, BMI, Sizes

This commit is contained in:
bossanyit
2020-11-16 15:42:40 +01:00
parent 5fa5382e3f
commit e28f1edf50
48 changed files with 2631 additions and 1441 deletions
+76 -37
View File
@@ -1,5 +1,8 @@
import 'dart:collection';
import 'dart:convert';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/model/customer_property.dart';
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';
@@ -10,43 +13,32 @@ class CustomerApi {
Future<List<Customer>> getRealCustomers(String param) async {
final body = await _client.get("customers/", param);
final Iterable json = jsonDecode(body);
final List<Customer> customers = json.map((customer) =>
Customer.fromJson(customer)).toList();
final List<Customer> customers = json.map((customer) => Customer.fromJson(customer)).toList();
return customers;
}
Future<void> saveCustomer(Customer customer) async {
String body = JsonEncoder().convert(customer.toJson());
print(" ===== saving customer id: " + customer.customerId.toString() + ":" +
body);
await _client.post(
"customers/" + customer.customerId.toString(),
body);
print(" ===== 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);
await _client.post(
"customers/update_firebase_uid/" + customerId.toString(),
uid);
print(" ===== 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);
await _client.post(
"customers",
body);
await _client.post("customers", body);
}
Future<void> addUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== add new user: " + body);
final String responseBody = await _client.post(
"registration",
body);
final String responseBody = await _client.post("registration", body);
Customer customer;
try {
int status = jsonDecode(responseBody)['status'];
@@ -64,9 +56,7 @@ class CustomerApi {
Future<void> getUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== login the user: " + body);
final String responseBody = await _client.post(
"login",
body);
final String responseBody = await _client.post("login", body);
Customer customer;
try {
customer = Customer.fromJson(jsonDecode(responseBody));
@@ -78,13 +68,11 @@ class CustomerApi {
Future<void> getUserByEmail(String email) async {
print(" ===== User getByEmail : " + email);
final String responseBody = await _client.get(
"customers/find_by_email/" + email,
"");
final String responseBody = await _client.get("customers/find_by_email/" + email, "");
Customer customer;
try {
customer = Customer.fromJson(jsonDecode(responseBody));
if ( customer.firebaseUid == null ) {
if (customer.firebaseUid == null) {
await this.updateFirebaseUid(customer.customerId, Cache().firebaseUid);
}
Cache().userLoggedIn = customer;
@@ -93,17 +81,17 @@ class CustomerApi {
}
}
Future<void> getCustomer(int customerId) async {
String body = "";
print(" ===== get the customer by id: " + customerId.toString());
try {
final String responseBody = await _client.get(
"customers/" + customerId.toString(),
body);
final String responseBody = await _client.get("customers/" + customerId.toString(), body);
Customer customer = Customer.fromJson(jsonDecode(responseBody));
print(" --- Customer: " + customer.toJson().toString());
final List properties = await this.getActualProperties(customerId);
print(" ---- Props: " + properties.toString());
Cache().afterRegistration(customer);
this._initProperties(properties);
} catch (exception) {
print("Exception: " + exception.toString());
print(" === go to registration ");
@@ -112,14 +100,33 @@ class CustomerApi {
}
}
void _initProperties(List<CustomerProperty> customerProperties) {
List<Property> properties = Cache().getProperties();
Customer customer = Cache().userLoggedIn;
customer.properties = LinkedHashMap<String, CustomerProperty>();
// reset Properties
properties.forEach((property) {
CustomerProperty customerProperty =
CustomerProperty(propertyId: property.propertyId, customerId: customer.customerId, dateAdd: null, propertyValue: 0);
customer.properties[property.propertyName] = customerProperty;
});
customerProperties.forEach((customerProperty) {
properties.forEach((property) {
if (customerProperty.propertyId == property.propertyId) {
customer.properties[property.propertyName] = customerProperty;
}
});
});
}
Future<Customer> getTrainee(int customerId) async {
String body = "";
Customer customer;
print(" ===== get Trainee customer by id: " + customerId.toString());
try {
final String responseBody = await _client.get(
"customers/" + customerId.toString(),
body);
final String responseBody = await _client.get("customers/" + customerId.toString(), body);
customer = Customer.fromJson(jsonDecode(responseBody));
print(" --- Trainee: " + customer.toJson().toString());
} catch (exception) {
@@ -134,10 +141,7 @@ class CustomerApi {
print("Get trainees list");
try {
String body = "";
final String responseBody = await _client.get(
"customers/trainees/" + trainerId.toString(),
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) {
@@ -146,4 +150,39 @@ class CustomerApi {
}
return trainees;
}
}
Future<List<CustomerProperty>> getAllProperties(int customerId) async {
final body = await _client.get("customer_property/", customerId.toString());
final Iterable json = jsonDecode(body);
final List<CustomerProperty> properties = json.map((property) => CustomerProperty.fromJson(property)).toList();
return properties;
}
Future<List<CustomerProperty>> getActualProperties(int customerId) async {
final body = await _client.get("customer_property/last/", customerId.toString());
final Iterable json = jsonDecode(body);
final List<CustomerProperty> properties = json.map((property) => CustomerProperty.fromJson(property)).toList();
return properties;
}
Future<void> addProperty(CustomerProperty property) async {
String body = JsonEncoder().convert(property.toJson());
print(" ===== add new customer property: " + body);
final String responseBody = await _client.post("customer_property", body);
try {
int status = jsonDecode(responseBody)['status'];
if (status != null) {
throw new Exception(jsonDecode(responseBody)['error']);
} else {
CustomerProperty customerProperty = CustomerProperty.fromJson(jsonDecode(responseBody));
if (customerProperty == null) {
throw new Exception("Property Insert was not successful");
}
}
} on FormatException {
throw new Exception(responseBody);
}
}
}
+7 -19
View File
@@ -22,8 +22,6 @@ class FirebaseApi {
_instance = this;
}
// Define an async function to initialize FlutterFire
Future<void> initializeFlutterFire() async {
try {
@@ -38,10 +36,7 @@ class FirebaseApi {
Future<String> signInEmail(String email, String password) async {
String rc = SIGN_IN_OK;
try {
userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password
);
userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password);
Cache().firebaseUid = userCredential.user.uid;
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
@@ -60,10 +55,7 @@ class FirebaseApi {
Future<String> registerEmail(String email, String password) async {
String rc = SIGN_IN_OK;
try {
userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password
);
userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password);
Cache().firebaseUid = userCredential.user.uid;
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
@@ -73,12 +65,9 @@ class FirebaseApi {
} else if (e.code == 'email-already-in-use') {
print('The account already exists for that email.');
rc = REGISTER_EMAIL_IN_USE;
userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password
);
if ( rc != null ) {
String rc = SIGN_IN_OK;
userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password);
if (rc != null) {
rc = SIGN_IN_OK;
}
}
} catch (e) {
@@ -93,8 +82,7 @@ class FirebaseApi {
final LoginResult result = await FacebookAuth.instance.login();
// Create a credential from the access token
final FacebookAuthCredential facebookAuthCredential =
FacebookAuthProvider.credential(result.accessToken.token);
final FacebookAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(result.accessToken.token);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(facebookAuthCredential);
@@ -107,4 +95,4 @@ class FirebaseApi {
Future<void> resetPassword(String email) async {
await FirebaseAuth.instance.sendPasswordResetEmail(email: email);
}
}
}