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

72 lines
2.4 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 with ID 1 via registered constructor...");
let calendar = new_calendar(1).
name("My First Calendar").
description("A calendar for testing Rhai integration");
let event = new_event(1).
title("My First Event").
description("An event for testing Rhai integration")
.add_attendee(new_attendee(1));
calendar.add_event(1);
print("Type of calendar object: " + type_of(calendar));
print("Created calendar: " + calendar.name);
// Save the calendar to the database
set_calendar(db, calendar);
print("Calendar saved to database");
// Check if calendar exists and retrieve it
if calendar_exists(db, 1) {
let retrieved_calendar = get_calendar_by_id(db, 1);
print("Retrieved calendar: " + 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 1");
}
// Create another calendar
print("Creating a new calendar with ID 2 using builder methods...");
let calendar2 = new_calendar(2).
name("My Second Calendar").
description("Another calendar for testing");
set_calendar(db, calendar2);
print("Second calendar saved");
// 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, 1);
print("Deleted calendar with ID 1");
// Verify deletion
if !calendar_exists(db, 1) {
print("Calendar with ID 1 was successfully deleted");
} else {
print("Failed to delete calendar with ID 1");
}
// Count remaining calendars
let remaining_calendars = get_all_calendars(db);
print("Remaining calendars: " + remaining_calendars.len());