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);
  }
}