2017-01-25 17:10:08 +01:00
|
|
|
|
using MediatR;
|
2017-03-15 15:04:13 +01:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
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;
|
2017-05-11 11:51:13 +02:00
|
|
|
|
using Ordering.API.Application.Commands;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-10-09 18:45:59 +01:00
|
|
|
|
using System.Net;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
|
|
|
|
{
|
2016-11-21 12:41:36 +01:00
|
|
|
|
[Route("api/v1/[controller]")]
|
2017-03-14 14:15:34 +01:00
|
|
|
|
[Authorize]
|
2018-05-15 16:58:53 +02:00
|
|
|
|
[ApiController]
|
2018-11-14 16:21:50 +01:00
|
|
|
|
public class OrdersController : ControllerBase
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly IMediator _mediator;
|
|
|
|
|
private readonly IOrderQueries _orderQueries;
|
2016-12-22 13:20:12 +01:00
|
|
|
|
private readonly IIdentityService _identityService;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2016-12-22 13:20:12 +01:00
|
|
|
|
public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService)
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2017-02-27 10:35:31 +01:00
|
|
|
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
|
|
|
|
_orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries));
|
|
|
|
|
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-11 11:51:13 +02:00
|
|
|
|
[Route("cancel")]
|
2017-05-14 14:48:37 +02:00
|
|
|
|
[HttpPut]
|
2017-10-09 18:45:59 +01:00
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<IActionResult> CancelOrderAsync([FromBody]CancelOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId)
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2017-05-03 18:36:52 -07:00
|
|
|
|
bool commandResult = false;
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2017-03-03 12:03:31 +01:00
|
|
|
|
if (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty)
|
|
|
|
|
{
|
2017-05-11 11:51:13 +02:00
|
|
|
|
var requestCancelOrder = new IdentifiedCommand<CancelOrderCommand, bool>(command, guid);
|
2017-06-12 13:52:23 +02:00
|
|
|
|
commandResult = await _mediator.Send(requestCancelOrder);
|
2017-03-03 12:03:31 +01:00
|
|
|
|
}
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
if (!commandResult)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Ok();
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-14 14:48:37 +02:00
|
|
|
|
[Route("ship")]
|
|
|
|
|
[HttpPut]
|
2017-10-09 18:45:59 +01:00
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<IActionResult> ShipOrderAsync([FromBody]ShipOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId)
|
2017-05-14 14:48:37 +02:00
|
|
|
|
{
|
|
|
|
|
bool commandResult = false;
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2017-05-14 14:48:37 +02:00
|
|
|
|
if (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty)
|
2017-03-03 12:10:30 +01:00
|
|
|
|
{
|
2017-05-14 14:48:37 +02:00
|
|
|
|
var requestShipOrder = new IdentifiedCommand<ShipOrderCommand, bool>(command, guid);
|
2017-06-12 13:52:23 +02:00
|
|
|
|
commandResult = await _mediator.Send(requestShipOrder);
|
2017-03-03 12:10:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
if (!commandResult)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2018-11-15 12:50:37 +01:00
|
|
|
|
return Ok();
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
[Route("{orderId:int}")]
|
|
|
|
|
[HttpGet]
|
2017-10-13 17:55:54 +01:00
|
|
|
|
[ProducesResponseType(typeof(Order),(int)HttpStatusCode.OK)]
|
2017-10-09 18:45:59 +01:00
|
|
|
|
[ProducesResponseType((int)HttpStatusCode.NotFound)]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<ActionResult> GetOrderAsync(int orderId)
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2016-12-17 14:41:16 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-11-14 16:21:50 +01:00
|
|
|
|
var order = await _orderQueries.GetOrderAsync(orderId);
|
2017-04-17 12:28:12 +02:00
|
|
|
|
|
2016-12-17 14:41:16 +01:00
|
|
|
|
return Ok(order);
|
|
|
|
|
}
|
2018-11-15 12:50:37 +01:00
|
|
|
|
catch
|
2016-12-17 14:41:16 +01:00
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2017-10-13 17:55:54 +01:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<OrderSummary>), (int)HttpStatusCode.OK)]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<ActionResult<IEnumerable<OrderSummary>>> GetOrdersAsync()
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2018-05-22 16:20:52 +02:00
|
|
|
|
var userid = _identityService.GetUserIdentity();
|
|
|
|
|
var orders = await _orderQueries.GetOrdersFromUserAsync(Guid.Parse(userid));
|
2018-11-15 12:50:37 +01:00
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
return Ok(orders);
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
[Route("cardtypes")]
|
2016-11-21 12:41:36 +01:00
|
|
|
|
[HttpGet]
|
2017-10-13 17:55:54 +01:00
|
|
|
|
[ProducesResponseType(typeof(IEnumerable<CardType>), (int)HttpStatusCode.OK)]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<ActionResult<IEnumerable<CardType>>> GetCardTypesAsync()
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2018-11-15 12:50:37 +01:00
|
|
|
|
var cardTypes = await _orderQueries.GetCardTypesAsync();
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
return Ok(cardTypes);
|
2018-11-15 12:50:37 +01:00
|
|
|
|
}
|
2018-02-02 11:26:47 +00:00
|
|
|
|
|
|
|
|
|
[Route("draft")]
|
|
|
|
|
[HttpPost]
|
2018-11-15 12:50:37 +01:00
|
|
|
|
public async Task<ActionResult<OrderDraftDTO>> CreateOrderDraftFromBasketDataAsync([FromBody] CreateOrderDraftCommand createOrderDraftCommand)
|
2018-02-02 11:26:47 +00:00
|
|
|
|
{
|
2018-11-14 16:21:50 +01:00
|
|
|
|
return await _mediator.Send(createOrderDraftCommand);
|
2018-02-02 11:26:47 +00:00
|
|
|
|
}
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
2018-11-15 12:50:37 +01:00
|
|
|
|
}
|