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:
event
of type MyEventModel
as input.try
block, which means it will attempt to execute the code within it and handle any errors that might occur.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.eventDateTime
into a specific string format using the DateFormat
class. The format used is 'yyyy-MM-dd HH:mm:ss'.Uuid
class. The generated ID is a string without any hyphens ('-') since they are replaced with an empty string.createDocument
using the databases
object. It passes several parameters to this function:
databaseId
: A constant value representing the ID of a specific database.collectionId
: A constant value representing the ID of a specific collection within the database.documentId
: The unique event ID generated earlier. This is used as the ID of the document to be created.data
: A map containing key-value pairs representing the data to be stored in the document. The keys include 'eventId'
, 'title'
, 'date'
, 'location'
, 'creatorId'
, and 'description'
, and their corresponding values are taken from the event
object.createDocument
function call. The await
keyword is used because the function is marked as async
, indicating that it may perform asynchronous operations.true
.try
block, it is caught by the catch
block.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:
attendee
of type AttendeesModel
as input.