wt1.1.1 ExercisePlan + ExercisePlan detail, Trainee

This commit is contained in:
Bossanyi Tibor
2020-09-16 15:41:39 +02:00
parent f0ba820385
commit 0b4ff2fb7f
54 changed files with 2985 additions and 313 deletions
+3
View File
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:aitrainer_app/util/common.dart';
import 'package:aitrainer_app/util/not_found_exception.dart';
import 'package:http/http.dart' as http;
import 'package:aitrainer_app/model/cache.dart';
@@ -22,6 +23,8 @@ class APIClient with Common {
);
if(response.statusCode == 200) {
return utf8.decode(response.bodyBytes);
} else if(response.statusCode == 404 ) {
throw NotFoundException(message: "Not Found");
} else {
throw Exception("Unable to perform HTTP request!");
}
+60 -23
View File
@@ -5,64 +5,65 @@ import 'package:aitrainer_app/service/api.dart';
import 'package:aitrainer_app/model/cache.dart';
class CustomerApi {
final APIClient _client=new APIClient();
final APIClient _client = new APIClient();
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 );
print(" ===== saving customer id: " + customer.customerId.toString() + ":" +
body);
await _client.post(
"customers/"+customer.customerId.toString(),
body);
"customers/" + customer.customerId.toString(),
body);
}
Future<void> addCustomer(Customer customer) async {
String body = JsonEncoder().convert(customer.toJson());
print(" ===== add new customer: " + body );
print(" ===== add new customer: " + body);
await _client.post(
"customers",
body);
"customers",
body);
}
Future<void> addUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== register new user: " + body );
print(" ===== register new user: " + body);
final String responseBody = await _client.post(
"registration",
body);
"registration",
body);
Customer customer;
try {
int status = jsonDecode(responseBody)['status'];
if ( status != null ) {
if (status != null) {
throw new Exception(jsonDecode(responseBody)['error']);
} else {
customer = Customer.fromJson(jsonDecode(responseBody));
Cache().afterRegistration(customer);
}
} on FormatException catch(exception) {
} on FormatException catch (exception) {
throw new Exception(responseBody);
}
}
Future<void> getUser(User user) async {
String body = JsonEncoder().convert(user.toJson());
print(" ===== login the user: " + body );
print(" ===== login the user: " + body);
final String responseBody = await _client.post(
"login",
body);
"login",
body);
Customer customer;
try {
customer = Customer.fromJson(jsonDecode(responseBody));
await Cache().afterLogin(customer);
} on FormatException catch(exception) {
} on FormatException catch (exception) {
throw new Exception(responseBody);
}
}
@@ -70,18 +71,54 @@ class CustomerApi {
Future<void> getCustomer(int customerId) async {
String body = "";
print(" ===== get the customer by id: " + customerId.toString() );
print(" ===== get the customer by id: " + customerId.toString());
try {
final String responseBody = await _client.get(
"customers/"+customerId.toString(),
body);
"customers/" + customerId.toString(),
body);
Customer customer = Customer.fromJson(jsonDecode(responseBody));
print(" --- Customer: " + customer.toJson().toString());
Cache().afterRegistration(customer);
} catch (exception) {
print ("Exception: " + exception.toString());
print (" === go to registration ");
print("Exception: " + exception.toString());
print(" === go to registration ");
Cache().logout();
Cache().startPage = "registration";
}
}
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);
customer = Customer.fromJson(jsonDecode(responseBody));
print(" --- Trainee: " + customer.toJson().toString());
} catch (exception) {
print("Exception: " + exception.toString());
throw Exception(exception);
}
return customer;
}
Future<List<Customer>> getTrainees(int trainerId) async {
List<Customer> trainees = List<Customer>();
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;
}
}
+145
View File
@@ -0,0 +1,145 @@
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/exercise_plan.dart';
import 'package:aitrainer_app/model/exercise_plan_detail.dart';
import 'package:aitrainer_app/util/not_found_exception.dart';
import 'dart:convert';
import 'api.dart';
class ExercisePlanApi {
final APIClient _client = new APIClient();
Future<ExercisePlan> saveExercisePlan(ExercisePlan exercisePlan) async {
String body = JsonEncoder().convert(exercisePlan.toJson());
print(" ===== saving exercisePlan $exercisePlan");
ExercisePlan savedExercisePlan;
try {
final String responseBody = await _client.post(
"exercise_plan",
body);
savedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));
Cache().setTraineeExercisePlan(savedExercisePlan);
} on Exception catch(e) {
throw new Exception(e.toString());
}
return savedExercisePlan;
}
Future<ExercisePlan> updateExercisePlan(
ExercisePlan exercisePlan,
int exercisePlanId) async {
String body = JsonEncoder().convert(exercisePlan.toJson());
print(" ===== saving exercisePlan $exercisePlan");
ExercisePlan updatedExercisePlan;
try {
final String responseBody = await _client.post(
"exercise_plan/" + exercisePlanId.toString(),
body);
updatedExercisePlan = ExercisePlan.fromJson(jsonDecode(responseBody));
Cache().setTraineeExercisePlan(updatedExercisePlan);
} on Exception catch(e) {
throw new Exception(e.toString());
}
return updatedExercisePlan;
}
Future<ExercisePlanDetail> saveExercisePlanDetail(ExercisePlanDetail exercisePlanDetail) async {
String body = JsonEncoder().convert(exercisePlanDetail.toJson());
print(" ===== update 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<ExercisePlanDetail> updateExercisePlanDetail(ExercisePlanDetail exercisePlanDetail,
int exercisePlanDetailId) async {
String body = JsonEncoder().convert(exercisePlanDetail.toJson());
print(" ===== 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<void> deleteExercisePlanDetail(int exercisePlanDetailId) async {
print(" ===== 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<void> deleteExercisePlan(int exercisePlanId) async {
String body = "";
print(" ===== delete exercisePlan $exercisePlanId");
try {
await _client.post(
"exercise_plan/delete/" + exercisePlanId.toString(),
body);
} on Exception catch(e) {
throw new Exception(e.toString());
}
return;
}
Future<ExercisePlan> getLastExercisePlan(int customerId) async {
String body = "";
print(" ===== 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) {
if ( e is NotFoundException) {
print("ExercisePlan not found for " + customerId.toString());
return exercisePlan;
} else {
throw new Exception(e.toString());
}
}
return exercisePlan;
}
Future<List<ExercisePlanDetail>> getExercisePlanDetail(int exercisePlanId) async {
String body = "";
print(" ===== get exercisePlanDetail $exercisePlanId");
List<ExercisePlanDetail> listExercisePlanDetail;
try {
final String responseBody = await _client.get(
"exercise_plan_detail/" + exercisePlanId.toString(),
body);
print("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;
}
}
+4 -2
View File
@@ -22,12 +22,14 @@ class ExerciseApi {
body);
}
Future<void> addExercise(Exercise exercise) async {
Future<Exercise> addExercise(Exercise exercise) async {
String body = JsonEncoder().convert(exercise.toJson());
print(" ===== add new exercise: " + body );
await _client.post(
final String response = await _client.post(
"exercises",
body);
final Exercise savedExercise = Exercise.fromJson(jsonDecode(response));
return savedExercise;
}
Future<List<Exercise>> getExercisesByCustomer(int customerId ) async {