2016-11-21 12:41:36 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
|
|
|
|
{
|
|
|
|
|
using Application.Commands;
|
|
|
|
|
using Application.Queries;
|
|
|
|
|
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]")]
|
|
|
|
|
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-11-24 14:58:37 +01:00
|
|
|
|
var newOrderRequest = new NewOrderRequest()
|
|
|
|
|
{
|
|
|
|
|
Buyer =GetUserName(), //TODO
|
|
|
|
|
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-11-24 14:58:37 +01:00
|
|
|
|
string GetUserName()
|
|
|
|
|
{
|
|
|
|
|
return "MOCK";
|
2016-11-21 12:41:36 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|