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:
59
src/Infrastructure/Data/EfRepository.cs
Normal file
59
src/Infrastructure/Data/EfRepository.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using ApplicationCore.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Infrastructure.Data
|
||||
{
|
||||
public class EfRepository<T> : IRepository<T> where T : BaseEntity
|
||||
{
|
||||
private readonly CatalogContext _dbContext;
|
||||
|
||||
public EfRepository(CatalogContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public T GetById(int id)
|
||||
{
|
||||
return _dbContext.Set<T>().SingleOrDefault(e => e.Id == id);
|
||||
}
|
||||
|
||||
public List<T> List()
|
||||
{
|
||||
return _dbContext.Set<T>().ToList();
|
||||
}
|
||||
|
||||
public List<T> List(ISpecification<T> spec)
|
||||
{
|
||||
var queryableResultWithIncludes = spec.Includes
|
||||
.Aggregate(_dbContext.Set<T>().AsQueryable(),
|
||||
(current, include) => current.Include(include));
|
||||
return queryableResultWithIncludes
|
||||
.Where(spec.Criteria)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public T Add(T entity)
|
||||
{
|
||||
_dbContext.Set<T>().Add(entity);
|
||||
_dbContext.SaveChanges();
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public void Delete(T entity)
|
||||
{
|
||||
_dbContext.Set<T>().Remove(entity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void Update(T entity)
|
||||
{
|
||||
_dbContext.Entry(entity).State = EntityState.Modified;
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -22,5 +22,8 @@
|
||||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.1" />
|
||||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user