Skip to content

Commit

Permalink
Merge pull request #13 from HammerheadShark666/run-vs-code-cleanup
Browse files Browse the repository at this point in the history
Run vs code cleanup
  • Loading branch information
HammerheadShark666 authored Aug 26, 2024
2 parents 4b84740 + 91f10f3 commit 7e4a358
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 87 deletions.
10 changes: 6 additions & 4 deletions Microservice.Customer.Api.Test.Unit/GetCustomerMediatrTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class GetCustomerMediatrTests
{
private readonly Mock<ICustomerRepository> customerRepositoryMock = new();
private readonly Mock<ICustomerHttpAccessor> customerHttpAccessorMock = new();
private readonly Mock<ILogger<GetCustomerQueryHandler>> loggerMock = new();
private readonly ServiceCollection services = new();
private ServiceProvider serviceProvider;
private IMediator mediator;
Expand Down Expand Up @@ -67,9 +66,12 @@ public async Task Get_customer_return_customer()
var actualResult = await mediator.Send(getCustomerRequest);
var expectedResult = new GetCustomerResponse(email, surname, firstName);

Assert.That(actualResult.Email, Is.EqualTo(expectedResult.Email));
Assert.That(actualResult.Surname, Is.EqualTo(expectedResult.Surname));
Assert.That(actualResult.FirstName, Is.EqualTo(expectedResult.FirstName));
Assert.Multiple(() =>
{
Assert.That(actualResult.Email, Is.EqualTo(expectedResult.Email));
Assert.That(actualResult.Surname, Is.EqualTo(expectedResult.Surname));
Assert.That(actualResult.FirstName, Is.EqualTo(expectedResult.FirstName));
});
}

[Test]
Expand Down
33 changes: 19 additions & 14 deletions Microservice.Customer.Api.Test.Unit/UpdateCustomerMediatrTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microservice.Customer.Api.Data.Repository.Interfaces;
using Microservice.Customer.Api.Helpers;
using Microservice.Customer.Api.Helpers.Interfaces;
using Microservice.Customer.Api.MediatR.AddCustomer;
using Microservice.Customer.Api.Mediatr.UpdateCustomer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
Expand All @@ -18,7 +18,6 @@ public class UpdateCustomerMediatrTests
{
private readonly Mock<ICustomerRepository> customerRepositoryMock = new();
private readonly Mock<ICustomerHttpAccessor> customerHttpAccessorMock = new();
private readonly Mock<ILogger<UpdateCustomerCommandHandler>> loggerMock = new();
private readonly ServiceCollection services = new();
private ServiceProvider serviceProvider;
private IMediator mediator;
Expand Down Expand Up @@ -160,13 +159,16 @@ public void Customer_not_updated_invalid_surname_first_name_return_exception_fai
await mediator.Send(updateCustomerRequest);
});

Assert.That(validationException.Errors.Count, Is.EqualTo(2));
Assert.That(validationException.Errors.ElementAt(0).ErrorMessage, Is.EqualTo("Surname length between 1 and 30."));
Assert.That(validationException.Errors.ElementAt(1).ErrorMessage, Is.EqualTo("First name length between 1 and 30."));
Assert.Multiple(() =>
{
Assert.That(validationException.Errors.Count, Is.EqualTo(2));
Assert.That(validationException.Errors.ElementAt(0).ErrorMessage, Is.EqualTo("Surname length between 1 and 30."));
Assert.That(validationException.Errors.ElementAt(1).ErrorMessage, Is.EqualTo("First name length between 1 and 30."));
});
}

[Test]
public void Customer_not_updated_no_email_surname_firstname_return_exception_fail_message()
public void Customer_not_updated_no_email_surname_first_name_return_exception_fail_message()
{
customerRepositoryMock
.Setup(x => x.ExistsAsync("[email protected]"))
Expand All @@ -179,13 +181,16 @@ public void Customer_not_updated_no_email_surname_firstname_return_exception_fai
await mediator.Send(updateCustomerRequest);
});

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(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."));
Assert.Multiple(() =>
{
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(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."));
});
}
}
9 changes: 3 additions & 6 deletions Microservice.Customer.Api/Data/Context/CustomerDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using Microservice.Customer.Api.Data.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Microservice.Customer.Api.Data.Contexts;
namespace Microservice.Customer.Api.Data.Context;

public class CustomerDbContext : DbContext
public class CustomerDbContext(DbContextOptions<CustomerDbContext> options) : DbContext(options)
{
public CustomerDbContext(DbContextOptions<CustomerDbContext> options) : base(options) { }

public DbSet<Domain.Customer> Customer { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down
6 changes: 3 additions & 3 deletions Microservice.Customer.Api/Data/Context/DefaultData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public class DefaultData
{
public static List<Domain.Customer> GetCustomerDefaultData()
{
return new List<Domain.Customer>()
{
return
[
CreateCustomer(new Guid("6c84d0a3-0c0c-435f-9ae0-4de09247ee15"), "Intergration_Test", "Intergration_Test", "[email protected]"),
CreateCustomer(new Guid("929eaf82-e4fd-4efe-9cae-ce4d7e32d159"), "Intergration_Test2", "Intergration_Test2", "[email protected]")
};
];
}

private static Domain.Customer CreateCustomer(Guid id, string surname, string firstName, string email)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microservice.Customer.Api.Data.Contexts;
using Microservice.Customer.Api.Data.Context;
using Microservice.Customer.Api.Data.Repository.Interfaces;
using Microsoft.EntityFrameworkCore;

Expand Down
14 changes: 7 additions & 7 deletions Microservice.Customer.Api/Endpoints/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microservice.Customer.Api.Extensions;
using Microservice.Customer.Api.Helpers.Exceptions;
using Microservice.Customer.Api.Helpers.Interfaces;
using Microservice.Customer.Api.MediatR.AddCustomer;
using Microservice.Customer.Api.Mediatr.UpdateCustomer;
using Microservice.Customer.Api.MediatR.GetCustomer;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -16,9 +16,9 @@ namespace Microservice.Customer.Api.Endpoints;

public static class Endpoints
{
public static void ConfigureRoutes(this WebApplication app, ConfigurationManager configuration)
public static void ConfigureRoutes(this WebApplication webApplication)
{
var customerGroup = app.MapGroup("v{version:apiVersion}/customers").WithTags("customers");
var customerGroup = webApplication.MapGroup("v{version:apiVersion}/customers").WithTags("customers");

customerGroup.MapGet("/logged-in", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
async ([FromServices] IMediator mediator, ICustomerHttpAccessor customerHttpAccessor) =>
Expand All @@ -30,13 +30,13 @@ public static void ConfigureRoutes(this WebApplication app, ConfigurationManager
.Produces<BadRequestException>((int)HttpStatusCode.BadRequest)
.Produces<ValidationException>((int)HttpStatusCode.BadRequest)
.WithName("GetCustomer")
.WithApiVersionSet(app.GetApiVersionSet())
.WithApiVersionSet(webApplication.GetApiVersionSet())
.MapToApiVersion(new ApiVersion(1, 0))
.WithOpenApi(x => new OpenApiOperation(x)
{
Summary = "Get a customer based on id.",
Description = "Gets a customer based on its id.",
Tags = new List<OpenApiTag> { new() { Name = "Microservice Customer System - Customers" } }
Tags = [new() { Name = "Microservice Customer System - Customers" }]
});

customerGroup.MapPut("/update", [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
Expand All @@ -54,14 +54,14 @@ public static void ConfigureRoutes(this WebApplication app, ConfigurationManager
.Produces<ArgumentException>((int)HttpStatusCode.BadRequest)
.Produces<NotFoundException>((int)HttpStatusCode.BadRequest)
.WithName("UpdateCustomer")
.WithApiVersionSet(app.GetApiVersionSet())
.WithApiVersionSet(webApplication.GetApiVersionSet())
.MapToApiVersion(new ApiVersion(1, 0))
.RequireAuthorization()
.WithOpenApi(x => new OpenApiOperation(x)
{
Summary = "Update customer details.",
Description = "Updates a customers details.",
Tags = new List<OpenApiTag> { new() { Name = "Microservice Customer System - Customers" } }
Tags = [new() { Name = "Microservice Customer System - Customers" }]
});
}
}
20 changes: 10 additions & 10 deletions Microservice.Customer.Api/Extensions/AppExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace Microservice.Customer.Api.Extensions;

public static class AppExtensions
{
public static void ConfigureSwagger(this WebApplication app)
public static void ConfigureSwagger(this WebApplication webApplication)
{
if (app.Environment.IsDevelopment())
if (webApplication.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
webApplication.UseSwagger();
webApplication.UseSwaggerUI(options =>
{
options.ConfigObject.AdditionalItems.Add("syntaxHighlight", false);

var descriptions = app.DescribeApiVersions();
var descriptions = webApplication.DescribeApiVersions();

// Build a swagger endpoint for each discovered API version
foreach (var description in descriptions)
Expand All @@ -28,19 +28,19 @@ public static void ConfigureSwagger(this WebApplication app)
}
}

public static ApiVersionSet GetApiVersionSet(this WebApplication app)
public static ApiVersionSet GetApiVersionSet(this WebApplication webApplication)
{
return app.NewApiVersionSet()
return webApplication.NewApiVersionSet()
.HasApiVersion(new ApiVersion(1))
.ReportApiVersions()
.Build();
}

public static void ConfigureMiddleware(this WebApplication app)
public static void ConfigureMiddleware(this WebApplication webApplication)
{
if (!app.Environment.IsDevelopment())
if (!webApplication.Environment.IsDevelopment())
{
app.UseMiddleware<ExceptionHandlingMiddleware>();
webApplication.UseMiddleware<ExceptionHandlingMiddleware>();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using Asp.Versioning;
using FluentValidation;
using MediatR;
using Microservice.Customer.Api.Address.Api.Extensions;
using Microservice.Customer.Api.Data.Contexts;
using Microservice.Customer.Api.Data.Context;
using Microservice.Customer.Api.Data.Repository;
using Microservice.Customer.Api.Data.Repository.Interfaces;
using Microservice.Customer.Api.Helpers;
Expand Down
2 changes: 1 addition & 1 deletion Microservice.Customer.Api/Extensions/JwtExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace Microservice.Customer.Api.Address.Api.Extensions;
namespace Microservice.Customer.Api.Extensions;

public static class JwtExtensions
{
Expand Down
8 changes: 2 additions & 6 deletions Microservice.Customer.Api/Helpers/CustomerHttpAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace Microservice.Customer.Api.Helpers;

public class CustomerHttpAccessor : Interfaces.ICustomerHttpAccessor
public class CustomerHttpAccessor(IHttpContextAccessor accessor) : Interfaces.ICustomerHttpAccessor
{
private readonly IHttpContextAccessor _accessor;
public CustomerHttpAccessor(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
private readonly IHttpContextAccessor _accessor = accessor;

public Guid CustomerId => new(_accessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier));
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

namespace Microservice.Customer.Api.Helpers.Swagger;

public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions>
public class ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider _provider;

public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider) => _provider = provider;
private readonly IApiVersionDescriptionProvider _provider = provider;

public void Configure(SwaggerGenOptions options)
{
Expand Down
9 changes: 2 additions & 7 deletions Microservice.Customer.Api/Helpers/ValidatorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace Microservice.Customer.Api.Helpers;

public class ValidatorBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
public class ValidatorBehavior<TRequest, TResponse>(IEnumerable<IValidator<TRequest>> validators) : IPipelineBehavior<TRequest, TResponse> where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator<TRequest>> _validators;

public ValidatorBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
private readonly IEnumerable<IValidator<TRequest>> _validators = validators;

public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@

namespace Microservice.Customer.Api.MediatR.GetCustomer;

public class GetCustomerValidator : AbstractValidator<GetCustomerRequest>
public class GetCustomerValidator(ICustomerRepository customerRepository) : AbstractValidator<GetCustomerRequest>
{
private readonly ICustomerRepository _customerRepository;

public GetCustomerValidator(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
private readonly ICustomerRepository _customerRepository = customerRepository;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microservice.Customer.Api.Helpers.Exceptions;
using Microservice.Customer.Api.Helpers.Interfaces;

namespace Microservice.Customer.Api.MediatR.AddCustomer;
namespace Microservice.Customer.Api.Mediatr.UpdateCustomer;

public class UpdateCustomerCommandHandler(ICustomerRepository customerRepository,
ILogger<UpdateCustomerCommandHandler> logger,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using AutoMapper;

namespace Microservice.Customer.Api.MediatR.AddCustomer;
namespace Microservice.Customer.Api.Mediatr.UpdateCustomer;

public class UpdateCustomerMapper : Profile
{
public UpdateCustomerMapper()
{
base.CreateMap<UpdateCustomerRequest, Microservice.Customer.Api.Domain.Customer>()
CreateMap<UpdateCustomerRequest, Domain.Customer>()
.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))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using MediatR;

namespace Microservice.Customer.Api.MediatR.AddCustomer;
namespace Microservice.Customer.Api.Mediatr.UpdateCustomer;

public record UpdateCustomerRequest(Guid Id, string Email, string Surname, string FirstName) : IRequest<UpdateCustomerResponse>;
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Microservice.Customer.Api.MediatR.AddCustomer;
namespace Microservice.Customer.Api.Mediatr.UpdateCustomer;

public record UpdateCustomerResponse(string message);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FluentValidation;
using Microservice.Customer.Api.Data.Repository.Interfaces;

namespace Microservice.Customer.Api.MediatR.AddCustomer;
namespace Microservice.Customer.Api.Mediatr.UpdateCustomer;

public class UpdateCustomerValidator : AbstractValidator<UpdateCustomerRequest>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

namespace Microservice.Customer.Api.Middleware;

internal sealed class ExceptionHandlingMiddleware : IMiddleware
internal sealed class ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger) : IMiddleware
{
private readonly ILogger<ExceptionHandlingMiddleware> _logger;

public ExceptionHandlingMiddleware(ILogger<ExceptionHandlingMiddleware> logger) => _logger = logger;
private readonly ILogger<ExceptionHandlingMiddleware> _logger = logger;

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
Expand All @@ -26,7 +24,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
}
}

private Task HandleExceptionAsync(HttpContext context, Exception exception)
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var httpStatusCode = HttpStatusCode.InternalServerError;

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <auto-generated />
using System;
using Microservice.Customer.Api.Data.Contexts;
using Microservice.Customer.Api.Data.Context;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down
Loading

0 comments on commit 7e4a358

Please sign in to comment.