Created
September 7, 2018 04:03
-
-
Save thangchung/a142a8c01657710e6399ca391274b439 to your computer and use it in GitHub Desktop.
Clean Domain-driven Design article - CartController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ApiController] | |
| [ApiVersion("1.0")] | |
| [Route("api/carts")] | |
| public class CartController : Controller | |
| { | |
| [HttpGet] | |
| [Route("{id}")] | |
| [Auth(Policy = "access_cart_api")] | |
| public async Task<IActionResult> Get([FromServices] IMediator eventor, Guid id, CancellationToken cancellationToken) | |
| { | |
| return await eventor.SendStream<GetCartRequest, GetCartResponse>( | |
| new GetCartRequest { CartId = id }, | |
| x => x.Result, | |
| cancellationToken); | |
| } | |
| [HttpPost] | |
| [Auth(Policy = "access_cart_api")] | |
| public async Task<IActionResult> Create([FromServices] IMediator eventor, InsertItemToNewCartRequest request, CancellationToken cancellationToken) => | |
| await eventor.SendStream<InsertItemToNewCartRequest, InsertItemToNewCartResponse>( | |
| request, | |
| x => x.Result, | |
| cancellationToken); | |
| [HttpPut] | |
| [Auth(Policy = "access_cart_api")] | |
| public async Task<IActionResult> Put([FromServices] IMediator eventor, UpdateItemInCartRequest request, CancellationToken cancellationToken) => | |
| await eventor.SendStream<UpdateItemInCartRequest, UpdateItemInCartResponse>( | |
| request, | |
| x => x.Result, | |
| cancellationToken); | |
| [HttpDelete] | |
| [Route("{cartId:guid}/items/{productId:guid}")] | |
| [Auth(Policy = "access_cart_api")] | |
| public async Task<IActionResult> RemoveItemInCart([FromServices] IMediator eventor, Guid cartId, Guid productId, CancellationToken cancellationToken) | |
| { | |
| return await eventor.SendStream<DeleteItemRequest, DeleteItemResponse>( | |
| new DeleteItemRequest { CartId = cartId, ProductId = productId }, | |
| x => x.ProductId, | |
| cancellationToken); | |
| } | |
| [HttpPut] | |
| [Route("{cartId:guid}/checkout")] | |
| [Auth(Policy = "access_cart_api")] | |
| public async Task<IActionResult> CheckoutCart([FromServices] IMediator eventor, Guid cartId, CancellationToken cancellationToken) | |
| { | |
| return await eventor.SendStream<CheckoutRequest, CheckoutResponse>( | |
| new CheckoutRequest { CartId = cartId }, | |
| x => x.IsSucceed, | |
| cancellationToken); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment