genCollection method

Future<Collection?> genCollection(
  1. String collectionName
)

Implementation

Future<Collection?> genCollection(String collectionName) async {
  String finalName = "${collectionName.toLowerCase()}s";
  try {
    CollectionList clist =
        await databases.listCollections(databaseId: config.DATABASEID);

    for (final element in clist.collections) {
      //clist.collections.forEach((element) async {
      if (element.name == finalName) {
        String? yesno = "";
        String collectionId = element.$id;
        while (yesno != "y" && yesno != "n") {
          printAwogenMessage(
              "found collection with name '$finalName'. Delete ? y/n \n");

          yesno = stdin.readLineSync();
        }
        if (yesno?.toLowerCase() == "y") {
          await databases.deleteCollection(
              databaseId: config.DATABASEID, collectionId: collectionId);
          printAwogenMessage("collection with name $finalName deleted.\n");
        } else {
          String newname = "${element.name}_$collectionId";
          printAwogenMessage("renaming collection to $newname\n");

          await databases.updateCollection(
              databaseId: config.DATABASEID,
              collectionId: collectionId,
              name: newname);
          printAwogenMessage("renamed collection to $newname\n");
        }
      }
    }

    Future<Collection> result = databases.createCollection(
      databaseId: config.DATABASEID,
      collectionId: ID.unique(),
      name: finalName,
    );

    return result;
  } on AppwriteException catch (e) {
    print(e.toString());
    return null;
  }
}