import 'dart:convert'; import 'package:aitrainer_app/model/customer.dart'; import 'package:aitrainer_app/model/user.dart'; import 'package:aitrainer_app/service/api.dart'; import 'package:aitrainer_app/model/auth.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 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(" ===== register new user: " + body ); final String responseBody = await _client.post( "registration", body); Customer customer = Customer.fromJson(jsonDecode(responseBody)); Auth().afterRegistration(customer); } 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 = Customer.fromJson(jsonDecode(responseBody)); Auth().afterRegistration(customer); } Future getCustomer(int customerId) async { String body = ""; print(" ===== get the customer by id: " + customerId.toString() ); final String responseBody = await _client.get( "customers/"+customerId.toString(), body); Customer customer = Customer.fromJson(jsonDecode(responseBody)); Auth().afterRegistration(customer); } }