78 lines
2.3 KiB
C#
Raw Normal View History

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 System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
{
[Route("api/v1/[controller]")]
//[Authorize]
public class OrdersController : Controller
{
private readonly IMediator _mediator;
private readonly IOrderQueries _orderQueries;
2016-12-22 13:20:12 +01:00
private readonly IIdentityService _identityService;
2016-12-22 13:20:12 +01:00
public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService)
{
2016-12-22 13:20:12 +01:00
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
_orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries));
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
}
[Route("new")]
[HttpPost]
2017-02-13 14:03:21 -08:00
public async Task<IActionResult> CreateOrder([FromBody]CreateOrderCommand createOrderCommand)
{
2017-02-13 14:03:21 -08:00
var result = await _mediator.SendAsync(createOrderCommand);
if (result)
{
return Ok();
}
return BadRequest();
}
[Route("{orderId:int}")]
[HttpGet]
public async Task<IActionResult> GetOrder(int orderId)
{
try
{
var order = await _orderQueries.GetOrder(orderId);
return Ok(order);
}
catch (KeyNotFoundException)
{
return NotFound();
}
}
[Route("")]
[HttpGet]
public async Task<IActionResult> GetOrders()
{
var orders = await _orderQueries.GetOrders();
return Ok(orders);
}
[Route("cardtypes")]
[HttpGet]
public async Task<IActionResult> GetCardTypes()
{
var cardTypes = await _orderQueries.GetCardTypes();
return Ok(cardTypes);
}
}
}