static final Client client = AppwriteService.getClient();
static final Account account = Account(client);
static final Databases databases = Databases(client);

An analogy for this code could be a set of keys that allow you to access different rooms in a building. The client variable is like the master key that gives you access to the entire building. The account variable is like a key that allows you to access your personal room, while the databases variable is like a key that allows you to access the database room where you can manage and store data.


Future<bool> createEvent(MyEventModel event) async {
    try {
      DateTime eventDateTime =
          DateTime.parse('${event.eventDate} ${event.eventTime}');
      String formattedDate =
          DateFormat('yyyy-MM-dd HH:mm:ss').format(eventDateTime);

      // Make the eventID exactly equal to the documentID
      String eventId = const Uuid().v4().replaceAll('-', '');

      await databases.createDocument(
        databaseId: AppwriteConstants.databaseId,
        collectionId: AppwriteConstants.eventsCollectionId,
        // Assign the eventId to the documentId to have the same value
        documentId: eventId,
        data: {
          'eventId': eventId,
          'title': event.title,
          'date': formattedDate,
          'location': event.location,
          'creatorId': event.creatorId,
          'description': event.description,
        },
      );
      return true;
    } catch (e) {
      print('Failed to create event: $e');
      return false;
    }
  }

This code is a function/method called createEvent that creates an event using a provided MyEventModel object. Let's break it down step by step:

  1. The function takes an object event of type MyEventModel as input.
  2. It starts with a try block, which means it will attempt to execute the code within it and handle any errors that might occur.
  3. The code first combines the eventDate and eventTime properties from the event object to create a single DateTime object called eventDateTime. This combines the date and time into a single value.
  4. The code then formats the eventDateTime into a specific string format using the DateFormat class. The format used is 'yyyy-MM-dd HH:mm:ss'.
  5. Next, a unique event ID is generated using the Uuid class. The generated ID is a string without any hyphens ('-') since they are replaced with an empty string.
  6. The code calls a function named createDocument using the databases object. It passes several parameters to this function:
  7. The code awaits the completion of the createDocument function call. The await keyword is used because the function is marked as async, indicating that it may perform asynchronous operations.
  8. If the document creation is successful (no exceptions are thrown), the function returns true.
  9. If an exception occurs during the execution of the code within the try block, it is caught by the catch block.
  10. In case of an exception, the error message is printed to the console using print(), indicating that the event creation has failed, and the function returns false.

In summary, this code creates an event by converting the date and time into a specific format, generating a unique event ID, and then storing the event details in a database using the event ID as the document ID. If the event creation is successful, it returns true, otherwise false.


static Future<bool> joinEvent(AttendeesModel attendee) async {
    try {
      String documentId = const Uuid().v4().replaceAll('-', '');
      await databases.createDocument(
        databaseId: AppwriteConstants.databaseId,
        collectionId: AppwriteConstants.attendeesCollectionId,
        documentId: documentId,
        data: {
          'userId': attendee.userId,
          'eventId': attendee.eventId,
        },
      );
      return true;
    } catch (e) {
      print('Failed to join event: $e');
      return false;
    }
  }

This code is a static function called joinEvent that allows a user to join an event by creating an entry in the attendees collection. Let's break it down step by step:

  1. The function takes an object attendee of type AttendeesModel as input.