74 lines
2.4 KiB
C#
Raw Normal View History

2016-10-21 05:46:30 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopOnContainers.WebMVC.Services;
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
2016-10-31 17:56:24 +01:00
using Microsoft.AspNetCore.Authorization;
using System.Net.Http;
using Polly.CircuitBreaker;
2016-10-21 05:46:30 +02:00
namespace Microsoft.eShopOnContainers.WebMVC.Controllers
{
2016-10-31 17:56:24 +01:00
[Authorize]
2016-10-21 05:46:30 +02:00
public class OrderController : Controller
{
private IOrderingService _orderSvc;
2016-11-02 20:41:12 +01:00
private IBasketService _basketSvc;
private readonly IIdentityParser<ApplicationUser> _appUserParser;
public OrderController(IOrderingService orderSvc, IBasketService basketSvc, IIdentityParser<ApplicationUser> appUserParser)
2016-10-21 05:46:30 +02:00
{
_appUserParser = appUserParser;
2016-10-21 05:46:30 +02:00
_orderSvc = orderSvc;
2016-11-02 20:41:12 +01:00
_basketSvc = basketSvc;
2016-10-21 05:46:30 +02:00
}
2016-11-02 20:41:12 +01:00
public async Task<IActionResult> Create()
2016-10-31 09:25:47 +01:00
{
var user = _appUserParser.Parse(HttpContext.User);
var basket = await _basketSvc.GetBasket(user);
2016-11-02 20:41:12 +01:00
var order = _basketSvc.MapBasketToOrder(basket);
var vm = _orderSvc.MapUserInfoIntoOrder(user, order);
vm.CardExpirationShortFormat();
2016-11-02 20:41:12 +01:00
return View(vm);
2016-10-21 05:46:30 +02:00
}
[HttpPost]
public async Task<IActionResult> Create(Order model, string action)
{
try
{
if (ModelState.IsValid)
{
var user = _appUserParser.Parse(HttpContext.User);
await _orderSvc.CreateOrder(model);
//Redirect to historic list.
return RedirectToAction("Index");
}
}
2017-04-06 11:59:11 +02:00
catch(BrokenCircuitException)
{
ModelState.AddModelError("Error", "It was not possible to create a new order, please try later on");
}
return View(model);
}
public async Task<IActionResult> Detail(string orderId)
{
var user = _appUserParser.Parse(HttpContext.User);
var order = await _orderSvc.GetOrder(user, orderId);
return View(order);
}
public async Task<IActionResult> Index(Order item)
2016-10-21 05:46:30 +02:00
{
var user = _appUserParser.Parse(HttpContext.User);
var vm = await _orderSvc.GetMyOrders(user);
return View(vm);
2016-10-21 05:46:30 +02:00
}
}
}