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;
|
2017-03-03 16:00:15 +01:00
|
|
|
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
|
2016-10-31 17:56:24 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2016-12-17 14:41:16 +01:00
|
|
|
using System.Net.Http;
|
2017-03-17 10:00:18 +01:00
|
|
|
using Polly.CircuitBreaker;
|
2017-05-11 11:51:13 +02:00
|
|
|
using WebMVC.Models;
|
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;
|
2016-11-28 12:58:51 +01:00
|
|
|
private readonly IIdentityParser<ApplicationUser> _appUserParser;
|
|
|
|
public OrderController(IOrderingService orderSvc, IBasketService basketSvc, IIdentityParser<ApplicationUser> appUserParser)
|
2016-10-21 05:46:30 +02:00
|
|
|
{
|
2016-11-28 12:58:51 +01: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
|
|
|
{
|
2016-11-28 12:58:51 +01:00
|
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
2016-11-16 10:19:00 +01:00
|
|
|
var basket = await _basketSvc.GetBasket(user);
|
2016-11-02 20:41:12 +01:00
|
|
|
var order = _basketSvc.MapBasketToOrder(basket);
|
2016-12-17 14:41:16 +01:00
|
|
|
var vm = _orderSvc.MapUserInfoIntoOrder(user, order);
|
|
|
|
vm.CardExpirationShortFormat();
|
2016-11-03 17:17:26 +01:00
|
|
|
|
2016-11-02 20:41:12 +01:00
|
|
|
return View(vm);
|
2016-10-21 05:46:30 +02:00
|
|
|
}
|
|
|
|
|
2016-11-03 17:17:26 +01:00
|
|
|
[HttpPost]
|
2017-05-11 11:51:13 +02:00
|
|
|
public async Task<IActionResult> Checkout(Order model)
|
2016-11-03 17:17:26 +01:00
|
|
|
{
|
2017-03-17 10:00:18 +01:00
|
|
|
try
|
2016-11-16 10:19:00 +01:00
|
|
|
{
|
2017-03-17 10:00:18 +01:00
|
|
|
if (ModelState.IsValid)
|
|
|
|
{
|
|
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
2017-05-11 11:51:13 +02:00
|
|
|
var basket = _orderSvc.MapOrderToBasket(model);
|
|
|
|
|
|
|
|
await _basketSvc.Checkout(basket);
|
2016-11-03 17:17:26 +01:00
|
|
|
|
2017-03-17 10:00:18 +01:00
|
|
|
//Redirect to historic list.
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 11:59:11 +02:00
|
|
|
catch(BrokenCircuitException)
|
2017-03-17 10:00:18 +01:00
|
|
|
{
|
2017-05-06 21:41:32 -07:00
|
|
|
ModelState.AddModelError("Error", "It was not possible to create a new order, please try later on. (Business Msg Due to Circuit-Breaker)");
|
2016-11-16 10:19:00 +01:00
|
|
|
}
|
|
|
|
return View(model);
|
2016-11-03 17:17:26 +01:00
|
|
|
}
|
|
|
|
|
2017-05-11 11:51:13 +02:00
|
|
|
[HttpPut]
|
|
|
|
public async Task<IActionResult> Cancel(Order model)
|
|
|
|
{
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
{
|
|
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
|
|
|
await _orderSvc.CancelOrder(model);
|
|
|
|
|
|
|
|
//Redirect to historic list.
|
|
|
|
return RedirectToAction("Index");
|
|
|
|
}
|
|
|
|
return View(model);
|
|
|
|
}
|
|
|
|
|
2016-12-17 14:41:16 +01:00
|
|
|
public async Task<IActionResult> Detail(string orderId)
|
2016-11-03 17:17:26 +01:00
|
|
|
{
|
2016-11-28 12:58:51 +01:00
|
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
2016-11-03 17:17:26 +01:00
|
|
|
|
2016-12-17 14:41:16 +01:00
|
|
|
var order = await _orderSvc.GetOrder(user, orderId);
|
2016-11-03 17:17:26 +01:00
|
|
|
return View(order);
|
|
|
|
}
|
|
|
|
|
2016-12-12 10:15:24 +01:00
|
|
|
public async Task<IActionResult> Index(Order item)
|
2016-10-21 05:46:30 +02:00
|
|
|
{
|
2016-11-28 12:58:51 +01:00
|
|
|
var user = _appUserParser.Parse(HttpContext.User);
|
2016-12-12 10:15:24 +01:00
|
|
|
var vm = await _orderSvc.GetMyOrders(user);
|
|
|
|
return View(vm);
|
2016-10-21 05:46:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|