Skip to content

Instantly share code, notes, and snippets.

@dcomartin

dcomartin/Cap.cs Secret

Created November 25, 2020 22:31
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 dcomartin/bfbdebd89f00c24a632b2f43158f30f4 to your computer and use it in GitHub Desktop.
Save dcomartin/bfbdebd89f00c24a632b2f43158f30f4 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using Sales.Contracts;
namespace Sales
{
public class PlaceOrderController : Controller
{
private readonly SalesDbContext _dbContext;
private readonly ICapPublisher _publisher;
public PlaceOrderController(SalesDbContext dbContext, ICapPublisher publisher)
{
_dbContext = dbContext;
_publisher = publisher;
}
[HttpPost("/sales/orders/{orderId:Guid}")]
public async Task<IActionResult> Action([FromRoute] Guid orderId)
{
using (var transaction = _dbContext.Database.BeginTransaction(_publisher))
{
_dbContext.Orders.Add(new Order
{
OrderId = orderId
});
await _dbContext.SaveChangesAsync();
var orderPlaced = new OrderPlaced
{
OrderId = orderId
};
await _publisher.PublishAsync(nameof(OrderPlaced), orderPlaced);
await transaction.CommitAsync();
}
return NoContent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment