addSchool method

Future<int> addSchool(
  1. String canton,
  2. String name
)

Implementation

Future<int> addSchool(String canton, String name) async {
  final List<Either<String, List<dynamic>>> responses = await Future.wait(
    [mappingGetRequest("/school").run(), mappingGetRequest("/cantons").run()],
  );
  final List<dynamic> schools =
      responses.first.getOrElse((String l) => <Map<String, dynamic>>[]);
  final List<dynamic> cantons =
      responses.last.getOrElse((String l) => <Map<String, dynamic>>[]);
  int cantonId = 0;
  for (final Map<String, dynamic> element in cantons) {
    if (element["canton"] == canton) {
      cantonId = element["id"];
      break;
    }
  }
  for (final Map<String, dynamic> element in schools) {
    if (element["name"] == name && element["canton"] == cantonId) {
      return element["id"];
    }
  }

  final Either<String, Map<String, dynamic>> res = await mappingPostRequest(
    "/school",
    <String, dynamic>{
      "canton": cantonId,
      "name": name,
    },
  ).run();

  return res.getOrElse((String l) => <String, dynamic>{})["id"];
}