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 with Logging { final APIClient _client = APIClient(); Future saveExercisePlan(ExercisePlan exercisePlan) async { String body = JsonEncoder().convert(exercisePlan.toJson()); log(" ===== saving exercisePlan $exercisePlan"); ExercisePlan savedExercisePlan; try { final String responseBody = await _client.post("exercise_plan", body); savedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody)); } on Exception catch (e) { throw new Exception(e.toString()); } return savedExercisePlan; } Future updateExercisePlan(ExercisePlan exercisePlan, int exercisePlanId) async { String body = JsonEncoder().convert(exercisePlan.toJson()); log(" ===== update exercisePlan $exercisePlan"); ExercisePlan updatedExercisePlan; try { final String responseBody = await _client.post("exercise_plan/" + exercisePlanId.toString(), body); updatedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody)); } on Exception catch (e) { throw new Exception(e.toString()); } return updatedExercisePlan; } Future saveExercisePlanDetail(ExercisePlanDetail exercisePlanDetail) async { String body = JsonEncoder().convert(exercisePlanDetail.toJson()); log(" ===== save exercisePlanDetail $exercisePlanDetail"); ExercisePlanDetail savedExercisePlanDetail; try { final String responseBody = await _client.post("exercise_plan_detail", body); savedExercisePlanDetail = ExercisePlanDetail.fromJson(jsonDecode(responseBody)); } on Exception catch (e) { throw new Exception(e.toString()); } return savedExercisePlanDetail; } Future updateExercisePlanDetail(ExercisePlanDetail exercisePlanDetail, int exercisePlanDetailId) async { String body = JsonEncoder().convert(exercisePlanDetail.toJson()); log(" ===== update exercisePlanDetail $exercisePlanDetail"); ExercisePlanDetail savedExercisePlanDetail; try { final String responseBody = await _client.post("exercise_plan_detail/" + exercisePlanDetailId.toString(), body); savedExercisePlanDetail = ExercisePlanDetail.fromJson(jsonDecode(responseBody)); } on Exception catch (e) { throw new Exception(e.toString()); } return savedExercisePlanDetail; } Future deleteExercisePlanDetail(int exercisePlanDetailId) async { log(" ===== delete exercisePlanDetail $exercisePlanDetailId"); String body = ""; try { await _client.post("exercise_plan_detail/delete/" + exercisePlanDetailId.toString(), body); } on Exception catch (e) { throw new Exception(e.toString()); } return; } Future deleteExercisePlan(int exercisePlanId) async { String body = ""; log(" ===== delete exercisePlan $exercisePlanId"); try { await _client.post("exercise_plan/delete/" + exercisePlanId.toString(), body); } on Exception catch (e) { throw new Exception(e.toString()); } return; } Future getLastExercisePlan(int customerId) async { String body = ""; log(" ===== get last exercisePlan $customerId"); ExercisePlan? exercisePlan; try { final String responseBody = await _client.get("exercise_plan/last/" + customerId.toString(), body); exercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody)); } on Exception catch (e) { print(e.toString()); if (e is NotFoundException) { log("ExercisePlan not found for " + customerId.toString()); return null; } else { throw new Exception(e.toString()); } } return exercisePlan; } Future> getExercisePlanDetail(int exercisePlanId) async { String body = ""; log(" ===== get exercisePlanDetail $exercisePlanId"); List listExercisePlanDetail = []; try { 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) { throw new Exception(e.toString()); } return listExercisePlanDetail; } }