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'; class CustomerApi { final APIClient _client = new APIClient(); Future> getRealCustomers(String param) async { final body = await _client.get("customers/", param); final Iterable json = jsonDecode(body); final List customers = json.map((customer) => Customer.fromJson(customer)).toList(); return customers; } Future 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); } Future updateFirebaseUid(int customerId, String uid) async { print(" ===== update Firebase uid : " + customerId.toString() + ": " + uid); await _client.post("customers/update_firebase_uid/" + customerId.toString(), uid); } Future addCustomer(Customer customer) async { String body = JsonEncoder().convert(customer.toJson()); print(" ===== add new customer: " + body); await _client.post("customers", body); } Future addUser(User user) async { String body = JsonEncoder().convert(user.toJson()); print(" ===== add new user: " + body); final String responseBody = await _client.post("registration", body); Customer customer; try { int status = jsonDecode(responseBody)['status']; if (status != null) { throw new Exception(jsonDecode(responseBody)['error']); } else { customer = Customer.fromJson(jsonDecode(responseBody)); Cache().afterRegistration(customer); } } on FormatException { throw new Exception(responseBody); } } Future getUser(User user) async { String body = JsonEncoder().convert(user.toJson()); print(" ===== login the user: " + body); final String responseBody = await _client.post("login", body); Customer customer; try { customer = Customer.fromJson(jsonDecode(responseBody)); await Cache().afterLogin(customer); } on FormatException { throw new Exception(responseBody); } } Future getUserByEmail(String email) async { print(" ===== User getByEmail : " + email); final String responseBody = await _client.get("customers/find_by_email/" + email, ""); Customer customer; try { customer = Customer.fromJson(jsonDecode(responseBody)); if (customer.firebaseUid == null) { await this.updateFirebaseUid(customer.customerId, Cache().firebaseUid); } Cache().userLoggedIn = customer; final List properties = await this.getActualProperties(customer.customerId); if (properties != null) { this._initProperties(properties); } } on FormatException { throw new Exception(responseBody); } } Future 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); 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); if (properties != null) { this._initProperties(properties); } } on Exception catch (exception) { print("Exception: " + exception.toString()); print(" === go to registration "); Cache().logout(); Cache().startPage = "registration"; } } void _initProperties(List customerProperties) { List properties = Cache().getProperties(); Customer customer = Cache().userLoggedIn; customer.properties = LinkedHashMap(); // 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 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); customer = Customer.fromJson(jsonDecode(responseBody)); print(" --- Trainee: " + customer.toJson().toString()); } catch (exception) { print("Exception: " + exception.toString()); throw Exception(exception); } return customer; } Future> getTrainees(int trainerId) async { List trainees = List(); print("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()); throw Exception(exception); } return trainees; } Future> getAllProperties(int customerId) async { final body = await _client.get("customer_property/", customerId.toString()); final Iterable json = jsonDecode(body); final List properties = json.map((property) => CustomerProperty.fromJson(property)).toList(); return properties; } Future> getActualProperties(int customerId) async { List properties; try { final body = await _client.get("customer_property/last/", customerId.toString()); final Iterable json = jsonDecode(body); properties = json.map((property) => CustomerProperty.fromJson(property)).toList(); if (properties != null) { properties.forEach((element) { print("Property " + element.toString()); }); } } on Exception catch (ex) { print(ex.toString()); } return properties; } Future 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); } } }