diff --git a/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs b/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs index 02d3f90..2a2463a 100644 --- a/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs +++ b/Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs @@ -14,9 +14,9 @@ namespace Microservice.Customer.Api.Test.Unit; [TestFixture] public class GetCustomerMediatrTests { - private Mock customerRepositoryMock = new Mock(); - private Mock customerHttpAccessorMock = new Mock(); - private ServiceCollection services = new ServiceCollection(); + private Mock customerRepositoryMock = new(); + private Mock customerHttpAccessorMock = new(); + private ServiceCollection services = new(); private ServiceProvider serviceProvider; private IMediator mediator; @@ -81,13 +81,13 @@ public void Get_customer_return_exception() customerHttpAccessorMock.Setup(x => x.CustomerId) .Returns(customerId); - var getCustomerRequest = new GetCustomerRequest(Guid.NewGuid()); + var getCustomerRequest = new GetCustomerRequest(Guid.NewGuid()); var validationException = Assert.ThrowsAsync(async () => { await mediator.Send(getCustomerRequest); }); - + Assert.That(validationException.Message, Is.EqualTo("Customer not found.")); } } \ No newline at end of file diff --git a/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs b/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs index c2b1ad8..61d8bf1 100644 --- a/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs +++ b/Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs @@ -2,7 +2,6 @@ using MediatR; using Microservice.Customer.Api.Data.Repository.Interfaces; using Microservice.Customer.Api.Helpers; -using Microservice.Customer.Api.Helpers.Exceptions; using Microservice.Customer.Api.Helpers.Interfaces; using Microservice.Customer.Api.MediatR.AddCustomer; using Microsoft.Extensions.DependencyInjection; @@ -14,9 +13,9 @@ namespace Microservice.Customer.Api.Test.Unit; [TestFixture] public class UpdateCustomerMediatrTests { - private Mock customerRepositoryMock = new Mock(); - private Mock customerHttpAccessorMock = new Mock(); - private ServiceCollection services = new ServiceCollection(); + private Mock customerRepositoryMock = new(); + private Mock customerHttpAccessorMock = new(); + private ServiceCollection services = new(); private ServiceProvider serviceProvider; private IMediator mediator; private Guid customerId; @@ -32,7 +31,7 @@ public void OneTimeSetup() services.AddAutoMapper(Assembly.GetAssembly(typeof(UpdateCustomerMapper))); serviceProvider = services.BuildServiceProvider(); - mediator = serviceProvider.GetRequiredService(); + mediator = serviceProvider.GetRequiredService(); } [OneTimeTearDown] @@ -75,7 +74,7 @@ public async Task Customer_updated_return_success_message() customer.Email = "Changed Email"; customerRepositoryMock - .Setup(x => x.UpdateAsync(customer)); + .Setup(x => x.UpdateAsync(customer)); var updateCustomerRequest = new UpdateCustomerRequest(customerId, "ValidEmail@hotmail.com", "TestSurname", "TestFirstName"); @@ -93,14 +92,14 @@ public void Customer_not_updated_id_does_not_exists_return_exception_fail_messag .Returns(Task.FromResult(false)); var updateCustomerRequest = new UpdateCustomerRequest(customerId, "ValidEmail@hotmail.com", "TestSurname", "TestFirstName"); - + var validationException = Assert.ThrowsAsync(async () => { await mediator.Send(updateCustomerRequest); }); Assert.That(validationException.Errors.Count, Is.EqualTo(1)); - Assert.That(validationException.Errors.ElementAt(0).ErrorMessage, Is.EqualTo("The customer does not exists.")); + Assert.That(validationException.Errors.ElementAt(0).ErrorMessage, Is.EqualTo("The customer does not exists.")); } [Test] @@ -116,9 +115,9 @@ public void Customer_not_updated_email_exists_return_exception_fail_message() { await mediator.Send(updateCustomerRequest); }); - - Assert.That(validationException .Errors.Count, Is.EqualTo(1)); - Assert.That(validationException.Errors.ElementAt(0).ErrorMessage, Is.EqualTo("Customer with this email already exists")); + + Assert.That(validationException.Errors.Count, Is.EqualTo(1)); + Assert.That(validationException.Errors.ElementAt(0).ErrorMessage, Is.EqualTo("Customer with this email already exists")); } [Test] @@ -160,7 +159,7 @@ public void Customer_not_updated_invalid_surname_firstname_return_exception_fail [Test] public void Customer_not_updated_no_email_surname_firstname_return_exception_fail_message() - { + { customerRepositoryMock .Setup(x => x.ExistsAsync("ValidEmail@hotmail.com")) .Returns(Task.FromResult(false)); @@ -175,10 +174,10 @@ public void Customer_not_updated_no_email_surname_firstname_return_exception_fai Assert.That(validationException.Errors.Count, Is.EqualTo(7)); Assert.That(validationException.Errors.ElementAt(0).ErrorMessage, Is.EqualTo("Email is required.")); Assert.That(validationException.Errors.ElementAt(1).ErrorMessage, Is.EqualTo("Email length between 8 and 150.")); - Assert.That(validationException.Errors.ElementAt(2).ErrorMessage, Is.EqualTo("Invalid Email.")); + Assert.That(validationException.Errors.ElementAt(2).ErrorMessage, Is.EqualTo("Invalid Email.")); Assert.That(validationException.Errors.ElementAt(3).ErrorMessage, Is.EqualTo("Surname is required.")); Assert.That(validationException.Errors.ElementAt(4).ErrorMessage, Is.EqualTo("Surname length between 1 and 30.")); Assert.That(validationException.Errors.ElementAt(5).ErrorMessage, Is.EqualTo("First name is required.")); Assert.That(validationException.Errors.ElementAt(6).ErrorMessage, Is.EqualTo("First name length between 1 and 30.")); - } + } } \ No newline at end of file diff --git a/Microservice.Customer.Api/Data/Context/CustomerDbContext.cs b/Microservice.Customer.Api/Data/Context/CustomerDbContext.cs index 516cb7b..bd18b70 100644 --- a/Microservice.Customer.Api/Data/Context/CustomerDbContext.cs +++ b/Microservice.Customer.Api/Data/Context/CustomerDbContext.cs @@ -4,15 +4,15 @@ namespace Microservice.Customer.Api.Data.Contexts; public class CustomerDbContext : DbContext -{ +{ public CustomerDbContext(DbContextOptions options) : base(options) { } - + public DbSet Customer { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { - base.OnModelCreating(modelBuilder); + base.OnModelCreating(modelBuilder); - modelBuilder.Entity().HasData(DefaultData.GetCustomerDefaultData()); + modelBuilder.Entity().HasData(DefaultData.GetCustomerDefaultData()); } } \ No newline at end of file diff --git a/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs b/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs index f1bf2e9..36a036a 100644 --- a/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs +++ b/Microservice.Customer.Api/Data/Repository/CustomerRepository.cs @@ -5,14 +5,14 @@ namespace Microservice.Customer.Api.Data.Repository; public class CustomerRepository(IDbContextFactory dbContextFactory) : ICustomerRepository -{ +{ public IDbContextFactory _dbContextFactory { get; set; } = dbContextFactory; public async Task AddAsync(Domain.Customer customer) { await using var db = await _dbContextFactory.CreateDbContextAsync(); await db.AddAsync(customer); - await db.SaveChangesAsync(); + await db.SaveChangesAsync(); return customer; } @@ -21,8 +21,8 @@ public async Task UpdateAsync(Domain.Customer entity) { using var db = _dbContextFactory.CreateDbContext(); db.Customer.Update(entity); - await db.SaveChangesAsync(); - } + await db.SaveChangesAsync(); + } public async Task> AllAsync() { diff --git a/Microservice.Customer.Api/Domain/Customer.cs b/Microservice.Customer.Api/Domain/Customer.cs index cf0378d..7da3b7b 100644 --- a/Microservice.Customer.Api/Domain/Customer.cs +++ b/Microservice.Customer.Api/Domain/Customer.cs @@ -6,7 +6,7 @@ namespace Microservice.Customer.Api.Domain; [Table("MSOS_Customer")] public class Customer { - [Key] + [Key] public Guid Id { get; set; } [MaxLength(30)] diff --git a/Microservice.Customer.Api/Endpoints/Endpoints.cs b/Microservice.Customer.Api/Endpoints/Endpoints.cs index 993a373..55e7bdb 100644 --- a/Microservice.Customer.Api/Endpoints/Endpoints.cs +++ b/Microservice.Customer.Api/Endpoints/Endpoints.cs @@ -41,9 +41,9 @@ public static void ConfigureRoutes(this WebApplication app, ConfigurationManager customerGroup.MapPut("/update", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] async ([FromBody] UpdateCustomerRequest updateCustomerRequest, [FromServices] IMediator mediator, ICustomerHttpAccessor customerHttpAccessor) => - { + { updateCustomerRequest = updateCustomerRequest with { Id = customerHttpAccessor.CustomerId }; - var updateCustomerResponse = await mediator.Send(updateCustomerRequest); + var updateCustomerResponse = await mediator.Send(updateCustomerRequest); return Results.Ok(updateCustomerResponse); }) .Accepts("application/json") diff --git a/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs b/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs index ef921fd..8342a02 100644 --- a/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs +++ b/Microservice.Customer.Api/Extensions/IServiceCollectionExtensions.cs @@ -8,7 +8,6 @@ using Microservice.Customer.Api.Helpers; using Microservice.Customer.Api.Helpers.Interfaces; using Microservice.Customer.Api.Helpers.Swagger; -using Microservice.Customer.Api.MediatR.AddCustomer; using Microservice.Customer.Api.MediatR.GetCustomer; using Microservice.Customer.Api.Middleware; using Microsoft.EntityFrameworkCore; @@ -20,7 +19,7 @@ namespace Microservice.Customer.Api.Extensions; public static class IServiceCollectionExtensions -{ +{ public static void ConfigureExceptionHandling(this IServiceCollection services) { services.AddTransient(); @@ -35,10 +34,10 @@ public static void ConfigureDI(this IServiceCollection services) { services.AddMemoryCache(); services.AddHttpContextAccessor(); - services.AddSingleton(); - services.AddScoped(); - services.TryAddSingleton(); - } + services.AddSingleton(); + services.AddScoped(); + services.TryAddSingleton(); + } public static void ConfigureAutoMapper(this IServiceCollection services) { @@ -49,14 +48,14 @@ public static void ConfigureDatabaseContext(this IServiceCollection services, Co { services.AddDbContextFactory(options => options.UseSqlServer(configuration.GetConnectionString(Helpers.Constants.DatabaseConnectionString), - options => options.EnableRetryOnFailure())); - } + options => options.EnableRetryOnFailure())); + } public static void ConfigureMediatr(this IServiceCollection services) { services.AddValidatorsFromAssemblyContaining(); services.AddMediatR(_ => _.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); - services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>)); + services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidatorBehavior<,>)); } public static void ConfigureApiVersioning(this IServiceCollection services) diff --git a/Microservice.Customer.Api/Extensions/JwtExtensions.cs b/Microservice.Customer.Api/Extensions/JwtExtensions.cs index 463388e..1662a05 100644 --- a/Microservice.Customer.Api/Extensions/JwtExtensions.cs +++ b/Microservice.Customer.Api/Extensions/JwtExtensions.cs @@ -6,7 +6,7 @@ namespace Microservice.Customer.Api.Address.Api.Extensions; public static class JwtExtensions -{ +{ public static void AddJwtAuthentication(this IServiceCollection services) { services.AddAuthentication(optiones => @@ -30,6 +30,6 @@ public static void AddJwtAuthentication(this IServiceCollection services) }; o.MapInboundClaims = false; - }); - } + }); + } } \ No newline at end of file diff --git a/Microservice.Customer.Api/Helpers/Constants.cs b/Microservice.Customer.Api/Helpers/Constants.cs index a4bf6df..e55bd1d 100644 --- a/Microservice.Customer.Api/Helpers/Constants.cs +++ b/Microservice.Customer.Api/Helpers/Constants.cs @@ -4,7 +4,7 @@ public class Constants { public const string JwtIssuer = "JWT_ISSUER"; public const string JwtAudience = "JWT_AUDIENCE"; - public const string JwtSymmetricSecurityKey = "JWT_SYMMETRIC_SECURITY_KEY"; + public const string JwtSymmetricSecurityKey = "JWT_SYMMETRIC_SECURITY_KEY"; public const string DatabaseConnectionString = "SQLAZURECONNSTR_CUSTOMER"; } \ No newline at end of file diff --git a/Microservice.Customer.Api/Helpers/CustomerHttpAccessor.cs b/Microservice.Customer.Api/Helpers/CustomerHttpAccessor.cs index 386f078..97afb94 100644 --- a/Microservice.Customer.Api/Helpers/CustomerHttpAccessor.cs +++ b/Microservice.Customer.Api/Helpers/CustomerHttpAccessor.cs @@ -10,5 +10,5 @@ public CustomerHttpAccessor(IHttpContextAccessor accessor) _accessor = accessor; } - public Guid CustomerId => new Guid( _accessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); + public Guid CustomerId => new(_accessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); } diff --git a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerMapper.cs b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerMapper.cs index dbc2078..c39e6c1 100644 --- a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerMapper.cs +++ b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerMapper.cs @@ -6,6 +6,6 @@ public class GetCustomerMapper : Profile { public GetCustomerMapper() { - base.CreateMap(); + base.CreateMap(); } } \ No newline at end of file diff --git a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs index 2c8f2a0..62f2bd0 100644 --- a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs +++ b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerQueryHandler.cs @@ -7,17 +7,17 @@ namespace Microservice.Customer.Api.MediatR.GetCustomer; public class GetCustomerQueryHandler(ICustomerRepository customerRepository, IMapper mapper) : IRequestHandler { - private ICustomerRepository _customerRepository { get; set; } = customerRepository ; + private ICustomerRepository _customerRepository { get; set; } = customerRepository; private IMapper _mapper { get; set; } = mapper; - + public async Task Handle(GetCustomerRequest request, CancellationToken cancellationToken) - { + { var customer = await _customerRepository.ByIdAsync(request.Id); if (customer == null) { throw new NotFoundException("Customer not found."); - } + } - return _mapper.Map(customer); + return _mapper.Map(customer); } } \ No newline at end of file diff --git a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerResponse.cs b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerResponse.cs index 52b9951..a748a1d 100644 --- a/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerResponse.cs +++ b/Microservice.Customer.Api/Mediatr/GetCustomer/GetCustomerResponse.cs @@ -1,3 +1,3 @@ namespace Microservice.Customer.Api.MediatR.GetCustomer; - + public record GetCustomerResponse(string Email, string Surname, string FirstName); \ No newline at end of file diff --git a/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerCommandHandler.cs b/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerCommandHandler.cs index e06c746..ccc0609 100644 --- a/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerCommandHandler.cs +++ b/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerCommandHandler.cs @@ -6,7 +6,7 @@ namespace Microservice.Customer.Api.MediatR.AddCustomer; -public class UpdateCustomerCommandHandler(ICustomerRepository customerRepository, +public class UpdateCustomerCommandHandler(ICustomerRepository customerRepository, IMapper mapper, ICustomerHttpAccessor customerHttpAccessor) : IRequestHandler { private ICustomerRepository _customerRepository { get; set; } = customerRepository; @@ -19,7 +19,7 @@ public async Task Handle(UpdateCustomerRequest updateCus if (existingCustomer == null) throw new NotFoundException("Customer not found."); - existingCustomer = _mapper.Map(updateCustomerRequest, existingCustomer); + existingCustomer = _mapper.Map(updateCustomerRequest, existingCustomer); await _customerRepository.UpdateAsync(existingCustomer); diff --git a/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerMapper.cs b/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerMapper.cs index a8207ea..7242a33 100644 --- a/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerMapper.cs +++ b/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerMapper.cs @@ -7,7 +7,7 @@ public class UpdateCustomerMapper : Profile public UpdateCustomerMapper() { base.CreateMap() - .ForMember(dest => dest.Id, opt => opt.Ignore()) + .ForMember(dest => dest.Id, opt => opt.Ignore()) .ForMember(x => x.Email, act => act.MapFrom(src => src.Email)) .ForMember(x => x.Surname, act => act.MapFrom(src => src.Surname)) .ForMember(x => x.FirstName, act => act.MapFrom(src => src.FirstName)) diff --git a/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerValidator.cs b/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerValidator.cs index 5dff579..7fff4d5 100644 --- a/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerValidator.cs +++ b/Microservice.Customer.Api/Mediatr/UpdateCustomer/UpdateCustomerValidator.cs @@ -20,9 +20,10 @@ public UpdateCustomerValidator(ICustomerRepository customerRepository) RuleFor(updateCustomerRequest => updateCustomerRequest.Email) .NotEmpty().WithMessage("Email is required.") .Length(8, 150).WithMessage("Email length between 8 and 150.") - .EmailAddress().WithMessage("Invalid Email."); + .EmailAddress().WithMessage("Invalid Email."); - RuleFor(updateCustomerRequest => updateCustomerRequest).MustAsync(async (updateCustomerRequest, cancellation) => { + RuleFor(updateCustomerRequest => updateCustomerRequest).MustAsync(async (updateCustomerRequest, cancellation) => + { return await EmailExists(updateCustomerRequest); }).WithMessage("Customer with this email already exists"); diff --git a/Microservice.Customer.Api/Middleware/ExceptionHandlingMiddleware.cs b/Microservice.Customer.Api/Middleware/ExceptionHandlingMiddleware.cs index e2c09f4..61e3d91 100644 --- a/Microservice.Customer.Api/Middleware/ExceptionHandlingMiddleware.cs +++ b/Microservice.Customer.Api/Middleware/ExceptionHandlingMiddleware.cs @@ -1,6 +1,6 @@ using FluentValidation; using FluentValidation.Results; -using Microservice.Customer.Api.Helpers.Exceptions; +using Microservice.Customer.Api.Helpers.Exceptions; using System.Net; using System.Text.Json; using static Microservice.Customer.Api.Helpers.Enums; diff --git a/Microservice.Customer.Api/Migrations/20240727104340_create-table-default-data.cs b/Microservice.Customer.Api/Migrations/20240727104340_create-table-default-data.cs index 3efb700..b6ad4a8 100644 --- a/Microservice.Customer.Api/Migrations/20240727104340_create-table-default-data.cs +++ b/Microservice.Customer.Api/Migrations/20240727104340_create-table-default-data.cs @@ -1,5 +1,4 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Migrations; #nullable disable diff --git a/Microservice.Customer.Api/Program.cs b/Microservice.Customer.Api/Program.cs index 7a15cfc..3c57375 100644 --- a/Microservice.Customer.Api/Program.cs +++ b/Microservice.Customer.Api/Program.cs @@ -1,6 +1,5 @@ using Microservice.Customer.Api.Endpoints; using Microservice.Customer.Api.Extensions; -using Microservice.Customer.Api.Middleware; var builder = WebApplication.CreateBuilder(args); @@ -13,7 +12,7 @@ builder.Services.ConfigureDatabaseContext(builder.Configuration); builder.Services.ConfigureExceptionHandling(); builder.Services.ConfigureJwt(); -builder.Services.ConfigureMediatr(); +builder.Services.ConfigureMediatr(); builder.Services.ConfigureSwagger(); var app = builder.Build();