2016-11-21 12:41:36 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
|
|
|
|
|
{
|
2016-12-19 10:20:02 +01:00
|
|
|
|
using Api.Application.Commands;
|
|
|
|
|
using Api.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;
|
2016-12-17 14:41:16 +01:00
|
|
|
|
using System.Collections.Generic;
|
2016-11-21 12:41:36 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
[Route("api/v1/[controller]")]
|
2016-12-21 11:30:11 +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-12-17 14:41:16 +01:00
|
|
|
|
public async Task<IActionResult> AddOrder([FromBody]NewOrderRequest order)
|
2016-11-21 12:41:36 +01:00
|
|
|
|
{
|
2016-12-12 10:15:24 +01:00
|
|
|
|
if (order.CardExpiration == DateTime.MinValue)
|
2016-12-19 17:55:48 +01:00
|
|
|
|
order.CardExpiration = DateTime.Now.AddYears(5);
|
|
|
|
|
|
|
|
|
|
if (order.CardTypeId == 0)
|
|
|
|
|
order.CardTypeId = 1;
|
2016-12-12 10:15:24 +01:00
|
|
|
|
|
2016-12-17 14:41:16 +01:00
|
|
|
|
order.Buyer = GetUserName();
|
2016-11-21 12:41:36 +01:00
|
|
|
|
|
2016-12-17 14:41:16 +01:00
|
|
|
|
var added = await _mediator.SendAsync(order);
|
2016-11-21 12:41:36 +01:00
|
|
|
|
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-12-17 14:41:16 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var order = await _orderQueries.GetOrder(orderId);
|
|
|
|
|
return Ok(order);
|
|
|
|
|
}
|
|
|
|
|
catch (KeyNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|