db/heromodels/examples/calendar_rhai/calendar.rhai
2025-05-22 23:57:33 +03:00

74 lines
3.1 KiB
Plaintext

// Get the database instance
let db = get_db();
// Create a new calendar using the constructor and builder methods
print("Creating a new calendar (ID will be DB-assigned) via registered constructor...");
let calendar = new_calendar(). // ID removed
name("My First Calendar").
description("A calendar for testing Rhai integration");
let event = new_event(). // ID removed
title("My First Event").
description("An event for testing Rhai integration")
.add_attendee(new_attendee(1)); // new_attendee(contact_id), not Attendee ID
// Add event's ID to calendar. event.id will be 0 if not saved separately.
// Calendar::add_event returns the modified calendar, so we re-assign.
calendar = calendar.add_event(event.id);
print("Type of calendar object: " + type_of(calendar));
print("Created calendar: " + calendar.name);
// Save the calendar to the database and capture the result with DB-assigned ID
let calendar = set_calendar(db, calendar); // Capture result
print("Calendar saved to database");
// Check if calendar exists and retrieve it
if calendar_exists(db, calendar.id) { // Use calendar.id from the saved object
let retrieved_calendar = get_calendar_by_id(db, calendar.id); // Use calendar.id
print("Retrieved calendar ID: " + retrieved_calendar.id + ", Name: " + retrieved_calendar.name);
// Access the 'description' field directly.
// Note: 'description' is Option<String>. Rhai handles options.
// You might want to check for 'is_some()' or 'is_none()' or use 'unwrap_or()' pattern if needed.
let desc = retrieved_calendar.description;
if desc != () && desc != "" { // Check against '()' for None and empty string
print("Description: " + desc);
} else {
print("No description available or it's None");
}
} else {
print("Failed to retrieve calendar with ID " + calendar.id);
}
// Create another calendar
print("Creating another new calendar (ID will be DB-assigned) using builder methods...");
let calendar2 = new_calendar(). // ID removed
name("My Second Calendar").
description("Another calendar for testing");
let calendar2 = set_calendar(db, calendar2); // Capture result
print("Second calendar saved with ID: " + calendar2.id);
// Get all calendars
let all_calendars = get_all_calendars(db);
print("Total calendars: " + all_calendars.len());
for cal_item in all_calendars { // Renamed loop variable to avoid conflict if 'calendar' is still in scope
// Access 'base_data.id' and 'name' fields directly
print("Calendar ID: " + cal_item.base_data.id + ", Name: " + cal_item.name);
}
// Delete a calendar
delete_calendar_by_id(db, calendar.id); // Use ID from the first calendar object
print("Attempted to delete calendar with ID " + calendar.id);
// Verify deletion
if !calendar_exists(db, calendar.id) { // Use ID from the first calendar object
print("Calendar with ID " + calendar.id + " was successfully deleted");
} else {
print("Failed to delete calendar with ID " + calendar.id + ", or it was not the one intended.");
}
// Count remaining calendars
let remaining_calendars = get_all_calendars(db);
print("Remaining calendars: " + remaining_calendars.len());