Adding guards and more tests (#68)
* Adding single entity by spec method to repository * Adding guards and more unit tests
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Ardalis.GuardClauses" Version="1.1.1" />
|
||||
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
23
src/ApplicationCore/Exceptions/BasketNotFoundException.cs
Normal file
23
src/ApplicationCore/Exceptions/BasketNotFoundException.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace ApplicationCore.Exceptions
|
||||
{
|
||||
public class BasketNotFoundException : Exception
|
||||
{
|
||||
public BasketNotFoundException(int basketId) : base($"No basket found with id {basketId}")
|
||||
{
|
||||
}
|
||||
|
||||
protected BasketNotFoundException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
|
||||
public BasketNotFoundException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public BasketNotFoundException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/ApplicationCore/Exceptions/GuardExtensions.cs
Normal file
14
src/ApplicationCore/Exceptions/GuardExtensions.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using ApplicationCore.Exceptions;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
|
||||
namespace Ardalis.GuardClauses
|
||||
{
|
||||
public static class FooGuard
|
||||
{
|
||||
public static void NullBasket(this IGuardClause guardClause, int basketId, Basket basket)
|
||||
{
|
||||
if (basket == null)
|
||||
throw new BasketNotFoundException(basketId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ApplicationCore.Interfaces
|
||||
{
|
||||
public interface IRepository<T> where T : BaseEntity
|
||||
{
|
||||
T GetById(int id);
|
||||
T GetSingleBySpec(ISpecification<T> spec);
|
||||
IEnumerable<T> ListAll();
|
||||
IEnumerable<T> List(ISpecification<T> spec);
|
||||
T Add(T entity);
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using ApplicationCore.Specifications;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Linq;
|
||||
using Ardalis.GuardClauses;
|
||||
|
||||
namespace ApplicationCore.Services
|
||||
{
|
||||
@@ -43,6 +44,7 @@ namespace ApplicationCore.Services
|
||||
|
||||
public async Task<int> GetBasketItemCountAsync(string userName)
|
||||
{
|
||||
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
||||
var basketSpec = new BasketWithItemsSpecification(userName);
|
||||
var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();
|
||||
if (basket == null)
|
||||
@@ -57,7 +59,9 @@ namespace ApplicationCore.Services
|
||||
|
||||
public async Task SetQuantities(int basketId, Dictionary<string, int> quantities)
|
||||
{
|
||||
Guard.Against.Null(quantities, nameof(quantities));
|
||||
var basket = await _basketRepository.GetByIdAsync(basketId);
|
||||
Guard.Against.NullBasket(basketId, basket);
|
||||
foreach (var item in basket.Items)
|
||||
{
|
||||
if (quantities.TryGetValue(item.Id.ToString(), out var quantity))
|
||||
@@ -71,6 +75,8 @@ namespace ApplicationCore.Services
|
||||
|
||||
public async Task TransferBasketAsync(string anonymousId, string userName)
|
||||
{
|
||||
Guard.Against.NullOrEmpty(anonymousId, nameof(anonymousId));
|
||||
Guard.Against.NullOrEmpty(userName, nameof(userName));
|
||||
var basketSpec = new BasketWithItemsSpecification(anonymousId);
|
||||
var basket = (await _basketRepository.ListAsync(basketSpec)).FirstOrDefault();
|
||||
if (basket == null) return;
|
||||
|
||||
@@ -3,6 +3,7 @@ using ApplicationCore.Entities.OrderAggregate;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.eShopWeb.ApplicationCore.Entities;
|
||||
using System.Collections.Generic;
|
||||
using Ardalis.GuardClauses;
|
||||
|
||||
namespace ApplicationCore.Services
|
||||
{
|
||||
@@ -24,6 +25,7 @@ namespace ApplicationCore.Services
|
||||
public async Task CreateOrderAsync(int basketId, Address shippingAddress)
|
||||
{
|
||||
var basket = await _basketRepository.GetByIdAsync(basketId);
|
||||
Guard.Against.NullBasket(basketId, basket);
|
||||
var items = new List<OrderItem>();
|
||||
foreach (var item in basket.Items)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user