61 lines
2.1 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.Models;
2016-10-31 17:56:24 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
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-10-31 17:56:24 +01:00
private ICatalogService _catalogSvc;
private readonly UserManager<ApplicationUser> _userManager;
public OrderController(IOrderingService orderSvc, ICatalogService catalogSvc, UserManager<ApplicationUser> userManager)
2016-10-21 05:46:30 +02:00
{
2016-10-31 17:56:24 +01:00
_userManager = userManager;
2016-10-21 05:46:30 +02:00
_orderSvc = orderSvc;
2016-10-31 17:56:24 +01:00
_catalogSvc = catalogSvc;
2016-10-21 05:46:30 +02:00
}
2016-10-31 17:56:24 +01:00
public async Task<IActionResult> AddToCart(string productId)
2016-10-31 09:25:47 +01:00
{
2016-10-31 17:56:24 +01:00
//CCE: I need product details (price, ...), so I retrieve from Catalog service again.
// I don't like POST with a form from the catalog view (I'm avoiding Ajax too),
// I prefer Url.Action and http call here to catalog service to retrieve this info.
var user = await _userManager.GetUserAsync(HttpContext.User);
var productDetails = _catalogSvc.GetCatalogItem(productId);
var product = new OrderItem()
{
ProductId = productId,
Quantity = 1,
ProductName = productDetails.Name,
PicsUrl = productDetails.PicsUrl,
UnitPrice = productDetails.Price
};
_orderSvc.AddToCart(user, product);
return RedirectToAction("Index", "Catalog");
2016-10-31 09:25:47 +01:00
}
2016-10-21 05:46:30 +02:00
public IActionResult Cart()
{
return View();
}
public IActionResult Create()
{
return View();
}
2016-10-31 17:56:24 +01:00
public async Task<IActionResult> Index(Order item)
2016-10-21 05:46:30 +02:00
{
2016-10-31 17:56:24 +01:00
var user = await _userManager.GetUserAsync(HttpContext.User);
return View(_orderSvc.GetMyOrders(user));
2016-10-21 05:46:30 +02:00
}
}
}