151 lines
5.6 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;
2019-02-21 15:56:15 +00:00
using Microsoft.Extensions.Logging;
using Ordering.API.Application.Behaviors;
using Ordering.API.Application.Commands;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
{
[Route("api/v1/[controller]")]
[Authorize]
[ApiController]
2018-11-14 16:21:50 +01:00
public class OrdersController : ControllerBase
{
private readonly IMediator _mediator;
private readonly IOrderQueries _orderQueries;
2016-12-22 13:20:12 +01:00
private readonly IIdentityService _identityService;
2019-02-21 15:56:15 +00:00
private readonly ILogger<OrdersController> _logger;
2019-02-21 15:56:15 +00:00
public OrdersController(
IMediator mediator,
IOrderQueries orderQueries,
IIdentityService identityService,
ILogger<OrdersController> logger)
{
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
_orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries));
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
2019-02-21 15:56:15 +00:00
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
[Route("cancel")]
[HttpPut]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> CancelOrderAsync([FromBody]CancelOrderCommand 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)
{
var requestCancelOrder = new IdentifiedCommand<CancelOrderCommand, bool>(command, guid);
2019-02-21 15:56:15 +00:00
_logger.LogInformation(
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
requestCancelOrder.GetGenericTypeName(),
nameof(requestCancelOrder.Command.OrderNumber),
requestCancelOrder.Command.OrderNumber,
requestCancelOrder);
2017-06-12 13:52:23 +02:00
commandResult = await _mediator.Send(requestCancelOrder);
2017-03-03 12:03:31 +01:00
}
if (!commandResult)
{
return BadRequest();
}
return Ok();
}
[Route("ship")]
[HttpPut]
[ProducesResponseType((int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
public async Task<IActionResult> ShipOrderAsync([FromBody]ShipOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId)
{
bool commandResult = false;
if (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty)
2017-03-03 12:10:30 +01:00
{
var requestShipOrder = new IdentifiedCommand<ShipOrderCommand, bool>(command, guid);
2019-02-21 15:56:15 +00:00
_logger.LogInformation(
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
requestShipOrder.GetGenericTypeName(),
nameof(requestShipOrder.Command.OrderNumber),
requestShipOrder.Command.OrderNumber,
requestShipOrder);
2017-06-12 13:52:23 +02:00
commandResult = await _mediator.Send(requestShipOrder);
2017-03-03 12:10:30 +01:00
}
if (!commandResult)
{
return BadRequest();
}
return Ok();
}
[Route("{orderId:int}")]
[HttpGet]
[ProducesResponseType(typeof(Order),(int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<ActionResult> GetOrderAsync(int orderId)
{
try
{
2018-11-14 16:21:50 +01:00
var order = await _orderQueries.GetOrderAsync(orderId);
2017-04-17 12:28:12 +02:00
return Ok(order);
}
catch
{
return NotFound();
}
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<OrderSummary>), (int)HttpStatusCode.OK)]
public async Task<ActionResult<IEnumerable<OrderSummary>>> GetOrdersAsync()
{
2018-05-22 16:20:52 +02:00
var userid = _identityService.GetUserIdentity();
var orders = await _orderQueries.GetOrdersFromUserAsync(Guid.Parse(userid));
return Ok(orders);
}
[Route("cardtypes")]
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<CardType>), (int)HttpStatusCode.OK)]
public async Task<ActionResult<IEnumerable<CardType>>> GetCardTypesAsync()
{
var cardTypes = await _orderQueries.GetCardTypesAsync();
return Ok(cardTypes);
}
[Route("draft")]
[HttpPost]
public async Task<ActionResult<OrderDraftDTO>> CreateOrderDraftFromBasketDataAsync([FromBody] CreateOrderDraftCommand createOrderDraftCommand)
{
2019-02-21 15:56:15 +00:00
_logger.LogInformation(
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})",
createOrderDraftCommand.GetGenericTypeName(),
nameof(createOrderDraftCommand.BuyerId),
createOrderDraftCommand.BuyerId,
createOrderDraftCommand);
2018-11-14 16:21:50 +01:00
return await _mediator.Send(createOrderDraftCommand);
}
}
}