91 lines
3.0 KiB
C#
Raw Normal View History

using MediatR;
2017-03-15 15:04:13 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Queries;
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
{
[Route("api/v1/[controller]")]
[Authorize]
public class OrdersController : Controller
{
private readonly IMediator _mediator;
private readonly IOrderQueries _orderQueries;
2016-12-22 13:20:12 +01:00
private readonly IIdentityService _identityService;
2016-12-22 13:20:12 +01:00
public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService)
{
2016-12-22 13:20:12 +01:00
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
_orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries));
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
}
[Route("new")]
[HttpPost]
2017-03-14 18:02:28 +01:00
public async Task<IActionResult> CreateOrder([FromBody]CreateOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId)
{
bool commandResult = false;
2017-03-03 12:03:31 +01:00
if (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty)
{
2017-03-14 18:02:28 +01:00
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(command, guid);
commandResult = await _mediator.SendAsync(requestCreateOrder);
2017-03-03 12:03:31 +01:00
}
2017-03-03 12:10:30 +01:00
else
{
// If no x-requestid header is found we process the order anyway. This is just temporary to not break existing clients
// that aren't still updated. When all clients were updated this could be removed.
commandResult = await _mediator.SendAsync(command);
2017-03-03 12:10:30 +01:00
}
return commandResult ? (IActionResult)Ok() : (IActionResult)BadRequest();
}
[Route("{orderId:int}")]
[HttpGet]
public async Task<IActionResult> GetOrder(int orderId)
{
try
{
2017-04-17 12:28:12 +02:00
var order = await _orderQueries
.GetOrderAsync(orderId);
return Ok(order);
}
catch (KeyNotFoundException)
{
return NotFound();
}
}
[Route("")]
[HttpGet]
public async Task<IActionResult> GetOrders()
{
var orderTask = _orderQueries.GetOrdersAsync();
var orders = await orderTask;
return Ok(orders);
}
[Route("cardtypes")]
[HttpGet]
public async Task<IActionResult> GetCardTypes()
{
2017-04-17 12:28:12 +02:00
var cardTypes = await _orderQueries
.GetCardTypesAsync();
return Ok(cardTypes);
2017-03-14 18:02:28 +01:00
}
}
}