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:
Steve Smith
2017-08-07 13:25:11 -04:00
committed by GitHub
parent 084db74c77
commit d7eb59c097
41 changed files with 449 additions and 360 deletions

View File

@@ -1,63 +0,0 @@
using ApplicationCore.Interfaces;
using System.Threading.Tasks;
using Microsoft.eShopWeb.ApplicationCore.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using Infrastructure.Data;
namespace Web.Services
{
public class BasketService : IBasketService
{
private readonly CatalogContext _context;
public BasketService(CatalogContext context)
{
_context = context;
}
public async Task<Basket> GetBasket(string basketId)
{
var basket = await _context.Baskets
.Include(b => b.Items)
.ThenInclude(i => i.Item)
.FirstOrDefaultAsync(b => b.Id == basketId);
if (basket == null)
{
basket = new Basket();
_context.Baskets.Add(basket);
await _context.SaveChangesAsync();
}
return basket;
}
public Task<Basket> CreateBasket()
{
return CreateBasketForUser(null);
}
public async Task<Basket> CreateBasketForUser(string userId)
{
var basket = new Basket();
_context.Baskets.Add(basket);
await _context.SaveChangesAsync();
return basket;
}
//public async Task UpdateBasket(Basket basket)
//{
// // only need to save changes here
// await _context.SaveChangesAsync();
//}
public async Task AddItemToBasket(Basket basket, int productId, int quantity)
{
var item = await _context.CatalogItems.FirstOrDefaultAsync(i => i.Id == productId);
basket.AddItem(item, item.Price, quantity);
await _context.SaveChangesAsync();
}
}
}