Sample: Copying an Activity Between Tenants
Operation: Copy Activity
Operation Model
public class CopyActivityInput : Entity
{
public required Guid TenantId { get; set; }
public required Guid ActivityId { get; set; }
}Operation Handler (API)
[OperationCommandHandler(AppConstants.Operations.CopyActivityName)]
public class CopyActivity(IDataServiceCreator dataServiceCreator)
: ICommandHandler<OperationCommand, OperationCommandResult>
{
public async Task<OperationCommandResult> HandleAsync(
OperationCommand command,
CancellationToken cancellationToken = default
)
{
try
{
var input =
command.InputEntity as CopyActivityInput
?? throw new Exception("Invalid input entity for CopyActivity operation");
// Get the destination tenant
var destinationTenant = await dataServiceCreator
.GetDataService<Tenant>(DataServiceCreatorArgs.Create<Tenant>(command.AppName))
.GetAsync(input.TenantId);
// Source data service (current tenant)
var sourceDataService = dataServiceCreator.GetDataService<Activity>(
DataServiceCreatorArgs.FromCommand<Activity>(command)
);
// Destination data service (target tenant)
var destinationDataService = dataServiceCreator.GetDataService<Activity>(
DataServiceCreatorArgs.Create<Activity>(command.AppName, destinationTenant.Name)
);
// Fetch the source activity
var sourceActivity = await sourceDataService.GetAsync(input.ActivityId);
// Assign a new ID for the destination copy
sourceActivity.Id = Guid.CreateVersion7();
// Save the copied activity into the destination tenant
await destinationDataService.SaveAsync([sourceActivity], true);
return OperationCommandResult.CreateSuccess(null);
}
catch (Exception ex)
{
Console.WriteLine($"Error copying activity: {ex.Message}");
return OperationCommandResult.CreateFailure();
}
}
}Page Definition
Key Concepts
Summary
PreviousReturning User Permissions and Config Values from the InitAppCommandNextBloqs 0.1.5 – Release Notes
Last updated