Update HttpEndpoints sample project
This commit is contained in:
parent
e443c30806
commit
005c0eb6ec
|
|
@ -0,0 +1,18 @@
|
|||
using AutoMapper;
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Workflows.Core.Models;
|
||||
|
||||
namespace Elsa.Samples.AspNet.HttpEndpoints.Activities;
|
||||
|
||||
public class MapTo<TSource, TDestination> : CodeActivity<TDestination>
|
||||
{
|
||||
public Input<TSource> Source { get; set; } = default!;
|
||||
|
||||
protected override void Execute(ActivityExecutionContext context)
|
||||
{
|
||||
var mapper = context.GetRequiredService<IMapper>();
|
||||
var source = Source.Get(context);
|
||||
var destination = mapper.Map<TDestination>(source);
|
||||
context.SetResult(destination);
|
||||
}
|
||||
}
|
||||
|
|
@ -13,4 +13,9 @@
|
|||
<ProjectReference Include="..\..\..\modules\Elsa.Workflows.Api\Elsa.Workflows.Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
namespace Elsa.Samples.AspNet.HttpEndpoints.Models;
|
||||
|
||||
public record CustomerDto(string Name, string Email);
|
||||
public record Customer(string Name, string Email);
|
||||
|
|
@ -1,18 +1,24 @@
|
|||
using Elsa.Extensions;
|
||||
using Elsa.Samples.AspNet.HttpEndpoints.Workflows;
|
||||
using Elsa.Samples.AspNet.HttpEndpoints.Models;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddElsa(elsa =>
|
||||
{
|
||||
// Add workflow.
|
||||
elsa.AddWorkflow<SubmissionWorkflow>();
|
||||
// Add activities.
|
||||
elsa.AddActivitiesFrom<Program>();
|
||||
|
||||
// Add workflows.
|
||||
elsa.AddWorkflowsFrom<Program>();
|
||||
|
||||
// Enable the HTTP feature for HTTP activities.
|
||||
elsa.UseHttp();
|
||||
});
|
||||
|
||||
// Add AutoMapper for demo purposes.
|
||||
builder.Services.AddAutoMapper(autoMapper => autoMapper.CreateMap<CustomerDto, Customer>());
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
var app = builder.Build();
|
||||
|
|
|
|||
|
|
@ -0,0 +1,67 @@
|
|||
using System.Net.Mime;
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Http;
|
||||
using Elsa.Samples.AspNet.HttpEndpoints.Activities;
|
||||
using Elsa.Samples.AspNet.HttpEndpoints.Models;
|
||||
using Elsa.Workflows.Core.Abstractions;
|
||||
using Elsa.Workflows.Core.Activities;
|
||||
using Elsa.Workflows.Core.Contracts;
|
||||
|
||||
namespace Elsa.Samples.AspNet.HttpEndpoints.Workflows;
|
||||
|
||||
/// <summary>
|
||||
/// A workflow that acts as an HTTP endpoint to which a client can POST a JSON payload which gts parsed into a strongly typed model.
|
||||
/// </summary>
|
||||
public class TypedSubmissionWorkflow : WorkflowBase
|
||||
{
|
||||
protected override void Build(IWorkflowBuilder builder)
|
||||
{
|
||||
// The payload will be deserialized into a Customer object. The JSON must look like this:
|
||||
// {
|
||||
// "name": "John Doe",
|
||||
// "email": "john.doe@localhost"
|
||||
// }
|
||||
var customerVariable = builder.WithVariable<CustomerDto>();
|
||||
|
||||
var mappedCustomerVariable = builder.WithVariable<Customer>();
|
||||
|
||||
builder.Root = new Sequence
|
||||
{
|
||||
Activities =
|
||||
{
|
||||
// Define an HTTP endpoint.
|
||||
new HttpEndpoint
|
||||
{
|
||||
Path = new("customers"),
|
||||
SupportedMethods = new(new[]{HttpMethods.Post}),
|
||||
CanStartWorkflow = true,
|
||||
ParsedContent = new(customerVariable)
|
||||
},
|
||||
|
||||
// Map the DTO to a domain model.
|
||||
new MapTo<CustomerDto, Customer>
|
||||
{
|
||||
Source = new(customerVariable),
|
||||
Result = new(mappedCustomerVariable)
|
||||
},
|
||||
|
||||
// Write a response.
|
||||
new WriteHttpResponse
|
||||
{
|
||||
ContentType = new(MediaTypeNames.Application.Json),
|
||||
Content = new(context =>
|
||||
{
|
||||
var customer = mappedCustomerVariable.Get(context)!;
|
||||
var name = customer.Name;
|
||||
var email = customer.Email;
|
||||
|
||||
return new
|
||||
{
|
||||
Message = $"Dear {name}, thank you for submitting your information! We'll send you an email at {email} shortly.",
|
||||
};
|
||||
}),
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue