Refactoring and Adding Tests (#28)
* Introducing repository and refactoring services. Changing entities to use int keys everywhere. * Refactoring application services to live in web project and only reference repositories, not EF contexts. * Cleaning up implementations * Moving logic out of CatalogController Moving entity knowledge out of viewmodels. * Implementing specification includes better for catalogservice * Cleaning up and adding specification unit tests
This commit is contained in:
@@ -29,23 +29,10 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
//var user = _appUserParser.Parse(HttpContext.User);
|
||||
var basket = await GetBasketFromSessionAsync();
|
||||
var basketModel = await GetBasketFromSessionAsync();
|
||||
|
||||
var viewModel = new BasketViewModel()
|
||||
{
|
||||
BuyerId = basket.BuyerId,
|
||||
Items = basket.Items.Select(i => new BasketItemViewModel()
|
||||
{
|
||||
Id = i.Id,
|
||||
UnitPrice = i.UnitPrice,
|
||||
PictureUrl = _uriComposer.ComposePicUri(i.Item.PictureUri),
|
||||
ProductId = i.Item.Id.ToString(),
|
||||
ProductName = i.Item.Name,
|
||||
Quantity = i.Quantity
|
||||
}).ToList()
|
||||
};
|
||||
|
||||
return View(viewModel);
|
||||
return View(basketModel);
|
||||
}
|
||||
|
||||
// GET: /Cart/AddToCart
|
||||
@@ -58,23 +45,23 @@ namespace Microsoft.eShopWeb.Controllers
|
||||
}
|
||||
var basket = await GetBasketFromSessionAsync();
|
||||
|
||||
await _basketService.AddItemToBasket(basket, productDetails.Id, 1);
|
||||
await _basketService.AddItemToBasket(basket.Id, productDetails.Id, productDetails.Price, 1);
|
||||
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
private async Task<Basket> GetBasketFromSessionAsync()
|
||||
private async Task<BasketViewModel> GetBasketFromSessionAsync()
|
||||
{
|
||||
string basketId = HttpContext.Session.GetString(_basketSessionKey);
|
||||
Basket basket = null;
|
||||
BasketViewModel basket = null;
|
||||
if (basketId == null)
|
||||
{
|
||||
basket = await _basketService.CreateBasketForUser(User.Identity.Name);
|
||||
HttpContext.Session.SetString(_basketSessionKey, basket.Id);
|
||||
HttpContext.Session.SetString(_basketSessionKey, basket.Id.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
basket = await _basketService.GetBasket(basketId);
|
||||
basket = await _basketService.GetBasket(int.Parse(basketId));
|
||||
}
|
||||
return basket;
|
||||
}
|
||||
|
||||
@@ -1,57 +1,21 @@
|
||||
using Microsoft.eShopWeb.Services;
|
||||
using Microsoft.eShopWeb.ViewModels;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ApplicationCore.Interfaces;
|
||||
|
||||
namespace Microsoft.eShopWeb.Controllers
|
||||
{
|
||||
public class CatalogController : Controller
|
||||
{
|
||||
private readonly IHostingEnvironment _env;
|
||||
private readonly ICatalogService _catalogService;
|
||||
private readonly IImageService _imageService;
|
||||
private readonly IAppLogger<CatalogController> _logger;
|
||||
|
||||
public CatalogController(IHostingEnvironment env,
|
||||
ICatalogService catalogService,
|
||||
IImageService imageService,
|
||||
IAppLogger<CatalogController> logger)
|
||||
{
|
||||
_env = env;
|
||||
_catalogService = catalogService;
|
||||
_imageService = imageService;
|
||||
_logger = logger;
|
||||
}
|
||||
public CatalogController(ICatalogService catalogService) => _catalogService = catalogService;
|
||||
|
||||
// GET: /<controller>/
|
||||
public async Task<IActionResult> Index(int? brandFilterApplied, int? typesFilterApplied, int? page)
|
||||
{
|
||||
var itemsPage = 10;
|
||||
var catalog = await _catalogService.GetCatalogItems(page ?? 0, itemsPage, brandFilterApplied, typesFilterApplied);
|
||||
|
||||
var vm = new CatalogIndex()
|
||||
{
|
||||
CatalogItems = catalog.Data,
|
||||
Brands = await _catalogService.GetBrands(),
|
||||
Types = await _catalogService.GetTypes(),
|
||||
BrandFilterApplied = brandFilterApplied ?? 0,
|
||||
TypesFilterApplied = typesFilterApplied ?? 0,
|
||||
PaginationInfo = new PaginationInfo()
|
||||
{
|
||||
ActualPage = page ?? 0,
|
||||
ItemsPerPage = catalog.Data.Count,
|
||||
TotalItems = catalog.Count,
|
||||
TotalPages = int.Parse(Math.Ceiling(((decimal)catalog.Count / itemsPage)).ToString())
|
||||
}
|
||||
};
|
||||
|
||||
vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : "";
|
||||
vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : "";
|
||||
|
||||
return View(vm);
|
||||
var catalogModel = await _catalogService.GetCatalogItems(page ?? 0, itemsPage, brandFilterApplied, typesFilterApplied);
|
||||
return View(catalogModel);
|
||||
}
|
||||
|
||||
public IActionResult Error()
|
||||
|
||||
Reference in New Issue
Block a user