Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tuannguyenssu
Created August 15, 2019 15:27
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 tuannguyenssu/dd2962ec013426cc6d560d07f969426e to your computer and use it in GitHub Desktop.
Save tuannguyenssu/dd2962ec013426cc6d560d07f969426e to your computer and use it in GitHub Desktop.
using System.Threading.Tasks;
using CQRSTest.Application.Customers.Commands.CreateCustomer;
using CQRSTest.Application.Customers.Commands.DeleteCustomer;
using CQRSTest.Application.Customers.Commands.UpdateCustomer;
using CQRSTest.Application.Customers.Queries.GetCustomerDetail;
using CQRSTest.Application.Customers.Queries.GetCustomersList;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace CQRSTest.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class CustomersController : Controller
{
private readonly IMediator _mediator;
public CustomersController(IMediator mediator)
{
_mediator = mediator;
}
// GET api/customers
[HttpGet]
public async Task<ActionResult<CustomersListViewModel>> GetAll()
{
return Ok(await _mediator.Send(new GetCustomersListQuery()));
}
// GET api/customers/5
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
return Ok(await _mediator.Send(new GetCustomerDetailQuery { Id = id }));
}
// POST api/customers
[HttpPost]
public async Task<IActionResult> Create([FromBody]CreateCustomerCommand command)
{
return Ok(await _mediator.Send(command));
}
// PUT api/customers/5
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, [FromBody]UpdateCustomerCommand command)
{
if (command == null || command.Id != id)
{
return BadRequest();
}
return Ok(await _mediator.Send(command));
}
// DELETE api/customers/5
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _mediator.Send(new DeleteCustomerCommand { Id = id });
return NoContent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment