Skip to content

Commit

Permalink
Migrate to .NET 8 (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschumpr authored Dec 1, 2023
2 parents a071f90 + 91993d4 commit e1f94bd
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 95 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

- name: Build solution
run: dotnet build BDMS.sln -c Release /warnaserror
Expand Down Expand Up @@ -52,7 +52,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 8.0.x

- name: Build solution
run: dotnet build BDMS.sln -c Release /warnaserror
Expand Down
5 changes: 2 additions & 3 deletions src/api/Authentication/BasicAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ public class BasicAuthenticationHandler : AuthenticationHandler<AuthenticationSc
/// <param name="options">The monitor for the options instance.</param>
/// <param name="logger">The <see cref="ILoggerFactory"/>.</param>
/// <param name="encoder">The <see cref="System.Text.Encodings.Web.UrlEncoder"/>.</param>
/// <param name="clock">The <see cref="ISystemClock"/>.</param>
/// <exception cref="ArgumentNullException">If <paramref name="dbContext"/> is <c>null</c>.</exception>
public BasicAuthenticationHandler(BdmsContext dbContext, IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
public BasicAuthenticationHandler(BdmsContext dbContext, IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder)
: base(options, logger, encoder)
{
this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
}
Expand Down
37 changes: 15 additions & 22 deletions src/api/BDMS.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AnalysisLevel>6.0-latest</AnalysisLevel>
<AnalysisLevel>8.0-all</AnalysisLevel>
<NoWarn>CS1591,CS8618,CS8620</NoWarn>
<Product>BDMS</Product>
<Authors>GeoWerkstatt GmbH</Authors>
Expand All @@ -18,32 +17,26 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..</DockerfileContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.104.13" />
<PackageReference Include="AWSSDK.S3" Version="3.7.304.1" />
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="EFCore.BulkExtensions.PostgreSql" Version="6.6.5" />
<PackageReference Include="EFCore.BulkExtensions.PostgreSql" Version="8.0.0" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="6.0.7" />
<PackageReference Include="Npgsql.NetTopologySuite" Version="6.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.4.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite" Version="8.0.0" />
<PackageReference Include="Npgsql.NetTopologySuite" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" />
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(AssemblyName).Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

</Project>
</Project>
3 changes: 3 additions & 0 deletions src/api/BdmsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public class BdmsContext : DbContext
public BdmsContext(DbContextOptions options)
: base(options)
{
#pragma warning disable CS0618 // Type or member is obsolete, however they do not plan on removing it any time soon.
// TODO: https://github.com/geoadmin/suite-bdms/issues/851
NpgsqlConnection.GlobalTypeMapper.MapEnum<Role>();
#pragma warning restore CS0618 // Type or member is obsolete, however they do not plan on removing it any time soon.
}

/// <summary>
Expand Down
20 changes: 10 additions & 10 deletions src/api/BdmsContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,16 +698,16 @@ FieldMeasurement SeededFieldMeasurements(Observation observation)
context.SaveChanges();

// Sync all database sequences
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.workgroups', 'id_wgp'), {workgroup_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.borehole', 'id_bho'), {borehole_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.events', 'id_evs'), {event_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.feedbacks', 'id_feb'), {feedback_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.files', 'id_fil'), {file_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.stratigraphy', 'id_sty'), {stratigraphy_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.layer', 'id_lay'), {layer_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.workflow', 'id_wkf'), {workflow_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.observation', 'id'), {observation_ids - 1})");
context.Database.ExecuteSqlRaw($"SELECT setval(pg_get_serial_sequence('bdms.hydrotest_result', 'id'), {hydrotestResult_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.workgroups', 'id_wgp'), {workgroup_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.borehole', 'id_bho'), {borehole_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.events', 'id_evs'), {event_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.feedbacks', 'id_feb'), {feedback_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.files', 'id_fil'), {file_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.stratigraphy', 'id_sty'), {stratigraphy_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.layer', 'id_lay'), {layer_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.workflow', 'id_wkf'), {workflow_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.observation', 'id'), {observation_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.hydrotest_result', 'id'), {hydrotestResult_ids - 1})");
}
}
#pragma warning restore CA1505
2 changes: 1 addition & 1 deletion src/api/BoreholeFileUploadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task UploadFileAndLinkToBorehole(IFormFile file, int boreholeId)
using (SHA256 sha256Hash = SHA256.Create())
{
using Stream stream = file.OpenReadStream();
byte[] hashBytes = sha256Hash.ComputeHash(stream);
byte[] hashBytes = await sha256Hash.ComputeHashAsync(stream).ConfigureAwait(false);
base64Hash = Convert.ToBase64String(hashBytes);
}

Expand Down
47 changes: 22 additions & 25 deletions src/api/Controllers/CodeListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,29 @@ public async Task<IEnumerable<Codelist>> GetAsync(string? schema = null, [FromQu
codeLists = codeLists.Where(c => c.Schema == schema);
}

if (testKindIds != null)
if (testKindIds?.Length > 0)
{
if (testKindIds.Any())
List<int> hydrotestResultGeolcodes = new();
List<int> flowDirectionGeolCodes = new();
List<int> evaluationMethodIds = new();

Array.ForEach(testKindIds, testKindId =>
{
List<int> hydrotestResultGeolcodes = new();
List<int> flowDirectionGeolCodes = new();
List<int> evaluationMethodIds = new();

Array.ForEach(testKindIds, testKindId =>
{
// Get the Geolcode associated with the TestKindId.
int testKindGeolCode = context.Codelists.SingleOrDefault(c => c.Id == testKindId)?.Geolcode ?? 0;

// Get the lists of Geolcodes from the HydroCodeLookup based on the testKindGeolCode.
hydrotestResultGeolcodes.AddRange(HydroCodeLookup.HydrotestResultParameterOptions.TryGetValue(testKindGeolCode, out List<int>? tempHRIds) ? tempHRIds : new List<int>());
flowDirectionGeolCodes.AddRange(HydroCodeLookup.HydrotestFlowDirectionOptions.TryGetValue(testKindGeolCode, out List<int>? tempFDIds) ? tempFDIds : new List<int>());
evaluationMethodIds.AddRange(HydroCodeLookup.HydrotestEvaluationMethodOptions.TryGetValue(testKindGeolCode, out List<int>? tempEMIds) ? tempEMIds : new List<int>());
});

// Return the Codelists where the Codelist's Geolcode matches any of the compatible geolcodes form the HydroCodeLookup.
codeLists = codeLists.Where(c =>
c.Geolcode != null &&
((c.Schema == HydrogeologySchemas.HydrotestResultParameterSchema && hydrotestResultGeolcodes.Contains(c.Geolcode.Value)) ||
(c.Schema == HydrogeologySchemas.FlowdirectionSchema && flowDirectionGeolCodes.Contains(c.Geolcode.Value)) ||
(c.Schema == HydrogeologySchemas.EvaluationMethodSchema && evaluationMethodIds.Contains(c.Geolcode.Value))));
}
// Get the Geolcode associated with the TestKindId.
int testKindGeolCode = context.Codelists.SingleOrDefault(c => c.Id == testKindId)?.Geolcode ?? 0;

// Get the lists of Geolcodes from the HydroCodeLookup based on the testKindGeolCode.
hydrotestResultGeolcodes.AddRange(HydroCodeLookup.HydrotestResultParameterOptions.TryGetValue(testKindGeolCode, out List<int>? tempHRIds) ? tempHRIds : new List<int>());
flowDirectionGeolCodes.AddRange(HydroCodeLookup.HydrotestFlowDirectionOptions.TryGetValue(testKindGeolCode, out List<int>? tempFDIds) ? tempFDIds : new List<int>());
evaluationMethodIds.AddRange(HydroCodeLookup.HydrotestEvaluationMethodOptions.TryGetValue(testKindGeolCode, out List<int>? tempEMIds) ? tempEMIds : new List<int>());
});

// Return the Codelists where the Codelist's Geolcode matches any of the compatible geolcodes form the HydroCodeLookup.
codeLists = codeLists.Where(c =>
c.Geolcode != null &&
((c.Schema == HydrogeologySchemas.HydrotestResultParameterSchema && hydrotestResultGeolcodes.Contains(c.Geolcode.Value)) ||
(c.Schema == HydrogeologySchemas.FlowdirectionSchema && flowDirectionGeolCodes.Contains(c.Geolcode.Value)) ||
(c.Schema == HydrogeologySchemas.EvaluationMethodSchema && evaluationMethodIds.Contains(c.Geolcode.Value))));
}

return await codeLists.AsNoTracking().ToListAsync().ConfigureAwait(false);
Expand Down Expand Up @@ -120,6 +117,6 @@ public async Task<ContentResult> DownloadCsvAsync(CancellationToken cancellation
cancellationToken).ConfigureAwait(false);

Response.Headers.ContentDisposition = "attachment; filename=codelist_export.csv";
return Content(await reader.ReadToEndAsync().ConfigureAwait(false), "text/csv", Encoding.UTF8);
return Content(await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false), "text/csv", Encoding.UTF8);
}
}
8 changes: 4 additions & 4 deletions src/api/Controllers/HydrotestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private async Task<List<Codelist>> GetCodelists(List<int> codelistIds)

private bool AreCodelistIdsPresent(Hydrotest hydrotest)
{
return hydrotest.CodelistIds != null && hydrotest.CodelistIds.Any();
return hydrotest.CodelistIds?.Count > 0;
}

private bool AreHydrotestCodelistsCompatible(Hydrotest hydrotest)
Expand All @@ -153,7 +153,7 @@ private bool AreHydrotestCodelistsCompatible(Hydrotest hydrotest)
.ToList();

// If there are HydrotestResults, check if the ParameterIds in the results are compatible.
if (hydrotest.HydrotestResults?.Any() == true && testKindGeolCodes.Any())
if (hydrotest.HydrotestResults?.Count > 0 && testKindGeolCodes.Count > 0)
{
var compatibleParameterIds = GetCompatibleCodelistIds(testKindGeolCodes, HydrogeologySchemas.HydrotestResultParameterSchema, HydroCodeLookup.HydrotestResultParameterOptions);
if (!hydrotest.HydrotestResults.All(r => compatibleParameterIds.Contains(r.ParameterId)))
Expand All @@ -165,7 +165,7 @@ private bool AreHydrotestCodelistsCompatible(Hydrotest hydrotest)
var compatibleCodelistIds = new List<int>();

// If there are CodelistIds, find the compatible CodelistIds for the flow direction and evaluation method options.
if (hydrotest.CodelistIds?.Any() == true && testKindGeolCodes.Any())
if (hydrotest.CodelistIds?.Count > 0 && testKindGeolCodes.Count > 0)
{
compatibleCodelistIds.AddRange(hydrotestKindCodelistIds);
compatibleCodelistIds.AddRange(GetCompatibleCodelistIds(testKindGeolCodes, HydrogeologySchemas.FlowdirectionSchema, HydroCodeLookup.HydrotestFlowDirectionOptions));
Expand All @@ -191,7 +191,7 @@ private List<int> GetCompatibleCodelistIds(List<int> testKindGeolCodes, string s
}
});

if (compatibleGeolCodes.Any())
if (compatibleGeolCodes.Count > 0)
{
return compatibleGeolCodes.Distinct().ToList();
}
Expand Down
4 changes: 2 additions & 2 deletions src/api/Controllers/UploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public async Task<ActionResult<int>> UploadFileAsync(int workgroupId, IFormFile
// Add attachments to borehole.
if (attachments != null)
{
var boreholeImportsWithAttachments = boreholeImports.Where(x => x.Attachments?.Any() == true).ToList();
var boreholeImportsWithAttachments = boreholeImports.Where(x => x.Attachments?.Length > 0).ToList();
foreach (var boreholeImport in boreholeImportsWithAttachments)
{
var attachmentFileNames = boreholeImport.Attachments?.Split(",").Select(s => s.Replace(" ", "", StringComparison.InvariantCulture)).ToList();
Expand All @@ -174,7 +174,7 @@ public async Task<ActionResult<int>> UploadFileAsync(int workgroupId, IFormFile
}

// Add lithology imports if provided
if (lithologyImports.Any())
if (lithologyImports.Count > 0)
{
// Get the kind id of a lithostratigraphy.
var lithoStratiKindId = context.Codelists.Single(cl => cl.Schema == "layer_kind" && cl.IsDefault == true).Id;
Expand Down
8 changes: 4 additions & 4 deletions src/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS development
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS development
ENV ASPNETCORE_ENVIRONMENT=Development
WORKDIR /src

# Install missing packages
RUN apt-get -y update
RUN apt-get -y install git vim curl htop
RUN dotnet tool install --global dotnet-ef --version 6.0.7
RUN dotnet tool install --global dotnet-ef --version 8.0.0
ENV PATH $PATH:/root/.dotnet/tools

# Restore dependencies and tools
Expand All @@ -14,7 +14,7 @@ RUN dotnet restore

ENTRYPOINT dotnet watch run --no-launch-profile

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG VERSION
ARG REVISION
WORKDIR /src
Expand All @@ -32,7 +32,7 @@ RUN dotnet publish \
-p:SourceRevisionId=${REVISION} \
-o /app/publish

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS deploy
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS deploy
ENV ASPNETCORE_ENVIRONMENT=Production
WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion src/api/Models/Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ public class Workflow : IIdentifyable
/// <summary>
/// Gets or sets the <see cref="Role"/>.
/// </summary>
[Column("id_rol_fk", TypeName = "int")]
[Column("id_rol_fk", TypeName = "integer")]
public Role? Role { get; set; }
}
30 changes: 11 additions & 19 deletions tests/BDMS.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<RootNamespace>BDMS</RootNamespace>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<NoWarn>CS1591,CS8618,CS8620,CA1001,CA1014,CS8625,CA2000,CA2007</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.104.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.7">
<PackageReference Include="AWSSDK.S3" Version="3.7.304.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\api\BDMS.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="TestData\boreholes_not_all_have_attachments.csv">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand Down Expand Up @@ -163,5 +156,4 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
</Project>
Loading

0 comments on commit e1f94bd

Please sign in to comment.