Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taak 3 hint (add PizzaValidator in DI) #2

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static IServiceCollection AddContosoPizzaServices(this IServiceCollection
services.AddScoped<IToppingService, ToppingService>();
services.AddSingleton(s => TimeProvider.System);
services.AddSingleton<IPriceCalculatorService, PriceCalculatorService>();
services.AddScoped<IPizzaValidator, PizzaValidator>();
services.AddScoped<IPizzaService, PizzaService>();
return services;
}
Expand Down
40 changes: 5 additions & 35 deletions src/Backend/Contoso.Pizza.AdminApi.Services/PizzaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace Contoso.Pizza.AdminApi.Services;

public class PizzaService : IPizzaService
{
private readonly IPriceCalculatorService _priceCalculatorService;
private readonly IPizzaValidator _pizzaValidator;
private readonly IPizzaRepository _repository;
private readonly IMapper _mapper;

public PizzaService(IPriceCalculatorService priceCalculatorService, IPizzaRepository repository, IMapper mapper)
public PizzaService(IPizzaValidator pizzaValidator, IPizzaRepository repository, IMapper mapper)
{
_priceCalculatorService = priceCalculatorService;
_pizzaValidator = pizzaValidator;
_repository = repository;
_mapper = mapper;
}
Expand All @@ -33,7 +33,7 @@ public async Task<IEnumerable<PizzaEntity>> GetAllAsync()

public async Task<PizzaEntity> AddAsync(PizzaEntity entity)
{
var validationErrors = await IsValidPizza(entity);
var validationErrors = await _pizzaValidator.IsValidPizza(entity);
if (validationErrors.Count != 0)
{
throw new ArgumentException(string.Join("; ", validationErrors));
Expand All @@ -45,7 +45,7 @@ public async Task<PizzaEntity> AddAsync(PizzaEntity entity)

public async Task<int> UpdateAsync(PizzaEntity entity)
{
var validationErrors = await IsValidPizza(entity);
var validationErrors = await _pizzaValidator.IsValidPizza(entity);
if (validationErrors.Count != 0)
{
throw new ArgumentException(string.Join("; ", validationErrors));
Expand All @@ -58,34 +58,4 @@ public async Task<int> DeleteAsync(Guid id)
{
return await _repository.DeleteAsync(id);
}

private async Task<List<string>> IsValidPizza(PizzaEntity pizza)
{
var validationErrors = new List<string>();

if (string.IsNullOrWhiteSpace(pizza.Name))
{
validationErrors.Add("Pizza name cannot be empty.");
}

if (pizza.Toppings == null || !pizza.Toppings.Any())
{
validationErrors.Add("Pizza must have at least one topping.");
}

var totalPrice = _priceCalculatorService.CalculatePrice(pizza);

if (totalPrice < 5 || totalPrice > 50)
{
validationErrors.Add("Total price of the pizza must be between $5 and $50.");
}

var existingPizzas = await _repository.GetAllAsync();
if (existingPizzas.Any(p => p.Name.Equals(pizza.Name, StringComparison.OrdinalIgnoreCase)))
{
validationErrors.Add("Pizza name must be unique.");
}

return validationErrors;
}
}
45 changes: 45 additions & 0 deletions src/Backend/Contoso.Pizza.AdminApi.Services/PizzaValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Contoso.Pizza.AdminApi.Models;
using Contoso.Pizza.Data.Contracts;

namespace Contoso.Pizza.AdminApi.Services;

public interface IPizzaValidator
{
Task<IReadOnlyCollection<string>> IsValidPizza(PizzaEntity pizza);
}

public class PizzaValidator(IPriceCalculatorService priceCalculatorService, IPizzaRepository repository) : IPizzaValidator
{
private readonly IPriceCalculatorService _priceCalculatorService = priceCalculatorService;
private readonly IPizzaRepository _repository = repository;

public async Task<IReadOnlyCollection<string>> IsValidPizza(PizzaEntity pizza)
{
var validationErrors = new List<string>();

if (string.IsNullOrWhiteSpace(pizza.Name))
{
validationErrors.Add("Pizza name cannot be empty.");
}

if (pizza.Toppings == null || !pizza.Toppings.Any())
{
validationErrors.Add("Pizza must have at least one topping.");
}

var totalPrice = _priceCalculatorService.CalculatePrice(pizza);

if (totalPrice < 5 || totalPrice > 50)
{
validationErrors.Add("Total price of the pizza must be between $5 and $50.");
}

var existingPizzas = await _repository.GetAllAsync();
if (existingPizzas.Any(p => p.Name.Equals(pizza.Name, StringComparison.OrdinalIgnoreCase)))
{
validationErrors.Add("Pizza name must be unique.");
}

return validationErrors;
}
}
23 changes: 21 additions & 2 deletions src/Backend/Contoso.Pizza.Data.Tests/PizzaServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,28 @@ namespace Contoso.Pizza.AdminApi.Models;
[TestFixture]
public class PizzaServiceTest
{
private IPriceCalculatorService _priceCalculatorService = default!;
private IPizzaRepository _repo = default!;
private PizzaValidator _sut = default!;

[SetUp]
public void Setup()
{
_priceCalculatorService = A.Fake<IPriceCalculatorService>();
_repo = A.Fake<IPizzaRepository>();
_sut = new PizzaValidator(_priceCalculatorService, _repo);
}

}

[Test]
public async Task CalculatePrice_WithBasicPizza_ReturnsMinimumAmount()
{
A.CallTo(() => _priceCalculatorService.CalculatePrice(A<PizzaEntity>._)).Returns(30);
A.CallTo(() => _repo.GetAllAsync())
.ReturnsLazily((c) =>
Task.FromResult<IEnumerable<DM.Pizza>>(
[
new DM.Pizza{Name = "Test Pizza", Price = 2,Sauce = new() { Name = "Sauce" }}
]
));
}
}