2016-11-21 12:41:36 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
|
|
|
|
{
|
|
|
|
|
using Application.Commands;
|
|
|
|
|
using Application.Queries;
|
2016-12-12 10:15:24 +01:00
|
|
|
|
using AspNetCore.Authorization;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2016-11-24 14:58:37 +01:00
|
|
|
|
using Models;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
[Route("api/v1/[controller]")]
|
2016-12-12 10:15:24 +01:00
|
|
|
|
[Authorize]
|
2016-11-21 12:41:36 +01:00
|
|
|
|
public class OrdersController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IMediator _mediator;
|
|
|
|
|
private readonly IOrderQueries _orderQueries;
|
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
public OrdersController(IMediator mediator, IOrderQueries orderQueries)
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
|
|
|
|
if (mediator == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(mediator));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (orderQueries == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(orderQueries));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_mediator = mediator;
|
|
|
|
|
_orderQueries = orderQueries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Route("new")]
|
|
|
|
|
[HttpPost]
|
2016-11-24 14:58:37 +01:00
|
|
|
|
public async Task<IActionResult> AddOrder([FromBody]NewOrderViewModel order)
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2016-12-12 10:15:24 +01:00
|
|
|
|
if (order.CardExpiration == DateTime.MinValue)
|
|
|
|
|
order.CardExpiration = DateTime.Now;
|
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
var newOrderRequest = new NewOrderRequest()
|
|
|
|
|
{
|
2016-12-12 10:15:24 +01:00
|
|
|
|
Buyer = GetUserName(),
|
2016-11-24 14:58:37 +01:00
|
|
|
|
CardTypeId = 1, //TODO
|
|
|
|
|
CardHolderName = order.CardHolderName,
|
|
|
|
|
CardNumber = order.CardNumber,
|
|
|
|
|
CardExpiration = order.CardExpiration,
|
|
|
|
|
CardSecurityNumber = order.CardSecurityNumber,
|
|
|
|
|
State = order.ShippingState,
|
|
|
|
|
City = order.ShippingCity,
|
|
|
|
|
Country = order.ShippingCountry,
|
|
|
|
|
Street = order.ShippingStreet
|
|
|
|
|
};
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
|
|
|
|
var added = await _mediator.SendAsync(newOrderRequest);
|
|
|
|
|
|
|
|
|
|
if (added)
|
|
|
|
|
{
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
[Route("{orderId:int}")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> GetOrder(int orderId)
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2016-11-24 14:58:37 +01:00
|
|
|
|
var order = await _orderQueries.GetOrder(orderId);
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
return Ok(order);
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
[Route("")]
|
2016-11-21 12:41:36 +01:00
|
|
|
|
[HttpGet]
|
2016-11-24 14:58:37 +01:00
|
|
|
|
public async Task<IActionResult> GetOrders()
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2016-11-24 14:58:37 +01:00
|
|
|
|
var orders = await _orderQueries.GetOrders();
|
2016-11-21 12:41:36 +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]
|
2016-11-24 14:58:37 +01:00
|
|
|
|
public async Task<IActionResult> GetCardTypes()
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2016-11-24 14:58:37 +01:00
|
|
|
|
var cardTypes = await _orderQueries.GetCardTypes();
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2016-11-24 14:58:37 +01:00
|
|
|
|
return Ok(cardTypes);
|
|
|
|
|
}
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2016-12-12 10:15:24 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the GUID corresponding to the Id of the authenticated user.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>GUID (string)</returns>
|
2016-11-24 14:58:37 +01:00
|
|
|
|
string GetUserName()
|
|
|
|
|
{
|
2016-12-12 10:15:24 +01:00
|
|
|
|
return HttpContext.User.FindFirst("sub").Value;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|