mirror of https://github.com/dapr/dotnet-sdk.git
151 lines
5.3 KiB
C#
151 lines
5.3 KiB
C#
// ------------------------------------------------------------
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
// ------------------------------------------------------------
|
|
|
|
namespace RoutingSample
|
|
{
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Dapr;
|
|
using Dapr.Client;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
/// <summary>
|
|
/// Startup class.
|
|
/// </summary>
|
|
public class Startup
|
|
{
|
|
/// <summary>
|
|
/// State store name.
|
|
/// </summary>
|
|
public const string StoreName = "statestore";
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Startup"/> class.
|
|
/// </summary>
|
|
/// <param name="configuration">Configuration.</param>
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
this.Configuration = configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the configuration.
|
|
/// </summary>
|
|
public IConfiguration Configuration { get; }
|
|
|
|
/// <summary>
|
|
/// Configures Services.
|
|
/// </summary>
|
|
/// <param name="services">Service Collection.</param>
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddDaprClient();
|
|
|
|
services.AddSingleton(new JsonSerializerOptions()
|
|
{
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
PropertyNameCaseInsensitive = true,
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Configures Application Builder and WebHost environment.
|
|
/// </summary>
|
|
/// <param name="app">Application builder.</param>
|
|
/// <param name="env">Webhost environment.</param>
|
|
/// <param name="serializerOptions">Options for JSON serialization.</param>
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSerializerOptions serializerOptions)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseCloudEvents();
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapSubscribeHandler();
|
|
|
|
endpoints.MapGet("{id}", Balance);
|
|
endpoints.MapPost("deposit", Deposit).WithTopic("deposit");
|
|
endpoints.MapPost("withdraw", Withdraw).WithTopic("withdraw");
|
|
});
|
|
|
|
async Task Balance(HttpContext context)
|
|
{
|
|
var client = context.RequestServices.GetRequiredService<DaprClient>();
|
|
|
|
var id = (string)context.Request.RouteValues["id"];
|
|
var account = await client.GetStateAsync<Account>(StoreName, id);
|
|
if (account == null)
|
|
{
|
|
context.Response.StatusCode = 404;
|
|
return;
|
|
}
|
|
|
|
context.Response.ContentType = "application/json";
|
|
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
|
}
|
|
|
|
async Task Deposit(HttpContext context)
|
|
{
|
|
var client = context.RequestServices.GetRequiredService<DaprClient>();
|
|
|
|
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
|
|
var account = await client.GetStateAsync<Account>(StoreName, transaction.Id);
|
|
if (account == null)
|
|
{
|
|
account = new Account() { Id = transaction.Id, };
|
|
}
|
|
|
|
if (transaction.Amount < 0m)
|
|
{
|
|
context.Response.StatusCode = 400;
|
|
return;
|
|
}
|
|
|
|
account.Balance += transaction.Amount;
|
|
await client.SaveStateAsync(StoreName, transaction.Id, account);
|
|
|
|
context.Response.ContentType = "application/json";
|
|
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
|
}
|
|
|
|
async Task Withdraw(HttpContext context)
|
|
{
|
|
var client = context.RequestServices.GetRequiredService<DaprClient>();
|
|
|
|
var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions);
|
|
var account = await client.GetStateAsync<Account>(StoreName, transaction.Id);
|
|
if (account == null)
|
|
{
|
|
context.Response.StatusCode = 404;
|
|
return;
|
|
}
|
|
|
|
if (transaction.Amount < 0m)
|
|
{
|
|
context.Response.StatusCode = 400;
|
|
return;
|
|
}
|
|
|
|
account.Balance -= transaction.Amount;
|
|
await client.SaveStateAsync(StoreName, transaction.Id, account);
|
|
|
|
context.Response.ContentType = "application/json";
|
|
await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions);
|
|
}
|
|
}
|
|
}
|
|
}
|