Skip to content

Commit 01260fd

Browse files
author
babisque
committed
fluent validation added to product
1 parent b73b546 commit 01260fd

File tree

5 files changed

+40
-35
lines changed

5 files changed

+40
-35
lines changed

Catalog/Catalog.API/Controllers/ProductController.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Catalog.Core.Entities;
33
using Catalog.Core.Logging;
44
using Catalog.Core.Repositories;
5+
using FluentValidation;
6+
using FluentValidation.Results;
57
using Microsoft.AspNetCore.Mvc;
68

79
namespace Catalog.API.Controllers
@@ -12,11 +14,13 @@ public class ProductController : ControllerBase
1214
{
1315
private readonly IProductRepository _productRepository;
1416
private readonly ILogger<ProductController> _logger;
17+
private readonly IValidator<Product> _validator;
1518

16-
public ProductController(IProductRepository productRepository, ILogger<ProductController> logger)
19+
public ProductController(IProductRepository productRepository, ILogger<ProductController> logger, IValidator<Product> validator)
1720
{
1821
_productRepository = productRepository;
1922
_logger = logger;
23+
_validator = validator;
2024
}
2125

2226
[HttpPost]
@@ -35,6 +39,20 @@ public async Task<ActionResult<Product>> Post([FromForm] ProductDtoRequest req)
3539
Image = req.Image != null ? await GetImageBytesAsync(req.Image) : null,
3640
Stock = req.Stock
3741
};
42+
43+
ValidationResult result = await _validator.ValidateAsync(product);
44+
if (!result.IsValid)
45+
{
46+
47+
var errors = result.Errors
48+
.GroupBy(e => e.PropertyName)
49+
.ToDictionary(
50+
g => g.Key,
51+
g => g.Select(e => e.ErrorMessage).ToArray()
52+
);
53+
54+
return BadRequest(new ValidationProblemDetails(errors));
55+
}
3856

3957
await _productRepository.CreateAsync(product);
4058
_logger.LogInformation($"Product created successfully with ID {product.Id}.");

Catalog/Catalog.API/Program.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
using Catalog.Core.Entities;
12
using Catalog.Core.Logging;
23
using Catalog.Core.Repositories;
4+
using Catalog.Core.Validators;
35
using Catalog.Infrastructure.Repositories;
6+
using FluentValidation;
47
using Microsoft.EntityFrameworkCore;
58

69
var builder = WebApplication.CreateBuilder(args);
@@ -27,6 +30,7 @@
2730
});
2831

2932
builder.Services.AddScoped<IProductRepository, ProductRepository>();
33+
builder.Services.AddScoped<IValidator<Product>, ProductValidator>();
3034

3135
var app = builder.Build();
3236

Catalog/Catalog.Core/Catalog.Core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="FluentValidation" Version="11.9.1" />
1011
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.0" />
1112
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="2.2.0" />
1213
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Catalog.Core.Entities;
2+
using FluentValidation;
3+
4+
namespace Catalog.Core.Validators;
5+
6+
public class ProductValidator : AbstractValidator<Product>
7+
{
8+
public ProductValidator()
9+
{
10+
RuleFor(p => p.Name).NotEmpty().Length(0, 100);
11+
RuleFor(p => p.Description).MaximumLength(255);
12+
RuleFor(p => p.Price).GreaterThan(0);
13+
RuleFor(p => p.Category).NotEmpty();
14+
RuleFor(p => p.Stock).GreaterThanOrEqualTo(0);
15+
}
16+
}

Catalog/script.sql

-34
This file was deleted.

0 commit comments

Comments
 (0)