2021-02-02 01:05:48 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using PoweredSoft.CQRS.Abstractions;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace PoweredSoft.CQRS.AspNetCore.Mvc
|
|
|
|
|
{
|
2021-02-07 00:12:41 -05:00
|
|
|
|
[Produces("application/json")]
|
2021-02-02 01:05:48 -05:00
|
|
|
|
[ApiController, Route("api/query/[controller]")]
|
|
|
|
|
public class QueryController<TQuery, TQueryResult> : Controller
|
|
|
|
|
where TQuery : class
|
|
|
|
|
{
|
2021-02-04 21:09:29 -05:00
|
|
|
|
[HttpPost, QueryControllerAuthorization]
|
2021-02-02 12:19:59 -05:00
|
|
|
|
public async Task<ActionResult<TQueryResult>> Handle([FromServices] IQueryHandler<TQuery, TQueryResult> handler,
|
2021-02-02 01:05:48 -05:00
|
|
|
|
[FromBody] TQuery query)
|
|
|
|
|
{
|
2021-02-02 12:19:59 -05:00
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
|
|
|
|
|
|
2021-02-05 14:05:18 -05:00
|
|
|
|
return Ok(await handler.HandleAsync(query, this.Request.HttpContext.RequestAborted));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet, QueryControllerAuthorization]
|
|
|
|
|
public async Task<ActionResult<TQueryResult>> HandleGet([FromServices] IQueryHandler<TQuery, TQueryResult> handler,
|
|
|
|
|
[FromQuery] TQuery query)
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
|
|
|
|
|
|
2021-02-02 12:19:59 -05:00
|
|
|
|
return Ok(await handler.HandleAsync(query, this.Request.HttpContext.RequestAborted));
|
2021-02-02 01:05:48 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|