public async Task <ViewCommand> ProcessRelationActionAsync(UsageType usageType, string collectionAlias, IEntity relatedEntity, string id, EditContext editContext, string actionId, object?customData) { var collection = _collectionProvider.GetCollection(collectionAlias); var button = collection.FindButton(actionId); if (button == null) { throw new Exception($"Cannot determine which button triggered action for collection {collectionAlias}"); } await EnsureAuthorizedUserAsync(editContext, button); EnsureValidEditContext(editContext, button); var relationContainer = editContext.GenerateRelationContainer(); // since the id is known, get the entity variant from the entity var entityVariant = collection.GetEntityVariant(editContext.Entity); ViewCommand viewCommand; var context = new ButtonContext(null, customData); switch (await button.ButtonClickBeforeRepositoryActionAsync(editContext, context)) { case CrudType.View: viewCommand = new NavigateCommand { Uri = UriHelper.Node(Constants.View, collectionAlias, entityVariant, null, id) }; break; case CrudType.Edit: viewCommand = new NavigateCommand { Uri = UriHelper.Node(Constants.Edit, collectionAlias, entityVariant, null, id) }; break; case CrudType.Update: await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalUpdateAsync(id, null, editContext.Entity, relationContainer)); viewCommand = new ReloadCommand(id); break; case CrudType.Insert: var insertedEntity = await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalInsertAsync(null, editContext.Entity, relationContainer)); if (insertedEntity == null) { throw new Exception("Inserting the new entity failed."); } editContext.SwapEntity(insertedEntity); await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalAddAsync(relatedEntity, editContext.Entity.Id)); viewCommand = new UpdateParameterCommand { Action = Constants.New, CollectionAlias = collectionAlias, VariantAlias = entityVariant.Alias, ParentId = null, Id = editContext.Entity.Id }; break; case CrudType.Delete: await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalDeleteAsync(id, null)); viewCommand = new ReloadCommand(); break; case CrudType.Pick: await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalAddAsync(relatedEntity, id)); viewCommand = new ReloadCommand(); break; case CrudType.Remove: await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalRemoveAsync(relatedEntity, id)); viewCommand = new ReloadCommand(); break; case CrudType.None: viewCommand = new NoOperationCommand(); break; case CrudType.Refresh: viewCommand = new ReloadCommand(); break; default: throw new InvalidOperationException(); } await button.ButtonClickAfterRepositoryActionAsync(editContext, context); return(viewCommand); }
public async Task <ViewCommand> ProcessEntityActionAsync(UsageType usageType, string collectionAlias, string?parentId, string?id, EditContext editContext, string actionId, object?customData) { var collection = _collectionProvider.GetCollection(collectionAlias); var entityVariant = collection.GetEntityVariant(editContext.Entity); var button = collection.FindButton(actionId); if (button == null) { throw new Exception($"Cannot determine which button triggered action for collection {collectionAlias}"); } await EnsureAuthorizedUserAsync(editContext, button); EnsureValidEditContext(editContext, button); var relationContainer = editContext.GenerateRelationContainer(); ViewCommand viewCommand; var context = new ButtonContext(parentId, customData); switch (await button.ButtonClickBeforeRepositoryActionAsync(editContext, context)) { case CrudType.View: viewCommand = new NavigateCommand { Uri = UriHelper.Node(Constants.View, collectionAlias, entityVariant, parentId, id) }; break; case CrudType.Edit: viewCommand = new NavigateCommand { Uri = UriHelper.Node(Constants.Edit, collectionAlias, entityVariant, parentId, id) }; break; case CrudType.Update: await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalUpdateAsync(id ?? throw new InvalidOperationException(), parentId, editContext.Entity, relationContainer)); viewCommand = new ReloadCommand(id); break; case CrudType.Insert: var entity = await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalInsertAsync(parentId, editContext.Entity, relationContainer)); if (entity == null) { throw new Exception("Inserting the new entity failed."); } editContext.SwapEntity(entity); viewCommand = new NavigateCommand { Uri = UriHelper.Node(Constants.Edit, collectionAlias, entityVariant, parentId, editContext.Entity.Id) }; break; case CrudType.Delete: await EnsureCorrectConcurrencyAsync(() => collection.Repository.InternalDeleteAsync(id ?? throw new InvalidOperationException(), parentId)); viewCommand = new NavigateCommand { Uri = UriHelper.Collection(Constants.List, collectionAlias, parentId) }; break; case CrudType.None: viewCommand = new NoOperationCommand(); break; case CrudType.Refresh: viewCommand = new ReloadCommand(); break; case CrudType.Return: viewCommand = new ReturnCommand(); break; default: throw new InvalidOperationException(); } await button.ButtonClickAfterRepositoryActionAsync(editContext, context); return(viewCommand); }