Update Specification and other packages to latest version (#582)
* Updating repositories and specification version Need to fix broken tests * removing test that would just be testing mocked result now * Refactored from IAsyncRepository and removed it. Tests pass. * Update packages
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Ardalis.GuardClauses" Version="3.0.1" />
|
<PackageReference Include="Ardalis.GuardClauses" Version="3.0.1" />
|
||||||
<PackageReference Include="Ardalis.Specification" Version="4.1.0" />
|
<PackageReference Include="Ardalis.Specification" Version="5.2.0" />
|
||||||
<PackageReference Include="MediatR" Version="9.0.0" />
|
<PackageReference Include="MediatR" Version="9.0.0" />
|
||||||
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
|
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Text.Json" Version="5.0.0" />
|
<PackageReference Include="System.Text.Json" Version="5.0.0" />
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
using Ardalis.Specification;
|
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
|
||||||
{
|
|
||||||
public interface IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
|
|
||||||
{
|
|
||||||
Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default);
|
|
||||||
Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default);
|
|
||||||
Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
|
|
||||||
Task<T> AddAsync(T entity, CancellationToken cancellationToken = default);
|
|
||||||
Task UpdateAsync(T entity, CancellationToken cancellationToken = default);
|
|
||||||
Task DeleteAsync(T entity, CancellationToken cancellationToken = default);
|
|
||||||
Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
|
|
||||||
Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
|
|
||||||
Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,8 +4,8 @@ using System.Threading.Tasks;
|
|||||||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
||||||
{
|
{
|
||||||
|
|
||||||
public interface IOrderRepository : IAsyncRepository<Order>
|
//public interface IOrderRepository : IAsyncRepository<Order>
|
||||||
{
|
//{
|
||||||
Task<Order> GetByIdWithItemsAsync(int id);
|
// Task<Order> GetByIdWithItemsAsync(int id);
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|||||||
8
src/ApplicationCore/Interfaces/IReadRepository.cs
Normal file
8
src/ApplicationCore/Interfaces/IReadRepository.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using Ardalis.Specification;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
||||||
|
{
|
||||||
|
public interface IReadRepository<T> : IReadRepositoryBase<T> where T : class, IAggregateRoot
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
8
src/ApplicationCore/Interfaces/IRepository.cs
Normal file
8
src/ApplicationCore/Interfaces/IRepository.cs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
using Ardalis.Specification;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces
|
||||||
|
{
|
||||||
|
public interface IRepository<T> : IRepositoryBase<T> where T : class, IAggregateRoot
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,10 +9,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
{
|
{
|
||||||
public class BasketService : IBasketService
|
public class BasketService : IBasketService
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<Basket> _basketRepository;
|
private readonly IRepository<Basket> _basketRepository;
|
||||||
private readonly IAppLogger<BasketService> _logger;
|
private readonly IAppLogger<BasketService> _logger;
|
||||||
|
|
||||||
public BasketService(IAsyncRepository<Basket> basketRepository,
|
public BasketService(IRepository<Basket> basketRepository,
|
||||||
IAppLogger<BasketService> logger)
|
IAppLogger<BasketService> logger)
|
||||||
{
|
{
|
||||||
_basketRepository = basketRepository;
|
_basketRepository = basketRepository;
|
||||||
@@ -22,7 +22,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
|
public async Task AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
|
||||||
{
|
{
|
||||||
var basketSpec = new BasketWithItemsSpecification(basketId);
|
var basketSpec = new BasketWithItemsSpecification(basketId);
|
||||||
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
|
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
|
||||||
Guard.Against.NullBasket(basketId, basket);
|
Guard.Against.NullBasket(basketId, basket);
|
||||||
|
|
||||||
basket.AddItem(catalogItemId, price, quantity);
|
basket.AddItem(catalogItemId, price, quantity);
|
||||||
@@ -40,7 +40,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
{
|
{
|
||||||
Guard.Against.Null(quantities, nameof(quantities));
|
Guard.Against.Null(quantities, nameof(quantities));
|
||||||
var basketSpec = new BasketWithItemsSpecification(basketId);
|
var basketSpec = new BasketWithItemsSpecification(basketId);
|
||||||
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
|
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
|
||||||
Guard.Against.NullBasket(basketId, basket);
|
Guard.Against.NullBasket(basketId, basket);
|
||||||
|
|
||||||
foreach (var item in basket.Items)
|
foreach (var item in basket.Items)
|
||||||
@@ -60,10 +60,10 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
|
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
|
||||||
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
||||||
var anonymousBasketSpec = new BasketWithItemsSpecification(anonymousId);
|
var anonymousBasketSpec = new BasketWithItemsSpecification(anonymousId);
|
||||||
var anonymousBasket = await _basketRepository.FirstOrDefaultAsync(anonymousBasketSpec);
|
var anonymousBasket = await _basketRepository.GetBySpecAsync(anonymousBasketSpec);
|
||||||
if (anonymousBasket == null) return;
|
if (anonymousBasket == null) return;
|
||||||
var userBasketSpec = new BasketWithItemsSpecification(userName);
|
var userBasketSpec = new BasketWithItemsSpecification(userName);
|
||||||
var userBasket = await _basketRepository.FirstOrDefaultAsync(userBasketSpec);
|
var userBasket = await _basketRepository.GetBySpecAsync(userBasketSpec);
|
||||||
if (userBasket == null)
|
if (userBasket == null)
|
||||||
{
|
{
|
||||||
userBasket = new Basket(userName);
|
userBasket = new Basket(userName);
|
||||||
|
|||||||
@@ -11,14 +11,14 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
{
|
{
|
||||||
public class OrderService : IOrderService
|
public class OrderService : IOrderService
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<Order> _orderRepository;
|
private readonly IRepository<Order> _orderRepository;
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
private readonly IAsyncRepository<Basket> _basketRepository;
|
private readonly IRepository<Basket> _basketRepository;
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
|
|
||||||
public OrderService(IAsyncRepository<Basket> basketRepository,
|
public OrderService(IRepository<Basket> basketRepository,
|
||||||
IAsyncRepository<CatalogItem> itemRepository,
|
IRepository<CatalogItem> itemRepository,
|
||||||
IAsyncRepository<Order> orderRepository,
|
IRepository<Order> orderRepository,
|
||||||
IUriComposer uriComposer)
|
IUriComposer uriComposer)
|
||||||
{
|
{
|
||||||
_orderRepository = orderRepository;
|
_orderRepository = orderRepository;
|
||||||
@@ -30,7 +30,7 @@ namespace Microsoft.eShopWeb.ApplicationCore.Services
|
|||||||
public async Task CreateOrderAsync(int basketId, Address shippingAddress)
|
public async Task CreateOrderAsync(int basketId, Address shippingAddress)
|
||||||
{
|
{
|
||||||
var basketSpec = new BasketWithItemsSpecification(basketId);
|
var basketSpec = new BasketWithItemsSpecification(basketId);
|
||||||
var basket = await _basketRepository.FirstOrDefaultAsync(basketSpec);
|
var basket = await _basketRepository.GetBySpecAsync(basketSpec);
|
||||||
|
|
||||||
Guard.Against.NullBasket(basketId, basket);
|
Guard.Against.NullBasket(basketId, basket);
|
||||||
Guard.Against.EmptyBasketOnCheckout(basket.Items);
|
Guard.Against.EmptyBasketOnCheckout(basket.Items);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
|||||||
|
|
||||||
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
||||||
{
|
{
|
||||||
public sealed class BasketWithItemsSpecification : Specification<Basket>
|
public sealed class BasketWithItemsSpecification : Specification<Basket>, ISingleResultSpecification
|
||||||
{
|
{
|
||||||
public BasketWithItemsSpecification(int basketId)
|
public BasketWithItemsSpecification(int basketId)
|
||||||
{
|
{
|
||||||
|
|||||||
16
src/ApplicationCore/Specifications/OrderWithItemsByIdSpec.cs
Normal file
16
src/ApplicationCore/Specifications/OrderWithItemsByIdSpec.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
using Ardalis.Specification;
|
||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopWeb.ApplicationCore.Specifications
|
||||||
|
{
|
||||||
|
public class OrderWithItemsByIdSpec : Specification<Order>, ISingleResultSpecification
|
||||||
|
{
|
||||||
|
public OrderWithItemsByIdSpec(int orderId)
|
||||||
|
{
|
||||||
|
Query
|
||||||
|
.Where(order => order.Id == orderId)
|
||||||
|
.Include(o => o.OrderItems)
|
||||||
|
.ThenInclude(i => i.ItemOrdered);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,6 @@ namespace BlazorAdmin.Services
|
|||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
private readonly string _apiUrl;
|
private readonly string _apiUrl;
|
||||||
|
|
||||||
|
|
||||||
public HttpService(HttpClient httpClient, BaseUrlConfiguration baseUrlConfiguration)
|
public HttpService(HttpClient httpClient, BaseUrlConfiguration baseUrlConfiguration)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_httpClient = httpClient;
|
||||||
|
|||||||
@@ -10,79 +10,85 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Microsoft.eShopWeb.Infrastructure.Data
|
namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||||
{
|
{
|
||||||
|
public class EfRepository<T> : RepositoryBase<T>, IReadRepository<T>, IRepository<T> where T : class, IAggregateRoot
|
||||||
|
{
|
||||||
|
public EfRepository(CatalogContext dbContext) : base(dbContext)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// "There's some repetition here - couldn't we have some the sync methods call the async?"
|
/// "There's some repetition here - couldn't we have some the sync methods call the async?"
|
||||||
/// https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/
|
/// https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
|
// public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity, IAggregateRoot
|
||||||
{
|
// {
|
||||||
protected readonly CatalogContext _dbContext;
|
// protected readonly CatalogContext _dbContext;
|
||||||
|
|
||||||
public EfRepository(CatalogContext dbContext)
|
// public EfRepository(CatalogContext dbContext)
|
||||||
{
|
// {
|
||||||
_dbContext = dbContext;
|
// _dbContext = dbContext;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
// public virtual async Task<T> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
var keyValues = new object[] { id };
|
// var keyValues = new object[] { id };
|
||||||
return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken);
|
// return await _dbContext.Set<T>().FindAsync(keyValues, cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default)
|
// public async Task<IReadOnlyList<T>> ListAllAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
return await _dbContext.Set<T>().ToListAsync(cancellationToken);
|
// return await _dbContext.Set<T>().ToListAsync(cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
// public async Task<IReadOnlyList<T>> ListAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
var specificationResult = ApplySpecification(spec);
|
// var specificationResult = ApplySpecification(spec);
|
||||||
return await specificationResult.ToListAsync(cancellationToken);
|
// return await specificationResult.ToListAsync(cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
// public async Task<int> CountAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
var specificationResult = ApplySpecification(spec);
|
// var specificationResult = ApplySpecification(spec);
|
||||||
return await specificationResult.CountAsync(cancellationToken);
|
// return await specificationResult.CountAsync(cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
|
// public async Task<T> AddAsync(T entity, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
await _dbContext.Set<T>().AddAsync(entity, cancellationToken);
|
// await _dbContext.Set<T>().AddAsync(entity, cancellationToken);
|
||||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
// await _dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
return entity;
|
// return entity;
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
|
// public async Task UpdateAsync(T entity, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
_dbContext.Entry(entity).State = EntityState.Modified;
|
// _dbContext.Entry(entity).State = EntityState.Modified;
|
||||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
// await _dbContext.SaveChangesAsync(cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
|
// public async Task DeleteAsync(T entity, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
_dbContext.Set<T>().Remove(entity);
|
// _dbContext.Set<T>().Remove(entity);
|
||||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
// await _dbContext.SaveChangesAsync(cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
// public async Task<T> FirstAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
var specificationResult = ApplySpecification(spec);
|
// var specificationResult = ApplySpecification(spec);
|
||||||
return await specificationResult.FirstAsync(cancellationToken);
|
// return await specificationResult.FirstAsync(cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
// public async Task<T> FirstOrDefaultAsync(ISpecification<T> spec, CancellationToken cancellationToken = default)
|
||||||
{
|
// {
|
||||||
var specificationResult = ApplySpecification(spec);
|
// var specificationResult = ApplySpecification(spec);
|
||||||
return await specificationResult.FirstOrDefaultAsync(cancellationToken);
|
// return await specificationResult.FirstOrDefaultAsync(cancellationToken);
|
||||||
}
|
// }
|
||||||
|
|
||||||
private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
// private IQueryable<T> ApplySpecification(ISpecification<T> spec)
|
||||||
{
|
// {
|
||||||
var evaluator = new SpecificationEvaluator<T>();
|
// var evaluator = new SpecificationEvaluator<T>();
|
||||||
return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
// return evaluator.GetQuery(_dbContext.Set<T>().AsQueryable(), spec);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
@@ -5,18 +5,18 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace Microsoft.eShopWeb.Infrastructure.Data
|
namespace Microsoft.eShopWeb.Infrastructure.Data
|
||||||
{
|
{
|
||||||
public class OrderRepository : EfRepository<Order>, IOrderRepository
|
//public class OrderRepository : EfRepository<Order>, IOrderRepository
|
||||||
{
|
//{
|
||||||
public OrderRepository(CatalogContext dbContext) : base(dbContext)
|
// public OrderRepository(CatalogContext dbContext) : base(dbContext)
|
||||||
{
|
// {
|
||||||
}
|
// }
|
||||||
|
|
||||||
public Task<Order> GetByIdWithItemsAsync(int id)
|
// public Task<Order> GetByIdWithItemsAsync(int id)
|
||||||
{
|
// {
|
||||||
return _dbContext.Orders
|
// return _dbContext.Orders
|
||||||
.Include(o => o.OrderItems)
|
// .Include(o => o.OrderItems)
|
||||||
.ThenInclude(i => i.ItemOrdered)
|
// .ThenInclude(i => i.ItemOrdered)
|
||||||
.FirstOrDefaultAsync(x => x.Id == id);
|
// .FirstOrDefaultAsync(x => x.Id == id);
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="4.1.0" />
|
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="5.2.0" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.10" />
|
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.10" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.11" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.11" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints
|
|||||||
.WithoutRequest
|
.WithoutRequest
|
||||||
.WithResponse<ListCatalogBrandsResponse>
|
.WithResponse<ListCatalogBrandsResponse>
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogBrand> _catalogBrandRepository;
|
private readonly IRepository<CatalogBrand> _catalogBrandRepository;
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
public List(IAsyncRepository<CatalogBrand> catalogBrandRepository,
|
public List(IRepository<CatalogBrand> catalogBrandRepository,
|
||||||
IMapper mapper)
|
IMapper mapper)
|
||||||
{
|
{
|
||||||
_catalogBrandRepository = catalogBrandRepository;
|
_catalogBrandRepository = catalogBrandRepository;
|
||||||
@@ -35,7 +35,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogBrandEndpoints
|
|||||||
{
|
{
|
||||||
var response = new ListCatalogBrandsResponse();
|
var response = new ListCatalogBrandsResponse();
|
||||||
|
|
||||||
var items = await _catalogBrandRepository.ListAllAsync(cancellationToken);
|
var items = await _catalogBrandRepository.ListAsync(cancellationToken);
|
||||||
|
|
||||||
response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>));
|
response.CatalogBrands.AddRange(items.Select(_mapper.Map<CatalogBrandDto>));
|
||||||
|
|
||||||
|
|||||||
@@ -17,10 +17,11 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
|||||||
.WithRequest<CreateCatalogItemRequest>
|
.WithRequest<CreateCatalogItemRequest>
|
||||||
.WithResponse<CreateCatalogItemResponse>
|
.WithResponse<CreateCatalogItemResponse>
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
|
|
||||||
public Create(IAsyncRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
|
public Create(IRepository<CatalogItem> itemRepository,
|
||||||
|
IUriComposer uriComposer)
|
||||||
{
|
{
|
||||||
_itemRepository = itemRepository;
|
_itemRepository = itemRepository;
|
||||||
_uriComposer = uriComposer;
|
_uriComposer = uriComposer;
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
|||||||
.WithRequest<DeleteCatalogItemRequest>
|
.WithRequest<DeleteCatalogItemRequest>
|
||||||
.WithResponse<DeleteCatalogItemResponse>
|
.WithResponse<DeleteCatalogItemResponse>
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
|
|
||||||
public Delete(IAsyncRepository<CatalogItem> itemRepository)
|
public Delete(IRepository<CatalogItem> itemRepository)
|
||||||
{
|
{
|
||||||
_itemRepository = itemRepository;
|
_itemRepository = itemRepository;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,10 +12,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
|||||||
.WithRequest<GetByIdCatalogItemRequest>
|
.WithRequest<GetByIdCatalogItemRequest>
|
||||||
.WithResponse<GetByIdCatalogItemResponse>
|
.WithResponse<GetByIdCatalogItemResponse>
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
|
|
||||||
public GetById(IAsyncRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
|
public GetById(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
|
||||||
{
|
{
|
||||||
_itemRepository = itemRepository;
|
_itemRepository = itemRepository;
|
||||||
_uriComposer = uriComposer;
|
_uriComposer = uriComposer;
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
|||||||
.WithRequest<ListPagedCatalogItemRequest>
|
.WithRequest<ListPagedCatalogItemRequest>
|
||||||
.WithResponse<ListPagedCatalogItemResponse>
|
.WithResponse<ListPagedCatalogItemResponse>
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
public ListPaged(IAsyncRepository<CatalogItem> itemRepository,
|
public ListPaged(IRepository<CatalogItem> itemRepository,
|
||||||
IUriComposer uriComposer,
|
IUriComposer uriComposer,
|
||||||
IMapper mapper)
|
IMapper mapper)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogItemEndpoints
|
|||||||
.WithRequest<UpdateCatalogItemRequest>
|
.WithRequest<UpdateCatalogItemRequest>
|
||||||
.WithResponse<UpdateCatalogItemResponse>
|
.WithResponse<UpdateCatalogItemResponse>
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
|
|
||||||
public Update(IAsyncRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
|
public Update(IRepository<CatalogItem> itemRepository, IUriComposer uriComposer)
|
||||||
{
|
{
|
||||||
_itemRepository = itemRepository;
|
_itemRepository = itemRepository;
|
||||||
_uriComposer = uriComposer;
|
_uriComposer = uriComposer;
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints
|
|||||||
.WithoutRequest
|
.WithoutRequest
|
||||||
.WithResponse<ListCatalogTypesResponse>
|
.WithResponse<ListCatalogTypesResponse>
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogType> _catalogTypeRepository;
|
private readonly IRepository<CatalogType> _catalogTypeRepository;
|
||||||
private readonly IMapper _mapper;
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
public List(IAsyncRepository<CatalogType> catalogTypeRepository,
|
public List(IRepository<CatalogType> catalogTypeRepository,
|
||||||
IMapper mapper)
|
IMapper mapper)
|
||||||
{
|
{
|
||||||
_catalogTypeRepository = catalogTypeRepository;
|
_catalogTypeRepository = catalogTypeRepository;
|
||||||
@@ -35,7 +35,7 @@ namespace Microsoft.eShopWeb.PublicApi.CatalogTypeEndpoints
|
|||||||
{
|
{
|
||||||
var response = new ListCatalogTypesResponse();
|
var response = new ListCatalogTypesResponse();
|
||||||
|
|
||||||
var items = await _catalogTypeRepository.ListAllAsync(cancellationToken);
|
var items = await _catalogTypeRepository.ListAsync(cancellationToken);
|
||||||
|
|
||||||
response.CatalogTypes.AddRange(items.Select(_mapper.Map<CatalogTypeDto>));
|
response.CatalogTypes.AddRange(items.Select(_mapper.Map<CatalogTypeDto>));
|
||||||
|
|
||||||
|
|||||||
@@ -13,8 +13,8 @@
|
|||||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
|
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
|
||||||
<PackageReference Include="MediatR" Version="9.0.0" />
|
<PackageReference Include="MediatR" Version="9.0.0" />
|
||||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
|
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.2" />
|
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.2.3" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.6.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.6.3" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.11" />
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.11" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.10" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.10" />
|
||||||
|
|||||||
@@ -88,7 +88,8 @@ namespace Microsoft.eShopWeb.PublicApi
|
|||||||
.AddEntityFrameworkStores<AppIdentityDbContext>()
|
.AddEntityFrameworkStores<AppIdentityDbContext>()
|
||||||
.AddDefaultTokenProviders();
|
.AddDefaultTokenProviders();
|
||||||
|
|
||||||
services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>));
|
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
|
||||||
|
services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>));
|
||||||
services.Configure<CatalogSettings>(Configuration);
|
services.Configure<CatalogSettings>(Configuration);
|
||||||
services.AddSingleton<IUriComposer>(new UriComposer(Configuration.Get<CatalogSettings>()));
|
services.AddSingleton<IUriComposer>(new UriComposer(Configuration.Get<CatalogSettings>()));
|
||||||
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
|
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
|
||||||
|
|||||||
@@ -10,12 +10,16 @@ namespace Microsoft.eShopWeb.Web.Configuration
|
|||||||
{
|
{
|
||||||
public static class ConfigureCoreServices
|
public static class ConfigureCoreServices
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddCoreServices(this IServiceCollection services, IConfiguration configuration)
|
public static IServiceCollection AddCoreServices(this IServiceCollection services,
|
||||||
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>));
|
//services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>));
|
||||||
|
services.AddScoped(typeof(IReadRepository<>), typeof(EfRepository<>));
|
||||||
|
services.AddScoped(typeof(IRepository<>), typeof(EfRepository<>));
|
||||||
|
|
||||||
services.AddScoped<IBasketService, BasketService>();
|
services.AddScoped<IBasketService, BasketService>();
|
||||||
services.AddScoped<IOrderService, OrderService>();
|
services.AddScoped<IOrderService, OrderService>();
|
||||||
services.AddScoped<IOrderRepository, OrderRepository>();
|
//services.AddScoped<IOrderRepository, OrderRepository>();
|
||||||
services.AddSingleton<IUriComposer>(new UriComposer(configuration.Get<CatalogSettings>()));
|
services.AddSingleton<IUriComposer>(new UriComposer(configuration.Get<CatalogSettings>()));
|
||||||
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
|
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
|
||||||
services.AddTransient<IEmailSender, EmailSender>();
|
services.AddTransient<IEmailSender, EmailSender>();
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||||
using Microsoft.eShopWeb.Web.ViewModels;
|
using Microsoft.eShopWeb.Web.ViewModels;
|
||||||
@@ -11,14 +12,15 @@ namespace Microsoft.eShopWeb.Web.Features.MyOrders
|
|||||||
{
|
{
|
||||||
public class GetMyOrdersHandler : IRequestHandler<GetMyOrders, IEnumerable<OrderViewModel>>
|
public class GetMyOrdersHandler : IRequestHandler<GetMyOrders, IEnumerable<OrderViewModel>>
|
||||||
{
|
{
|
||||||
private readonly IOrderRepository _orderRepository;
|
private readonly IReadRepository<Order> _orderRepository;
|
||||||
|
|
||||||
public GetMyOrdersHandler(IOrderRepository orderRepository)
|
public GetMyOrdersHandler(IReadRepository<Order> orderRepository)
|
||||||
{
|
{
|
||||||
_orderRepository = orderRepository;
|
_orderRepository = orderRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<OrderViewModel>> Handle(GetMyOrders request, CancellationToken cancellationToken)
|
public async Task<IEnumerable<OrderViewModel>> Handle(GetMyOrders request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var specification = new CustomerOrdersWithItemsSpecification(request.UserName);
|
var specification = new CustomerOrdersWithItemsSpecification(request.UserName);
|
||||||
var orders = await _orderRepository.ListAsync(specification, cancellationToken);
|
var orders = await _orderRepository.ListAsync(specification, cancellationToken);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||||
using Microsoft.eShopWeb.Web.ViewModels;
|
using Microsoft.eShopWeb.Web.ViewModels;
|
||||||
@@ -10,17 +11,18 @@ namespace Microsoft.eShopWeb.Web.Features.OrderDetails
|
|||||||
{
|
{
|
||||||
public class GetOrderDetailsHandler : IRequestHandler<GetOrderDetails, OrderViewModel>
|
public class GetOrderDetailsHandler : IRequestHandler<GetOrderDetails, OrderViewModel>
|
||||||
{
|
{
|
||||||
private readonly IOrderRepository _orderRepository;
|
private readonly IReadRepository<Order> _orderRepository;
|
||||||
|
|
||||||
public GetOrderDetailsHandler(IOrderRepository orderRepository)
|
public GetOrderDetailsHandler(IReadRepository<Order> orderRepository)
|
||||||
{
|
{
|
||||||
_orderRepository = orderRepository;
|
_orderRepository = orderRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<OrderViewModel> Handle(GetOrderDetails request, CancellationToken cancellationToken)
|
public async Task<OrderViewModel> Handle(GetOrderDetails request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var customerOrders = await _orderRepository.ListAsync(new CustomerOrdersWithItemsSpecification(request.UserName), cancellationToken);
|
var spec = new OrderWithItemsByIdSpec(request.OrderId);
|
||||||
var order = customerOrders.FirstOrDefault(o => o.Id == request.OrderId);
|
var order = await _orderRepository.GetBySpecAsync(spec, cancellationToken);
|
||||||
|
|
||||||
if (order == null)
|
if (order == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ namespace Microsoft.eShopWeb.Web.Services
|
|||||||
{
|
{
|
||||||
public class BasketViewModelService : IBasketViewModelService
|
public class BasketViewModelService : IBasketViewModelService
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<Basket> _basketRepository;
|
private readonly IRepository<Basket> _basketRepository;
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
|
|
||||||
public BasketViewModelService(IAsyncRepository<Basket> basketRepository,
|
public BasketViewModelService(IRepository<Basket> basketRepository,
|
||||||
IAsyncRepository<CatalogItem> itemRepository,
|
IRepository<CatalogItem> itemRepository,
|
||||||
IUriComposer uriComposer)
|
IUriComposer uriComposer)
|
||||||
{
|
{
|
||||||
_basketRepository = basketRepository;
|
_basketRepository = basketRepository;
|
||||||
@@ -28,7 +28,7 @@ namespace Microsoft.eShopWeb.Web.Services
|
|||||||
public async Task<BasketViewModel> GetOrCreateBasketForUser(string userName)
|
public async Task<BasketViewModel> GetOrCreateBasketForUser(string userName)
|
||||||
{
|
{
|
||||||
var basketSpec = new BasketWithItemsSpecification(userName);
|
var basketSpec = new BasketWithItemsSpecification(userName);
|
||||||
var basket = (await _basketRepository.FirstOrDefaultAsync(basketSpec));
|
var basket = (await _basketRepository.GetBySpecAsync(basketSpec));
|
||||||
|
|
||||||
if (basket == null)
|
if (basket == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ namespace Microsoft.eShopWeb.Web.Services
|
|||||||
{
|
{
|
||||||
public class CatalogItemViewModelService : ICatalogItemViewModelService
|
public class CatalogItemViewModelService : ICatalogItemViewModelService
|
||||||
{
|
{
|
||||||
private readonly IAsyncRepository<CatalogItem> _catalogItemRepository;
|
private readonly IRepository<CatalogItem> _catalogItemRepository;
|
||||||
|
|
||||||
public CatalogItemViewModelService(IAsyncRepository<CatalogItem> catalogItemRepository)
|
public CatalogItemViewModelService(IRepository<CatalogItem> catalogItemRepository)
|
||||||
{
|
{
|
||||||
_catalogItemRepository = catalogItemRepository;
|
_catalogItemRepository = catalogItemRepository;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,16 +18,16 @@ namespace Microsoft.eShopWeb.Web.Services
|
|||||||
public class CatalogViewModelService : ICatalogViewModelService
|
public class CatalogViewModelService : ICatalogViewModelService
|
||||||
{
|
{
|
||||||
private readonly ILogger<CatalogViewModelService> _logger;
|
private readonly ILogger<CatalogViewModelService> _logger;
|
||||||
private readonly IAsyncRepository<CatalogItem> _itemRepository;
|
private readonly IRepository<CatalogItem> _itemRepository;
|
||||||
private readonly IAsyncRepository<CatalogBrand> _brandRepository;
|
private readonly IRepository<CatalogBrand> _brandRepository;
|
||||||
private readonly IAsyncRepository<CatalogType> _typeRepository;
|
private readonly IRepository<CatalogType> _typeRepository;
|
||||||
private readonly IUriComposer _uriComposer;
|
private readonly IUriComposer _uriComposer;
|
||||||
|
|
||||||
public CatalogViewModelService(
|
public CatalogViewModelService(
|
||||||
ILoggerFactory loggerFactory,
|
ILoggerFactory loggerFactory,
|
||||||
IAsyncRepository<CatalogItem> itemRepository,
|
IRepository<CatalogItem> itemRepository,
|
||||||
IAsyncRepository<CatalogBrand> brandRepository,
|
IRepository<CatalogBrand> brandRepository,
|
||||||
IAsyncRepository<CatalogType> typeRepository,
|
IRepository<CatalogType> typeRepository,
|
||||||
IUriComposer uriComposer)
|
IUriComposer uriComposer)
|
||||||
{
|
{
|
||||||
_logger = loggerFactory.CreateLogger<CatalogViewModelService>();
|
_logger = loggerFactory.CreateLogger<CatalogViewModelService>();
|
||||||
@@ -80,7 +80,7 @@ namespace Microsoft.eShopWeb.Web.Services
|
|||||||
public async Task<IEnumerable<SelectListItem>> GetBrands()
|
public async Task<IEnumerable<SelectListItem>> GetBrands()
|
||||||
{
|
{
|
||||||
_logger.LogInformation("GetBrands called.");
|
_logger.LogInformation("GetBrands called.");
|
||||||
var brands = await _brandRepository.ListAllAsync();
|
var brands = await _brandRepository.ListAsync();
|
||||||
|
|
||||||
var items = brands
|
var items = brands
|
||||||
.Select(brand => new SelectListItem() { Value = brand.Id.ToString(), Text = brand.Brand })
|
.Select(brand => new SelectListItem() { Value = brand.Id.ToString(), Text = brand.Brand })
|
||||||
@@ -96,7 +96,7 @@ namespace Microsoft.eShopWeb.Web.Services
|
|||||||
public async Task<IEnumerable<SelectListItem>> GetTypes()
|
public async Task<IEnumerable<SelectListItem>> GetTypes()
|
||||||
{
|
{
|
||||||
_logger.LogInformation("GetTypes called.");
|
_logger.LogInformation("GetTypes called.");
|
||||||
var types = await _typeRepository.ListAllAsync();
|
var types = await _typeRepository.ListAsync();
|
||||||
|
|
||||||
var items = types
|
var items = types
|
||||||
.Select(type => new SelectListItem() { Value = type.Id.ToString(), Text = type.Type })
|
.Select(type => new SelectListItem() { Value = type.Id.ToString(), Text = type.Type })
|
||||||
|
|||||||
@@ -22,14 +22,14 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Ardalis.ApiEndpoints" Version="2.0.0" />
|
<PackageReference Include="Ardalis.ApiEndpoints" Version="2.0.0" />
|
||||||
<PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" />
|
<PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" />
|
||||||
<PackageReference Include="Ardalis.Specification" Version="4.1.0" />
|
<PackageReference Include="Ardalis.Specification" Version="5.2.0" />
|
||||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
|
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.1" />
|
||||||
|
|
||||||
<PackageReference Include="MediatR" Version="9.0.0" />
|
<PackageReference Include="MediatR" Version="9.0.0" />
|
||||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
|
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
|
||||||
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
|
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" Condition="'$(Configuration)'=='Release'" PrivateAssets="All" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.8.0" />
|
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.11.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
|
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />
|
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.BasketRepositoryTests
|
|||||||
public class SetQuantities
|
public class SetQuantities
|
||||||
{
|
{
|
||||||
private readonly CatalogContext _catalogContext;
|
private readonly CatalogContext _catalogContext;
|
||||||
private readonly IAsyncRepository<Basket> _basketRepository;
|
private readonly EfRepository<Basket> _basketRepository;
|
||||||
private readonly BasketBuilder BasketBuilder = new BasketBuilder();
|
private readonly BasketBuilder BasketBuilder = new BasketBuilder();
|
||||||
|
|
||||||
public SetQuantities()
|
public SetQuantities()
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||||
using Microsoft.eShopWeb.Infrastructure.Data;
|
using Microsoft.eShopWeb.Infrastructure.Data;
|
||||||
using Microsoft.eShopWeb.UnitTests.Builders;
|
using Microsoft.eShopWeb.UnitTests.Builders;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -11,7 +12,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
|
|||||||
public class GetById
|
public class GetById
|
||||||
{
|
{
|
||||||
private readonly CatalogContext _catalogContext;
|
private readonly CatalogContext _catalogContext;
|
||||||
private readonly OrderRepository _orderRepository;
|
private readonly EfRepository<Order> _orderRepository;
|
||||||
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
|
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
|
||||||
private readonly ITestOutputHelper _output;
|
private readonly ITestOutputHelper _output;
|
||||||
public GetById(ITestOutputHelper output)
|
public GetById(ITestOutputHelper output)
|
||||||
@@ -21,7 +22,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
|
|||||||
.UseInMemoryDatabase(databaseName: "TestCatalog")
|
.UseInMemoryDatabase(databaseName: "TestCatalog")
|
||||||
.Options;
|
.Options;
|
||||||
_catalogContext = new CatalogContext(dbOptions);
|
_catalogContext = new CatalogContext(dbOptions);
|
||||||
_orderRepository = new OrderRepository(_catalogContext);
|
_orderRepository = new EfRepository<Order>(_catalogContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -37,6 +38,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
|
|||||||
Assert.Equal(OrderBuilder.TestBuyerId, orderFromRepo.BuyerId);
|
Assert.Equal(OrderBuilder.TestBuyerId, orderFromRepo.BuyerId);
|
||||||
|
|
||||||
// Note: Using InMemoryDatabase OrderItems is available. Will be null if using SQL DB.
|
// Note: Using InMemoryDatabase OrderItems is available. Will be null if using SQL DB.
|
||||||
|
// Use the OrderWithItemsByIdSpec instead of just GetById to get the full aggregate
|
||||||
var firstItem = orderFromRepo.OrderItems.FirstOrDefault();
|
var firstItem = orderFromRepo.OrderItems.FirstOrDefault();
|
||||||
Assert.Equal(OrderBuilder.TestUnits, firstItem.Units);
|
Assert.Equal(OrderBuilder.TestUnits, firstItem.Units);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||||
|
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||||
using Microsoft.eShopWeb.Infrastructure.Data;
|
using Microsoft.eShopWeb.Infrastructure.Data;
|
||||||
using Microsoft.eShopWeb.UnitTests.Builders;
|
using Microsoft.eShopWeb.UnitTests.Builders;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -12,7 +13,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
|
|||||||
public class GetByIdWithItemsAsync
|
public class GetByIdWithItemsAsync
|
||||||
{
|
{
|
||||||
private readonly CatalogContext _catalogContext;
|
private readonly CatalogContext _catalogContext;
|
||||||
private readonly OrderRepository _orderRepository;
|
private readonly EfRepository<Order> _orderRepository;
|
||||||
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
|
private OrderBuilder OrderBuilder { get; } = new OrderBuilder();
|
||||||
|
|
||||||
public GetByIdWithItemsAsync()
|
public GetByIdWithItemsAsync()
|
||||||
@@ -21,7 +22,7 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
|
|||||||
.UseInMemoryDatabase(databaseName: "TestCatalog")
|
.UseInMemoryDatabase(databaseName: "TestCatalog")
|
||||||
.Options;
|
.Options;
|
||||||
_catalogContext = new CatalogContext(dbOptions);
|
_catalogContext = new CatalogContext(dbOptions);
|
||||||
_orderRepository = new OrderRepository(_catalogContext);
|
_orderRepository = new EfRepository<Order>(_catalogContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -47,7 +48,8 @@ namespace Microsoft.eShopWeb.IntegrationTests.Repositories.OrderRepositoryTests
|
|||||||
_catalogContext.SaveChanges();
|
_catalogContext.SaveChanges();
|
||||||
|
|
||||||
//Act
|
//Act
|
||||||
var orderFromRepo = await _orderRepository.GetByIdWithItemsAsync(secondOrderId);
|
var spec = new OrderWithItemsByIdSpec(secondOrderId);
|
||||||
|
var orderFromRepo = await _orderRepository.GetBySpecAsync(spec);
|
||||||
|
|
||||||
//Assert
|
//Assert
|
||||||
Assert.Equal(secondOrderId, orderFromRepo.Id);
|
Assert.Equal(secondOrderId, orderFromRepo.Id);
|
||||||
|
|||||||
@@ -11,25 +11,20 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
|||||||
public class AddItemToBasket
|
public class AddItemToBasket
|
||||||
{
|
{
|
||||||
private readonly string _buyerId = "Test buyerId";
|
private readonly string _buyerId = "Test buyerId";
|
||||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||||
|
|
||||||
public AddItemToBasket()
|
|
||||||
{
|
|
||||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnce()
|
public async Task InvokesBasketRepositoryFirstOrDefaultAsyncOnce()
|
||||||
{
|
{
|
||||||
var basket = new Basket(_buyerId);
|
var basket = new Basket(_buyerId);
|
||||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||||
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||||
|
|
||||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
|
|
||||||
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
|
await basketService.AddItemToBasket(basket.Id, 1, 1.50m);
|
||||||
|
|
||||||
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -37,7 +32,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
|||||||
{
|
{
|
||||||
var basket = new Basket(_buyerId);
|
var basket = new Basket(_buyerId);
|
||||||
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
basket.AddItem(1, It.IsAny<decimal>(), It.IsAny<int>());
|
||||||
_mockBasketRepo.Setup(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
_mockBasketRepo.Setup(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default)).ReturnsAsync(basket);
|
||||||
|
|
||||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
|||||||
public class DeleteBasket
|
public class DeleteBasket
|
||||||
{
|
{
|
||||||
private readonly string _buyerId = "Test buyerId";
|
private readonly string _buyerId = "Test buyerId";
|
||||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||||
|
|
||||||
public DeleteBasket()
|
|
||||||
{
|
|
||||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()
|
public async Task ShouldInvokeBasketRepositoryDeleteAsyncOnce()
|
||||||
|
|||||||
@@ -1,40 +1,35 @@
|
|||||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
using Microsoft.eShopWeb.ApplicationCore.Exceptions;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||||
{
|
{
|
||||||
public class SetQuantities
|
public class SetQuantities
|
||||||
{
|
{
|
||||||
private readonly int _invalidId = -1;
|
private readonly int _invalidId = -1;
|
||||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||||
|
|
||||||
public SetQuantities()
|
[Fact]
|
||||||
{
|
public async Task ThrowsGivenInvalidBasketId()
|
||||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
{
|
||||||
}
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
|
|
||||||
[Fact]
|
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
|
||||||
public async Task ThrowsGivenInvalidBasketId()
|
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
|
||||||
{
|
}
|
||||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
|
||||||
|
[Fact]
|
||||||
await Assert.ThrowsAsync<BasketNotFoundException>(async () =>
|
public async Task ThrowsGivenNullQuantities()
|
||||||
await basketService.SetQuantities(_invalidId, new System.Collections.Generic.Dictionary<string, int>()));
|
{
|
||||||
}
|
var basketService = new BasketService(null, null);
|
||||||
|
|
||||||
[Fact]
|
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
||||||
public async Task ThrowsGivenNullQuantities()
|
await basketService.SetQuantities(123, null));
|
||||||
{
|
}
|
||||||
var basketService = new BasketService(null, null);
|
}
|
||||||
|
}
|
||||||
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
|
|
||||||
await basketService.SetQuantities(123, null));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,41 +1,36 @@
|
|||||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Services;
|
using Microsoft.eShopWeb.ApplicationCore.Services;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTests
|
||||||
{
|
{
|
||||||
public class TransferBasket
|
public class TransferBasket
|
||||||
{
|
{
|
||||||
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
|
private readonly string _nonexistentAnonymousBasketBuyerId = "nonexistent-anonymous-basket-buyer-id";
|
||||||
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
|
private readonly string _existentAnonymousBasketBuyerId = "existent-anonymous-basket-buyer-id";
|
||||||
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
|
private readonly string _nonexistentUserBasketBuyerId = "newuser@microsoft.com";
|
||||||
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
|
private readonly string _existentUserBasketBuyerId = "testuser@microsoft.com";
|
||||||
private readonly Mock<IAsyncRepository<Basket>> _mockBasketRepo;
|
private readonly Mock<IRepository<Basket>> _mockBasketRepo = new();
|
||||||
|
|
||||||
public TransferBasket()
|
[Fact]
|
||||||
|
public async Task ThrowsGivenNullAnonymousId()
|
||||||
{
|
{
|
||||||
_mockBasketRepo = new Mock<IAsyncRepository<Basket>>();
|
var basketService = new BasketService(null, null);
|
||||||
}
|
|
||||||
|
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
|
||||||
[Fact]
|
}
|
||||||
public async Task ThrowsGivenNullAnonymousId()
|
|
||||||
{
|
[Fact]
|
||||||
var basketService = new BasketService(null, null);
|
public async Task ThrowsGivenNullUserId()
|
||||||
|
{
|
||||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync(null, "steve"));
|
var basketService = new BasketService(null, null);
|
||||||
}
|
|
||||||
|
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
|
||||||
[Fact]
|
|
||||||
public async Task ThrowsGivenNullUserId()
|
|
||||||
{
|
|
||||||
var basketService = new BasketService(null, null);
|
|
||||||
|
|
||||||
await Assert.ThrowsAsync<ArgumentNullException>(async () => await basketService.TransferBasketAsync("abcdefg", null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -43,12 +38,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
|||||||
{
|
{
|
||||||
var anonymousBasket = null as Basket;
|
var anonymousBasket = null as Basket;
|
||||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||||
.ReturnsAsync(anonymousBasket)
|
.ReturnsAsync(anonymousBasket)
|
||||||
.ReturnsAsync(userBasket);
|
.ReturnsAsync(userBasket);
|
||||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
await basketService.TransferBasketAsync(_nonexistentAnonymousBasketBuyerId, _existentUserBasketBuyerId);
|
||||||
_mockBasketRepo.Verify(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
_mockBasketRepo.Verify(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default), Times.Once);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -60,7 +55,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
|||||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||||
userBasket.AddItem(1, 10, 4);
|
userBasket.AddItem(1, 10, 4);
|
||||||
userBasket.AddItem(2, 99, 3);
|
userBasket.AddItem(2, 99, 3);
|
||||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||||
.ReturnsAsync(anonymousBasket)
|
.ReturnsAsync(anonymousBasket)
|
||||||
.ReturnsAsync(userBasket);
|
.ReturnsAsync(userBasket);
|
||||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
@@ -77,7 +72,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
|||||||
{
|
{
|
||||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||||
var userBasket = new Basket(_existentUserBasketBuyerId);
|
var userBasket = new Basket(_existentUserBasketBuyerId);
|
||||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||||
.ReturnsAsync(anonymousBasket)
|
.ReturnsAsync(anonymousBasket)
|
||||||
.ReturnsAsync(userBasket);
|
.ReturnsAsync(userBasket);
|
||||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
@@ -91,12 +86,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Services.BasketServiceTes
|
|||||||
{
|
{
|
||||||
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
var anonymousBasket = new Basket(_existentAnonymousBasketBuyerId);
|
||||||
var userBasket = null as Basket;
|
var userBasket = null as Basket;
|
||||||
_mockBasketRepo.SetupSequence(x => x.FirstOrDefaultAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
_mockBasketRepo.SetupSequence(x => x.GetBySpecAsync(It.IsAny<BasketWithItemsSpecification>(), default))
|
||||||
.ReturnsAsync(anonymousBasket)
|
.ReturnsAsync(anonymousBasket)
|
||||||
.ReturnsAsync(userBasket);
|
.ReturnsAsync(userBasket);
|
||||||
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
var basketService = new BasketService(_mockBasketRepo.Object, null);
|
||||||
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId);
|
await basketService.TransferBasketAsync(_existentAnonymousBasketBuyerId, _nonexistentUserBasketBuyerId);
|
||||||
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once);
|
_mockBasketRepo.Verify(x => x.AddAsync(It.Is<Basket>(x => x.BuyerId == _nonexistentUserBasketBuyerId), default), Times.Once);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Ardalis.Specification.EntityFrameworkCore;
|
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities.BasketAggregate;
|
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -13,16 +12,12 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
|||||||
private readonly int _testBasketId = 123;
|
private readonly int _testBasketId = 123;
|
||||||
private readonly string _buyerId = "Test buyerId";
|
private readonly string _buyerId = "Test buyerId";
|
||||||
|
|
||||||
// tests with specifications can use an evaluator or just WhereExpressions.FirstOrDefault if only one
|
|
||||||
private readonly SpecificationEvaluator<Basket> _evaluator = new SpecificationEvaluator<Basket>();
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void MatchesBasketWithGivenBasketId()
|
public void MatchesBasketWithGivenBasketId()
|
||||||
{
|
{
|
||||||
var spec = new BasketWithItemsSpecification(_testBasketId);
|
var spec = new BasketWithItemsSpecification(_testBasketId);
|
||||||
|
|
||||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
Assert.NotNull(result);
|
Assert.NotNull(result);
|
||||||
Assert.Equal(_testBasketId, result.Id);
|
Assert.Equal(_testBasketId, result.Id);
|
||||||
@@ -34,8 +29,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
|||||||
int badBasketId = -1;
|
int badBasketId = -1;
|
||||||
var spec = new BasketWithItemsSpecification(badBasketId);
|
var spec = new BasketWithItemsSpecification(badBasketId);
|
||||||
|
|
||||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||||
.Any();
|
|
||||||
|
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
}
|
}
|
||||||
@@ -45,8 +39,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
|||||||
{
|
{
|
||||||
var spec = new BasketWithItemsSpecification(_buyerId);
|
var spec = new BasketWithItemsSpecification(_buyerId);
|
||||||
|
|
||||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
var result = spec.Evaluate(GetTestBasketCollection()).FirstOrDefault();
|
||||||
.FirstOrDefault();
|
|
||||||
|
|
||||||
Assert.NotNull(result);
|
Assert.NotNull(result);
|
||||||
Assert.Equal(_buyerId, result.BuyerId);
|
Assert.Equal(_buyerId, result.BuyerId);
|
||||||
@@ -58,8 +51,7 @@ namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Specifications
|
|||||||
string badBuyerId = "badBuyerId";
|
string badBuyerId = "badBuyerId";
|
||||||
var spec = new BasketWithItemsSpecification(badBuyerId);
|
var spec = new BasketWithItemsSpecification(badBuyerId);
|
||||||
|
|
||||||
var result = _evaluator.GetQuery(GetTestBasketCollection().AsQueryable(), spec)
|
var result = spec.Evaluate(GetTestBasketCollection()).Any();
|
||||||
.Any();
|
|
||||||
|
|
||||||
Assert.False(result);
|
Assert.False(result);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
|
|||||||
{
|
{
|
||||||
public class GetMyOrders
|
public class GetMyOrders
|
||||||
{
|
{
|
||||||
private readonly Mock<IOrderRepository> _mockOrderRepository;
|
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
|
||||||
|
|
||||||
public GetMyOrders()
|
public GetMyOrders()
|
||||||
{
|
{
|
||||||
@@ -20,12 +20,12 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
|
|||||||
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
||||||
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
||||||
|
|
||||||
_mockOrderRepository = new Mock<IOrderRepository>();
|
_mockOrderRepository = new Mock<IReadRepository<Order>>();
|
||||||
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
|
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task NotReturnNullIfOrdersArePresent()
|
public async Task NotReturnNullIfOrdersArePresIent()
|
||||||
{
|
{
|
||||||
var request = new eShopWeb.Web.Features.MyOrders.GetMyOrders("SomeUserName");
|
var request = new eShopWeb.Web.Features.MyOrders.GetMyOrders("SomeUserName");
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using Ardalis.Specification;
|
using Ardalis.Specification;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
using Microsoft.eShopWeb.ApplicationCore.Entities.OrderAggregate;
|
||||||
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
|
||||||
|
using Microsoft.eShopWeb.ApplicationCore.Specifications;
|
||||||
using Microsoft.eShopWeb.Web.Features.OrderDetails;
|
using Microsoft.eShopWeb.Web.Features.OrderDetails;
|
||||||
using Moq;
|
using Moq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -12,7 +13,7 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
|
|||||||
{
|
{
|
||||||
public class GetOrderDetails
|
public class GetOrderDetails
|
||||||
{
|
{
|
||||||
private readonly Mock<IOrderRepository> _mockOrderRepository;
|
private readonly Mock<IReadRepository<Order>> _mockOrderRepository;
|
||||||
|
|
||||||
public GetOrderDetails()
|
public GetOrderDetails()
|
||||||
{
|
{
|
||||||
@@ -20,8 +21,9 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
|
|||||||
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
var address = new Address(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>());
|
||||||
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
Order order = new Order("buyerId", address, new List<OrderItem> { item });
|
||||||
|
|
||||||
_mockOrderRepository = new Mock<IOrderRepository>();
|
_mockOrderRepository = new Mock<IReadRepository<Order>>();
|
||||||
_mockOrderRepository.Setup(x => x.ListAsync(It.IsAny<ISpecification<Order>>(),default)).ReturnsAsync(new List<Order> { order });
|
_mockOrderRepository.Setup(x => x.GetBySpecAsync(It.IsAny<OrderWithItemsByIdSpec>(),default))
|
||||||
|
.ReturnsAsync(order);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -35,17 +37,5 @@ namespace Microsoft.eShopWeb.UnitTests.MediatorHandlers.OrdersTests
|
|||||||
|
|
||||||
Assert.NotNull(result);
|
Assert.NotNull(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public async Task BeNullIfOrderNotFound()
|
|
||||||
{
|
|
||||||
var request = new eShopWeb.Web.Features.OrderDetails.GetOrderDetails("SomeUserName", 100);
|
|
||||||
|
|
||||||
var handler = new GetOrderDetailsHandler(_mockOrderRepository.Object);
|
|
||||||
|
|
||||||
var result = await handler.Handle(request, CancellationToken.None);
|
|
||||||
|
|
||||||
Assert.Null(result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user