Skip to content

Commit

Permalink
Upgrade to .net 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aimenux committed Jun 4, 2024
1 parent dbae5ef commit 543701d
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 165 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,6 @@ MigrationBackup/

# Ionide (cross platform F# VS Code tools) working folder
.ionide/

# JetBrains folders
.idea/*
9 changes: 5 additions & 4 deletions Api/Api.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ben.Demystifier" Version="0.4.1" />
<PackageReference Include="FluentValidation.AspNetCore" Version="10.3.6" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

</Project>
17 changes: 8 additions & 9 deletions Api/Domain/Company.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace Api.Domain
namespace Api.Domain;

public class Company
{
public class Company
{
public string Name { get; set; }
public string Name { get; set; }

public string Address { get; set; }
public string Address { get; set; }

public string RegistrationNumber { get; set; }
public string RegistrationNumber { get; set; }

public CompanyStatus CompanyStatus { get; set; }
}
}
public CompanyStatus CompanyStatus { get; set; }
}
23 changes: 11 additions & 12 deletions Api/Domain/CompanyService.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using Api.Infrastructure;

namespace Api.Domain
namespace Api.Domain;

public class CompanyService : ICompanyService
{
public class CompanyService : ICompanyService
{
private readonly IProxy _proxy;
private readonly IProxy _proxy;

public CompanyService(IProxy proxy)
{
public CompanyService(IProxy proxy)
{
_proxy = proxy;
}

public async Task<Company> GetCompanyAsync(string registrationNumber, CancellationToken cancellationToken = default)
{
public async Task<Company> GetCompanyAsync(string registrationNumber, CancellationToken cancellationToken = default)
{
var company = await _proxy.GetCompanyAsync(registrationNumber, cancellationToken);
CheckEligibilityRules(company);
return company;
}

private static void CheckEligibilityRules(Company company)
{
private static void CheckEligibilityRules(Company company)
{
if (company.CompanyStatus != CompanyStatus.Active)
{
throw DomainException.InvalidCompanyStatus();
Expand All @@ -30,5 +30,4 @@ private static void CheckEligibilityRules(Company company)
throw DomainException.InvalidCompanyAddress();
}
}
}
}
}
83 changes: 41 additions & 42 deletions Api/Infrastructure/Proxy.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
using Api.Domain;

namespace Api.Infrastructure
namespace Api.Infrastructure;

public class Proxy : IProxy
{
public class Proxy : IProxy
public Task<Company> GetCompanyAsync(string registrationNumber, CancellationToken cancellationToken = default)
{
public Task<Company> GetCompanyAsync(string registrationNumber, CancellationToken cancellationToken = default)
{
var nextValue = Randomize.Next();
var nextValue = Randomize.Next();

return nextValue switch
return nextValue switch
{
< 300 => Task.FromResult(new Company
{
< 300 => Task.FromResult(new Company
{
Name = Randomize.RandomString(10),
RegistrationNumber = registrationNumber,
Address = Randomize.RandomString(20),
CompanyStatus = CompanyStatus.Active
}),
< 400 => Task.FromResult(new Company
{
Name = Randomize.RandomString(10),
RegistrationNumber = registrationNumber,
Address = Randomize.RandomString(20),
CompanyStatus = CompanyStatus.Delisted
}),
< 600 => Task.FromResult(new Company
{
Name = Randomize.RandomString(10),
RegistrationNumber = registrationNumber,
CompanyStatus = CompanyStatus.Active
}),
< 700 => throw InfrastructureException.PartnerWebServiceIsDown(),
< 800 => throw InfrastructureException.PartnerWebServiceIsTakingTooLongToRespond(),
_ => throw InfrastructureException.PartnerWebServiceReceivingTooManyRequests()
};
}
Name = Randomize.RandomString(10),
RegistrationNumber = registrationNumber,
Address = Randomize.RandomString(20),
CompanyStatus = CompanyStatus.Active
}),
< 400 => Task.FromResult(new Company
{
Name = Randomize.RandomString(10),
RegistrationNumber = registrationNumber,
Address = Randomize.RandomString(20),
CompanyStatus = CompanyStatus.Delisted
}),
< 600 => Task.FromResult(new Company
{
Name = Randomize.RandomString(10),
RegistrationNumber = registrationNumber,
CompanyStatus = CompanyStatus.Active
}),
< 700 => throw InfrastructureException.PartnerWebServiceIsDown(),
< 800 => throw InfrastructureException.PartnerWebServiceIsTakingTooLongToRespond(),
_ => throw InfrastructureException.PartnerWebServiceReceivingTooManyRequests()
};
}

public class Randomize
{
private static readonly Random Random = new(Guid.NewGuid().GetHashCode());
private sealed class Randomize
{
private static readonly Random Random = new(Guid.NewGuid().GetHashCode());

public static int Next() => Random.Next(1, 1000);
public static int Next() => Random.Next(1, 1000);

public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[Random.Next(s.Length)]).ToArray());
}
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[Random.Next(s.Length)]).ToArray());
}
}
}
}
11 changes: 5 additions & 6 deletions Api/Presentation/Validators/CompanyRequestDtoValidator.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Api.Presentation.ViewModels;
using FluentValidation;

namespace Api.Presentation.Validators
namespace Api.Presentation.Validators;

public class CompanyRequestDtoValidator : AbstractValidator<CompanyRequestDto>
{
public class CompanyRequestDtoValidator : AbstractValidator<CompanyRequestDto>
public CompanyRequestDtoValidator()
{
public CompanyRequestDtoValidator()
{
RuleFor(x => x.RegistrationNumber)
.NotEmpty()
.MinimumLength(5)
.MaximumLength(10);
}
}
}
}
19 changes: 9 additions & 10 deletions Api/Presentation/ViewModels/CompanyResponseDto.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using Api.Domain;

namespace Api.Presentation.ViewModels
namespace Api.Presentation.ViewModels;

public class CompanyResponseDto
{
public class CompanyResponseDto
{
public string Name { get; set; }
public string Name { get; set; }

public string Address { get; set; }
public string Address { get; set; }

public string RegistrationNumber { get; set; }
public string RegistrationNumber { get; set; }

public CompanyResponseDto(Company company)
{
public CompanyResponseDto(Company company)
{
Name = company.Name;
Address = company.Address;
RegistrationNumber = company.RegistrationNumber;
}
}
}
}
85 changes: 43 additions & 42 deletions Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,68 @@
using Api.Infrastructure;
using Api.Presentation.Middlewares;
using Api.Presentation.Validators;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Mvc;

namespace Api
namespace Api;

public class Startup
{
public class Startup
public Startup(IConfiguration configuration)
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
Configuration = configuration;
}

public IConfiguration Configuration { get; }
public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.ConfigureApiBehaviorOptions(options =>
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
options.InvalidModelStateResponseFactory = context =>
var problemDetails = new ValidationProblemDetails(context.ModelState);
return new BadRequestObjectResult(problemDetails)
{
var problemDetails = new ValidationProblemDetails(context.ModelState);
return new BadRequestObjectResult(problemDetails)
{
ContentTypes = { "application/problem+json" }
};
ContentTypes = { "application/problem+json" }
};
});
};
});

services.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<CompanyRequestDtoValidator>());
services.AddFluentValidationAutoValidation();
services.AddValidatorsFromAssemblyContaining<CompanyRequestDtoValidator>();

services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();

services.AddScoped<IProxy, Proxy>();
services.AddScoped<ICompanyService, CompanyService>();
}
services.AddScoped<IProxy, Proxy>();
services.AddScoped<ICompanyService, CompanyService>();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseDeveloperExceptionPage();
}

app.UseExceptionHandlingMiddleware();
app.UseExceptionHandlingMiddleware();

app.UseHttpsRedirection();
app.UseHttpsRedirection();

app.UseRouting();
app.UseRouting();

app.UseAuthorization();
app.UseAuthorization();

app.UseSwagger();
app.UseSwaggerUI();
app.UseSwagger();
app.UseSwaggerUI();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
1 change: 1 addition & 0 deletions ExceptionHandlingMiddlewareDemo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.github\workflows\ci.yml = .github\workflows\ci.yml
README.md = README.md
global.json = global.json
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{F13C9ADF-5755-4273-94C9-97C197B7CB8C}"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Exceptions are thrown by :

Exceptions are catched by the exception middleware and formatted using [problem details specification](https://datatracker.ietf.org/doc/html/rfc7807)

**`Tools`** : vs22, net 6.0, xunit
**`Tools`** : net 8.0, xunit
Loading

0 comments on commit 543701d

Please sign in to comment.