Add Document Approval sample project

This commit is contained in:
Sipke Schoorstra 2021-04-02 16:56:08 +02:00
parent 7f38b01db5
commit 9d333ec5b8
8 changed files with 202 additions and 0 deletions

View file

@ -249,6 +249,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Server.Orleans", "src\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Server.Hangfire", "src\server\Elsa.Server.Hangfire\Elsa.Server.Hangfire.csproj", "{546C123A-F855-459F-886C-9D90AA14D12B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.DocumentApproval", "src\samples\aspnet\Elsa.Samples.DocumentApproval\Elsa.Samples.DocumentApproval.csproj", "{C890480A-F21B-4F6F-821A-67FA19DAD539}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -603,6 +605,10 @@ Global
{546C123A-F855-459F-886C-9D90AA14D12B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{546C123A-F855-459F-886C-9D90AA14D12B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{546C123A-F855-459F-886C-9D90AA14D12B}.Release|Any CPU.Build.0 = Release|Any CPU
{C890480A-F21B-4F6F-821A-67FA19DAD539}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C890480A-F21B-4F6F-821A-67FA19DAD539}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C890480A-F21B-4F6F-821A-67FA19DAD539}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C890480A-F21B-4F6F-821A-67FA19DAD539}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -722,6 +728,7 @@ Global
{2B67E954-3B04-402D-A9A7-AAAB1D6C3215} = {B43B546E-23F3-46E8-ACB7-D04F05CDA180}
{76BD888E-F0FD-40DD-B025-2ED773C1C50B} = {468E498C-59DB-4541-8D8A-0D89DE9083AB}
{546C123A-F855-459F-886C-9D90AA14D12B} = {468E498C-59DB-4541-8D8A-0D89DE9083AB}
{C890480A-F21B-4F6F-821A-67FA19DAD539} = {22E75696-6FE9-436A-9097-EE21C603F818}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8B0975FD-7050-48B0-88C5-48C33378E158}

View file

@ -0,0 +1,89 @@
using System.Net;
using System.Net.Http;
using Elsa.Activities.Console;
using Elsa.Activities.ControlFlow;
using Elsa.Activities.Http;
using Elsa.Activities.Http.Extensions;
using Elsa.Activities.Http.Models;
using Elsa.Activities.Primitives;
using Elsa.Builders;
namespace Elsa.Samples.DocumentApproval
{
/// <summary>
/// A workflow that gives Jack and Lucy a chance to review a submitted document.
/// </summary>
public class DocumentApprovalWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.HttpEndpoint(activity => activity
.WithMethod(HttpMethod.Post.Method)
.WithPath("/documents")
.WithReadContent())
.SetVariable("Document", context => context.GetInput<HttpRequestModel>()!.Body)
.WriteLine(context => $"Document received from {context.GetVariable<dynamic>("Document")!.Author.Name}")
.WriteHttpResponse(activity => activity
.WithContent(context => "<h1>Request for Approval Sent</h1><p>Your document has been received and will be reviewed shortly.</p>")
.WithContentType("text/html")
.WithStatusCode(HttpStatusCode.OK).WithResponseHeaders(new HttpResponseHeaders { ["X-Powered-By"] = "Elsa Workflows" })
)
.Then<Fork>(fork1 => fork1.WithBranches("Jack", "Lucy"), fork1 =>
{
fork1
.When("Jack")
.WriteLine(context => $"Jack approve url: \n {context.GenerateSignalUrl("Approve:Jack")}")
.Then<Fork>(fork2 => fork2.WithBranches("Approve", "Reject"), fork2 =>
{
fork2
.When("Approve")
.SignalReceived("Approve:Jack")
.SetVariable("Approved", context => context.SetVariable<int>("Approved", approved => approved == 0 ? 1 : approved))
.ThenNamed("JoinJack");
fork2
.When("Reject")
.SignalReceived("Reject:Jack")
.SetVariable<int>("Approved", 2)
.ThenNamed("JoinJack");
}).WithName("ForkJack")
.Add<Join>(x => x.WithMode(Join.JoinMode.WaitAny)).WithName("JoinJack")
.ThenNamed("JoinJackLucy");
fork1.When("Lucy")
.WriteLine(context => $"Lucy approve url: \n {context.GenerateSignalUrl("Approve:Lucy")}")
.Then<Fork>(fork2 => fork2.WithBranches("Approve", "Reject"),
fork2 =>
{
fork2
.When("Approve")
.SignalReceived("Approve:Lucy")
.SetVariable("Approved", context => context.SetVariable<int>("Approved", approved => approved == 0 ? 1 : approved))
.ThenNamed("JoinLucy");
fork2
.When("Reject")
.SignalReceived("Reject:Lucy")
.SetVariable<int>("Approved", 2)
.ThenNamed("JoinLucy");
}).WithName("ForkLucy")
.Add<Join>(x => x.WithMode(Join.JoinMode.WaitAny)).WithName("JoinLucy")
.ThenNamed("JoinJackLucy");
}
)
.Add<Join>(x => x.WithMode(Join.JoinMode.WaitAll)).WithName("JoinJackLucy")
.WriteLine(context => $"Approved: {context.GetVariable<int>("Approved")}").WithName("AfterJoinJackLucy")
.If(context => context.GetVariable<int>("Approved") == 1, @if =>
{
@if
.When(OutcomeNames.True)
.WriteLine(context => $"Document ${context.GetVariable<dynamic>("Document")!.Id} approved!`");
@if
.When(OutcomeNames.False)
.WriteLine(context => $"Document ${context.GetVariable<dynamic>("Document")!.Id} rejected!`");
});
}
}
}

View file

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\activities\Elsa.Activities.Http\Elsa.Activities.Http.csproj" />
<ProjectReference Include="..\..\..\core\Elsa\Elsa.csproj" />
<ProjectReference Include="..\..\..\persistence\Elsa.Persistence.EntityFramework\Elsa.Persistence.EntityFramework.Sqlite\Elsa.Persistence.EntityFramework.Sqlite.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Elsa.Samples.DocumentApproval
{
public class Program
{
public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
}

View file

@ -0,0 +1,26 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:62841/"
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Elsa.Samples.HelloWorldHttp": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:62841"
}
}
}

View file

@ -0,0 +1,31 @@
using Elsa.Persistence.EntityFramework.Core.Extensions;
using Elsa.Persistence.EntityFramework.Sqlite;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Samples.DocumentApproval
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services
.AddElsa(options => options
.UseEntityFrameworkPersistence(ef => ef.UseSqlite())
.AddConsoleActivities()
.AddHttpActivities()
.AddWorkflow<DocumentApprovalWorkflow>());
}
public void Configure(IApplicationBuilder app)
{
// Add HTTP activities middleware.
app.UseHttpActivities();
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
}
}

View file

@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information",
"Microsoft.EntityFrameworkCore": "Warning"
}
}
}

View file

@ -0,0 +1,11 @@
POST http://localhost:62841/documents
Content-Type: application/json
{
"Id": "document-1",
"Author": {
"Name": "Amanda"
}
}
###