Skip to content

Instantly share code, notes, and snippets.

@sfmskywalker
Created November 2, 2019 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfmskywalker/46b1503799655a1ad96f58fb3f7d2ee0 to your computer and use it in GitHub Desktop.
Save sfmskywalker/46b1503799655a1ad96f58fb3f7d2ee0 to your computer and use it in GitHub Desktop.
DeleteUser.cs - Building Workflow Driven .NET Core Applications with Elsa
using System.Threading;
using System.Threading.Tasks;
using Elsa.Attributes;
using Elsa.Expressions;
using Elsa.Results;
using Elsa.Samples.UserRegistration.Web.Models;
using Elsa.Services;
using Elsa.Services.Models;
using MongoDB.Driver;
namespace Elsa.Samples.UserRegistration.Web.Activities
{
[ActivityDefinition(Category = "Users", Description = "Delete a User", Icon = "fas fa-user-minus", Outcomes = new[]{ OutcomeNames.Done, "Not Found" })]
public class DeleteUser : Activity
{
private readonly IMongoCollection<User> _store;
public DeleteUser(IMongoCollection<User> store)
{
_store = store;
}
[ActivityProperty(Hint = "Enter an expression that evaluates to the ID of the user to activate.")]
public WorkflowExpression<string> UserId
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
protected override async Task<ActivityExecutionResult> OnExecuteAsync(WorkflowExecutionContext context, CancellationToken cancellationToken)
{
var userId = await context.EvaluateAsync(UserId, cancellationToken);
var result = await _store.DeleteOneAsync(x => x.Id == userId, cancellationToken);
return result.DeletedCount == 0 ? Outcome("Not Found") : Done();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment