using MediatR; 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 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] public class OrdersController : Controller { private readonly IMediator _mediator; private readonly IOrderQueries _orderQueries; private readonly IIdentityService _identityService; public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); _orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries)); _identityService = identityService ?? throw new ArgumentNullException(nameof(identityService)); } [Route("cancel")] [HttpPut] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest)] public async Task CancelOrder([FromBody]CancelOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId) { bool commandResult = false; if (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) { var requestCancelOrder = new IdentifiedCommand(command, guid); commandResult = await _mediator.Send(requestCancelOrder); } return commandResult ? (IActionResult)Ok() : (IActionResult)BadRequest(); } [Route("ship")] [HttpPut] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest)] public async Task ShipOrder([FromBody]ShipOrderCommand command, [FromHeader(Name = "x-requestid")] string requestId) { bool commandResult = false; if (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) { var requestShipOrder = new IdentifiedCommand(command, guid); commandResult = await _mediator.Send(requestShipOrder); } return commandResult ? (IActionResult)Ok() : (IActionResult)BadRequest(); } [Route("{orderId:int}")] [HttpGet] [ProducesResponseType(typeof(Order),(int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.NotFound)] public async Task GetOrder(int orderId) { try { var order = await _orderQueries .GetOrderAsync(orderId); return Ok(order); } catch (KeyNotFoundException) { return NotFound(); } } [Route("")] [HttpGet] [ProducesResponseType(typeof(IEnumerable), (int)HttpStatusCode.OK)] public async Task GetOrders() { var userid = _identityService.GetUserIdentity(); var orders = await _orderQueries.GetOrdersFromUserAsync(Guid.Parse(userid)); return Ok(orders); } [Route("cardtypes")] [HttpGet] [ProducesResponseType(typeof(IEnumerable), (int)HttpStatusCode.OK)] public async Task GetCardTypes() { var cardTypes = await _orderQueries .GetCardTypesAsync(); return Ok(cardTypes); } [Route("draft")] [HttpPost] public async Task GetOrderDraftFromBasketData([FromBody] CreateOrderDraftCommand createOrderDraftCommand) { var draft = await _mediator.Send(createOrderDraftCommand); return Ok(draft); } } }