From b9448a37df7330f62e7afffa78ebc4f5c6ed9a5d Mon Sep 17 00:00:00 2001 From: Peter Liapin Date: Tue, 23 Jan 2024 18:26:59 +0530 Subject: [PATCH 1/6] First attempt to upgrade to .NET 8.0 --- .azure/azure-pipelines.yml | 4 ++-- .github/workflows/build_and_test.yml | 2 +- .github/workflows/build_test_and_publish.yml | 2 +- .../OnlineSales.Plugin.EmailSync.csproj | 2 +- .../OnlineSales.Plugin.ReverseProxy.csproj | 2 +- .../OnlineSales.Plugin.SendGrid.csproj | 2 +- .../Exceptions/AwsSnsException.cs | 5 ----- .../Exceptions/GetshoutoutException.cs | 5 ----- .../Exceptions/NotifyLkException.cs | 5 ----- .../Exceptions/SMSPluginException.cs | 5 ----- .../Exceptions/SmscException.cs | 5 ----- .../Exceptions/TwilioException.cs | 5 ----- .../Exceptions/UnknownCountryCodeException.cs | 5 ----- .../OnlineSales.Plugin.Sms.csproj | 2 +- .../OnlineSales.Plugin.TestPlugin.csproj | 2 +- .../OnlineSales.Plugin.Vsto.csproj | 2 +- .../Controllers/ActivityLogController.cs | 4 ++-- src/OnlineSales/Controllers/BaseController.cs | 8 ++++---- .../Controllers/UsersController.cs | 4 ++-- src/OnlineSales/Exceptions/EmailException.cs | 5 ----- .../MissingConfigurationException.cs | 5 ----- .../Exceptions/PluginDbContextException.cs | 5 ----- .../PluginDbContextNotFoundException.cs | 5 ----- .../PluginDbContextTooManyException.cs | 5 ----- src/OnlineSales/Exceptions/QueryException.cs | 5 ----- .../Exceptions/SyncEmailLogTaskException.cs | 5 ----- src/OnlineSales/OnlineSales.csproj | 20 +++++++++---------- .../Properties/launchSettings.json | 3 +-- .../Environment/TestAuthenticationHandler.cs | 4 ++-- .../OnlineSales.Tests.csproj | 4 ++-- 30 files changed, 33 insertions(+), 104 deletions(-) diff --git a/.azure/azure-pipelines.yml b/.azure/azure-pipelines.yml index 2e9e1067..ea985097 100644 --- a/.azure/azure-pipelines.yml +++ b/.azure/azure-pipelines.yml @@ -12,9 +12,9 @@ steps: displayName: 'docker compose -f ./docker-compose/docker-compose.tests.yml up -d' - task: UseDotNet@2 - displayName: 'install .net sdk 7.x' + displayName: 'install .net sdk 8.x' inputs: - version: 7.x + version: 8.x installationPath: $(Agent.ToolsDirectory)/dotnet - script: | diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 2921c280..c02e3edf 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -21,7 +21,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v3 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x - name: docker compose -f ./docker-compose/docker-compose.tests.yml up -d run: docker compose -f ./docker-compose/docker-compose.tests.yml up -d diff --git a/.github/workflows/build_test_and_publish.yml b/.github/workflows/build_test_and_publish.yml index 32bcb452..d81816e8 100644 --- a/.github/workflows/build_test_and_publish.yml +++ b/.github/workflows/build_test_and_publish.yml @@ -32,7 +32,7 @@ jobs: - name: Setup .NET uses: actions/setup-dotnet@v3 with: - dotnet-version: 7.0.x + dotnet-version: 8.0.x - name: Setup NuGet uses: nuget/setup-nuget@v1 diff --git a/plugins/OnlineSales.Plugin.EmailSync/OnlineSales.Plugin.EmailSync.csproj b/plugins/OnlineSales.Plugin.EmailSync/OnlineSales.Plugin.EmailSync.csproj index 162d8e53..2cf82f89 100644 --- a/plugins/OnlineSales.Plugin.EmailSync/OnlineSales.Plugin.EmailSync.csproj +++ b/plugins/OnlineSales.Plugin.EmailSync/OnlineSales.Plugin.EmailSync.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 true enable enable diff --git a/plugins/OnlineSales.Plugin.ReverseProxy/OnlineSales.Plugin.ReverseProxy.csproj b/plugins/OnlineSales.Plugin.ReverseProxy/OnlineSales.Plugin.ReverseProxy.csproj index 1260ad15..8d6da813 100644 --- a/plugins/OnlineSales.Plugin.ReverseProxy/OnlineSales.Plugin.ReverseProxy.csproj +++ b/plugins/OnlineSales.Plugin.ReverseProxy/OnlineSales.Plugin.ReverseProxy.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 enable true enable diff --git a/plugins/OnlineSales.Plugin.SendGrid/OnlineSales.Plugin.SendGrid.csproj b/plugins/OnlineSales.Plugin.SendGrid/OnlineSales.Plugin.SendGrid.csproj index 92fd4512..b7498cea 100644 --- a/plugins/OnlineSales.Plugin.SendGrid/OnlineSales.Plugin.SendGrid.csproj +++ b/plugins/OnlineSales.Plugin.SendGrid/OnlineSales.Plugin.SendGrid.csproj @@ -1,7 +1,7 @@ - net7.0 + net8.0 true enable enable diff --git a/plugins/OnlineSales.Plugin.Sms/Exceptions/AwsSnsException.cs b/plugins/OnlineSales.Plugin.Sms/Exceptions/AwsSnsException.cs index 8f325c9c..8b14cf15 100644 --- a/plugins/OnlineSales.Plugin.Sms/Exceptions/AwsSnsException.cs +++ b/plugins/OnlineSales.Plugin.Sms/Exceptions/AwsSnsException.cs @@ -27,10 +27,5 @@ public AwsSnsException(string? message, Exception? innerException) : base(message, innerException) { } - - protected AwsSnsException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.Sms/Exceptions/GetshoutoutException.cs b/plugins/OnlineSales.Plugin.Sms/Exceptions/GetshoutoutException.cs index cd722cad..07236ecb 100644 --- a/plugins/OnlineSales.Plugin.Sms/Exceptions/GetshoutoutException.cs +++ b/plugins/OnlineSales.Plugin.Sms/Exceptions/GetshoutoutException.cs @@ -22,9 +22,4 @@ public GetshoutoutException(string? message, Exception? innerException) : base(message, innerException) { } - - protected GetshoutoutException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.Sms/Exceptions/NotifyLkException.cs b/plugins/OnlineSales.Plugin.Sms/Exceptions/NotifyLkException.cs index 74511bb7..0ffd49d2 100644 --- a/plugins/OnlineSales.Plugin.Sms/Exceptions/NotifyLkException.cs +++ b/plugins/OnlineSales.Plugin.Sms/Exceptions/NotifyLkException.cs @@ -22,10 +22,5 @@ public NotifyLkException(string? message, Exception? innerException) : base(message, innerException) { } - - protected NotifyLkException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.Sms/Exceptions/SMSPluginException.cs b/plugins/OnlineSales.Plugin.Sms/Exceptions/SMSPluginException.cs index 721d643b..0e5d2aa5 100644 --- a/plugins/OnlineSales.Plugin.Sms/Exceptions/SMSPluginException.cs +++ b/plugins/OnlineSales.Plugin.Sms/Exceptions/SMSPluginException.cs @@ -27,10 +27,5 @@ public SmsPluginException(string? message, Exception? innerException) : base(message, innerException) { } - - protected SmsPluginException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.Sms/Exceptions/SmscException.cs b/plugins/OnlineSales.Plugin.Sms/Exceptions/SmscException.cs index f1038765..76d8865c 100644 --- a/plugins/OnlineSales.Plugin.Sms/Exceptions/SmscException.cs +++ b/plugins/OnlineSales.Plugin.Sms/Exceptions/SmscException.cs @@ -27,10 +27,5 @@ public SmscException(string? message, Exception? innerException) : base(message, innerException) { } - - protected SmscException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.Sms/Exceptions/TwilioException.cs b/plugins/OnlineSales.Plugin.Sms/Exceptions/TwilioException.cs index bfef540b..46073d01 100644 --- a/plugins/OnlineSales.Plugin.Sms/Exceptions/TwilioException.cs +++ b/plugins/OnlineSales.Plugin.Sms/Exceptions/TwilioException.cs @@ -22,10 +22,5 @@ public TwilioException(string? message, Exception? innerException) : base(message, innerException) { } - - protected TwilioException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.Sms/Exceptions/UnknownCountryCodeException.cs b/plugins/OnlineSales.Plugin.Sms/Exceptions/UnknownCountryCodeException.cs index 1fe8ac17..f818cf58 100644 --- a/plugins/OnlineSales.Plugin.Sms/Exceptions/UnknownCountryCodeException.cs +++ b/plugins/OnlineSales.Plugin.Sms/Exceptions/UnknownCountryCodeException.cs @@ -22,10 +22,5 @@ public UnknownCountryCodeException(string? message, Exception? innerException) : base(message, innerException) { } - - protected UnknownCountryCodeException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.Sms/OnlineSales.Plugin.Sms.csproj b/plugins/OnlineSales.Plugin.Sms/OnlineSales.Plugin.Sms.csproj index 796da52d..1f253e54 100644 --- a/plugins/OnlineSales.Plugin.Sms/OnlineSales.Plugin.Sms.csproj +++ b/plugins/OnlineSales.Plugin.Sms/OnlineSales.Plugin.Sms.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 true enable enable diff --git a/plugins/OnlineSales.Plugin.TestPlugin/OnlineSales.Plugin.TestPlugin.csproj b/plugins/OnlineSales.Plugin.TestPlugin/OnlineSales.Plugin.TestPlugin.csproj index 2ab6936c..eef39946 100644 --- a/plugins/OnlineSales.Plugin.TestPlugin/OnlineSales.Plugin.TestPlugin.csproj +++ b/plugins/OnlineSales.Plugin.TestPlugin/OnlineSales.Plugin.TestPlugin.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 true enable enable diff --git a/plugins/OnlineSales.Plugin.Vsto/OnlineSales.Plugin.Vsto.csproj b/plugins/OnlineSales.Plugin.Vsto/OnlineSales.Plugin.Vsto.csproj index a2e1d485..c2e2c051 100644 --- a/plugins/OnlineSales.Plugin.Vsto/OnlineSales.Plugin.Vsto.csproj +++ b/plugins/OnlineSales.Plugin.Vsto/OnlineSales.Plugin.Vsto.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 true enable enable diff --git a/src/OnlineSales/Controllers/ActivityLogController.cs b/src/OnlineSales/Controllers/ActivityLogController.cs index 3dac1195..7f836495 100644 --- a/src/OnlineSales/Controllers/ActivityLogController.cs +++ b/src/OnlineSales/Controllers/ActivityLogController.cs @@ -38,8 +38,8 @@ public virtual async Task>> Get([FromQu var qp = queryProviderFactory.BuildQueryProvider(); var result = await qp.GetResult(); - Response.Headers.Add(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); - Response.Headers.Add(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); + Response.Headers.Append(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); + Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); return Ok(mapper.Map>(result.Records)); } } diff --git a/src/OnlineSales/Controllers/BaseController.cs b/src/OnlineSales/Controllers/BaseController.cs index 6817daf3..88530741 100644 --- a/src/OnlineSales/Controllers/BaseController.cs +++ b/src/OnlineSales/Controllers/BaseController.cs @@ -104,8 +104,8 @@ public virtual async Task>> Get([FromQuery] string? query) var qp = queryProviderFactory.BuildQueryProvider(); var result = await qp.GetResult(); - Response.Headers.Add(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); - Response.Headers.Add(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); + Response.Headers.Append(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); + Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); var res = mapper.Map>(result.Records); RemoveSecondLevelObjects(res); return Ok(res); @@ -120,8 +120,8 @@ public virtual async Task>> Export([FromQuery] string? que var qp = queryProviderFactory.BuildQueryProvider(int.MaxValue); var result = await qp.GetResult(); - Response.Headers.Add(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); - Response.Headers.Add(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); + Response.Headers.Append(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); + Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); var res = mapper.Map>(result.Records); RemoveSecondLevelObjects(res); return Ok(res); diff --git a/src/OnlineSales/Controllers/UsersController.cs b/src/OnlineSales/Controllers/UsersController.cs index ef62a632..98403de6 100644 --- a/src/OnlineSales/Controllers/UsersController.cs +++ b/src/OnlineSales/Controllers/UsersController.cs @@ -38,8 +38,8 @@ public async Task> GetAll() { var allUsers = await userManager.Users.ToListAsync(); var resultsToClient = mapper.Map(allUsers).ToArray(); - Response.Headers.Add(ResponseHeaderNames.TotalCount, resultsToClient.Count().ToString()); - Response.Headers.Add(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); + Response.Headers.Append(ResponseHeaderNames.TotalCount, resultsToClient.Count().ToString()); + Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); return Ok(resultsToClient); } diff --git a/src/OnlineSales/Exceptions/EmailException.cs b/src/OnlineSales/Exceptions/EmailException.cs index 06c71ede..eecc6f40 100644 --- a/src/OnlineSales/Exceptions/EmailException.cs +++ b/src/OnlineSales/Exceptions/EmailException.cs @@ -22,9 +22,4 @@ public EmailException(string? message, Exception? innerException) : base(message, innerException) { } - - protected EmailException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/MissingConfigurationException.cs b/src/OnlineSales/Exceptions/MissingConfigurationException.cs index 7c78bb45..b10bf4ad 100644 --- a/src/OnlineSales/Exceptions/MissingConfigurationException.cs +++ b/src/OnlineSales/Exceptions/MissingConfigurationException.cs @@ -22,9 +22,4 @@ public MissingConfigurationException(string? message, Exception? innerException) : base(message, innerException) { } - - protected MissingConfigurationException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/PluginDbContextException.cs b/src/OnlineSales/Exceptions/PluginDbContextException.cs index 48f84969..12a877cc 100644 --- a/src/OnlineSales/Exceptions/PluginDbContextException.cs +++ b/src/OnlineSales/Exceptions/PluginDbContextException.cs @@ -25,10 +25,5 @@ public PluginDbContextException(string? message, Type? unregisteredDbContext, Ex UnregisteredDbContext = unregisteredDbContext; } - protected PluginDbContextException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - public Type? UnregisteredDbContext { get; private set; } } \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/PluginDbContextNotFoundException.cs b/src/OnlineSales/Exceptions/PluginDbContextNotFoundException.cs index d0d4b22b..02d9886b 100644 --- a/src/OnlineSales/Exceptions/PluginDbContextNotFoundException.cs +++ b/src/OnlineSales/Exceptions/PluginDbContextNotFoundException.cs @@ -25,9 +25,4 @@ public PluginDbContextNotFoundException(Type? unregisteredDbContext, Exception? : base(HardcodedMessage, unregisteredDbContext, innerException) { } - - protected PluginDbContextNotFoundException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/PluginDbContextTooManyException.cs b/src/OnlineSales/Exceptions/PluginDbContextTooManyException.cs index a74afbb8..91a348fd 100644 --- a/src/OnlineSales/Exceptions/PluginDbContextTooManyException.cs +++ b/src/OnlineSales/Exceptions/PluginDbContextTooManyException.cs @@ -25,9 +25,4 @@ public PluginDbContextTooManyException(Type? unregisteredDbContext, Exception? i : base(HardcodedMessage, unregisteredDbContext, innerException) { } - - protected PluginDbContextTooManyException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/QueryException.cs b/src/OnlineSales/Exceptions/QueryException.cs index 0e443d26..49b66142 100644 --- a/src/OnlineSales/Exceptions/QueryException.cs +++ b/src/OnlineSales/Exceptions/QueryException.cs @@ -19,11 +19,6 @@ public QueryException(IEnumerable innerExceptions) FailedCommands = innerExceptions.SelectMany(e => e.FailedCommands).ToList(); } - protected QueryException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } - public List> FailedCommands { get; init; } = new List>(); } } \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs b/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs index 44dbf784..e441ee9a 100644 --- a/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs +++ b/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs @@ -22,10 +22,5 @@ public SyncEmailLogTaskException(string? message, Exception? innerException) : base(message, innerException) { } - - protected SyncEmailLogTaskException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } } diff --git a/src/OnlineSales/OnlineSales.csproj b/src/OnlineSales/OnlineSales.csproj index 9cf9d902..0bd19d23 100644 --- a/src/OnlineSales/OnlineSales.csproj +++ b/src/OnlineSales/OnlineSales.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 enable enable OnlineSales @@ -38,31 +38,31 @@ - - - + + + - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + - + - + diff --git a/src/OnlineSales/Properties/launchSettings.json b/src/OnlineSales/Properties/launchSettings.json index 16b1213a..ec664f13 100644 --- a/src/OnlineSales/Properties/launchSettings.json +++ b/src/OnlineSales/Properties/launchSettings.json @@ -12,8 +12,7 @@ "OnlineSales": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "swagger", - "applicationUrl": "http://localhost:45437", + "applicationUrl": "http://localhost:16305", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs b/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs index 60b62f9c..c55bdd16 100644 --- a/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs +++ b/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs @@ -14,8 +14,8 @@ public class TestAuthenticationHandler : AuthenticationHandler options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) - : base(options, logger, encoder, clock) + public TestAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder) + : base(options, logger, encoder) { } diff --git a/tests/OnlineSales.Tests/OnlineSales.Tests.csproj b/tests/OnlineSales.Tests/OnlineSales.Tests.csproj index 4002bd25..ab88b928 100644 --- a/tests/OnlineSales.Tests/OnlineSales.Tests.csproj +++ b/tests/OnlineSales.Tests/OnlineSales.Tests.csproj @@ -1,7 +1,7 @@  - net7.0 + net8.0 enable enable @@ -25,7 +25,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - + From 94f434812437460569ae3ea7047c5b4c199a6188 Mon Sep 17 00:00:00 2001 From: Peter Liapin Date: Tue, 23 Jan 2024 18:45:45 +0530 Subject: [PATCH 2/6] Unused using has been removed --- src/OnlineSales/DataAnnotations/CurrencyCodeAttribute.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/OnlineSales/DataAnnotations/CurrencyCodeAttribute.cs b/src/OnlineSales/DataAnnotations/CurrencyCodeAttribute.cs index 4b8a8546..eb9ea3a3 100644 --- a/src/OnlineSales/DataAnnotations/CurrencyCodeAttribute.cs +++ b/src/OnlineSales/DataAnnotations/CurrencyCodeAttribute.cs @@ -5,7 +5,6 @@ using System.ComponentModel.DataAnnotations; using System.Globalization; using System.Text; -using Npgsql.Internal.TypeHandlers.NetworkHandlers; namespace OnlineSales.DataAnnotations; From d48fa3493d51f5c09d30710bc13e7ea77ab65a4d Mon Sep 17 00:00:00 2001 From: Peter Liapin Date: Tue, 23 Jan 2024 22:38:23 +0530 Subject: [PATCH 3/6] Migration to NpgsqlDataSourceBuilder and support for EnableDynamicJson --- .../OnlineSales.Plugin.Sms/Services/SmscService.cs | 2 -- src/OnlineSales/Controllers/AccountsController.cs | 2 -- src/OnlineSales/Data/EsDbContext.cs | 3 --- src/OnlineSales/Data/PgDbContext.cs | 13 +++++++++---- src/OnlineSales/Entities/DealPipeline.cs | 2 +- src/OnlineSales/Helpers/JsonHelper.cs | 4 ---- src/OnlineSales/OnlineSales.csproj | 1 - 7 files changed, 10 insertions(+), 17 deletions(-) diff --git a/plugins/OnlineSales.Plugin.Sms/Services/SmscService.cs b/plugins/OnlineSales.Plugin.Sms/Services/SmscService.cs index f08e248c..0a4a1b16 100644 --- a/plugins/OnlineSales.Plugin.Sms/Services/SmscService.cs +++ b/plugins/OnlineSales.Plugin.Sms/Services/SmscService.cs @@ -2,12 +2,10 @@ // Licensed under the MIT license. See LICENSE file in the samples root for full license information. // -using System.Reflection; using System.Web; using Newtonsoft.Json; using OnlineSales.Plugin.Sms.Configuration; using OnlineSales.Plugin.Sms.Exceptions; -using Serilog; namespace OnlineSales.Plugin.Sms.Services; diff --git a/src/OnlineSales/Controllers/AccountsController.cs b/src/OnlineSales/Controllers/AccountsController.cs index e1482814..dca7b765 100644 --- a/src/OnlineSales/Controllers/AccountsController.cs +++ b/src/OnlineSales/Controllers/AccountsController.cs @@ -5,8 +5,6 @@ using AutoMapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using OnlineSales.Configuration; using OnlineSales.Data; using OnlineSales.DTOs; using OnlineSales.Entities; diff --git a/src/OnlineSales/Data/EsDbContext.cs b/src/OnlineSales/Data/EsDbContext.cs index faca56ab..df39d8e6 100644 --- a/src/OnlineSales/Data/EsDbContext.cs +++ b/src/OnlineSales/Data/EsDbContext.cs @@ -2,9 +2,6 @@ // Licensed under the MIT license. See LICENSE file in the samples root for full license information. // -using System; -using System.Reflection; -using Microsoft.EntityFrameworkCore.Metadata.Internal; using Nest; using OnlineSales.Configuration; using OnlineSales.DataAnnotations; diff --git a/src/OnlineSales/Data/PgDbContext.cs b/src/OnlineSales/Data/PgDbContext.cs index cbb56dc7..43d7a950 100644 --- a/src/OnlineSales/Data/PgDbContext.cs +++ b/src/OnlineSales/Data/PgDbContext.cs @@ -7,8 +7,8 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; -using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql; using OnlineSales.Configuration; using OnlineSales.DataAnnotations; using OnlineSales.Entities; @@ -208,11 +208,16 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) throw new MissingConfigurationException("Postgres configuration is mandatory."); } + var dataSourceBuilder = new NpgsqlDataSourceBuilder(postgresConfig.ConnectionString); + dataSourceBuilder.EnableDynamicJson(); + var dataSource = dataSourceBuilder + .Build(); + optionsBuilder.UseNpgsql( - postgresConfig.ConnectionString, + dataSource, b => b.MigrationsHistoryTable("_migrations")) - .UseSnakeCaseNamingConvention() - .ReplaceService(); + .UseSnakeCaseNamingConvention() + .ReplaceService(); Console.WriteLine("PgDbContext successfully configured"); } diff --git a/src/OnlineSales/Entities/DealPipeline.cs b/src/OnlineSales/Entities/DealPipeline.cs index b9e23e7a..b0bba40e 100644 --- a/src/OnlineSales/Entities/DealPipeline.cs +++ b/src/OnlineSales/Entities/DealPipeline.cs @@ -4,7 +4,7 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -using Newtonsoft.Json; +using System.Text.Json.Serialization; using OnlineSales.DataAnnotations; namespace OnlineSales.Entities; diff --git a/src/OnlineSales/Helpers/JsonHelper.cs b/src/OnlineSales/Helpers/JsonHelper.cs index 2ac4d298..8ab693f8 100644 --- a/src/OnlineSales/Helpers/JsonHelper.cs +++ b/src/OnlineSales/Helpers/JsonHelper.cs @@ -4,10 +4,6 @@ using System.Text.Json; using System.Text.Json.Serialization; -using System.Text.Json.Serialization.Metadata; -using CsvHelper.TypeConversion; -using Newtonsoft.Json.Converters; -using Newtonsoft.Json.Serialization; using OnlineSales.Configuration; namespace OnlineSales.Helpers; diff --git a/src/OnlineSales/OnlineSales.csproj b/src/OnlineSales/OnlineSales.csproj index 0bd19d23..d0c9d65d 100644 --- a/src/OnlineSales/OnlineSales.csproj +++ b/src/OnlineSales/OnlineSales.csproj @@ -41,7 +41,6 @@ - From 4e8f28aa302f163765c782d6224fc44e057f5c43 Mon Sep 17 00:00:00 2001 From: Peter Liapin Date: Tue, 23 Jan 2024 23:48:12 +0530 Subject: [PATCH 4/6] Fix for singletone object being created multiple times --- .../Data/EmailSyncDbContext.cs | 5 ++-- .../Data/SendgridDbContext.cs | 5 ++-- .../Data/SmsDbContext.cs | 5 ++-- .../Data/TestPluginDbContext.cs | 6 +++-- .../Data/VstoDbContext.cs | 5 ++-- src/OnlineSales/Data/PgDbContext.cs | 27 ++++++++++--------- src/OnlineSales/Data/PluginDbContextBase.cs | 5 ++-- src/OnlineSales/Dockerfile | 4 +-- src/OnlineSales/Program.cs | 21 ++++++++++++++- 9 files changed, 55 insertions(+), 28 deletions(-) diff --git a/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs b/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs index 1aa31e8b..88c622b9 100644 --- a/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs +++ b/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs @@ -7,6 +7,7 @@ using EntityFrameworkCore.EncryptColumn.Util; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Npgsql; using OnlineSales.Data; using OnlineSales.Interfaces; using OnlineSales.Plugin.EmailSync.Entities; @@ -22,8 +23,8 @@ public EmailSyncDbContext() { } - public EmailSyncDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper) - : base(options, configuration, httpContextHelper) + public EmailSyncDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper, NpgsqlDataSource dataSource) + : base(options, configuration, httpContextHelper, dataSource) { var key = configuration.GetSection("EmailSync:EncryptionKey").Get(); if (key == null ) diff --git a/plugins/OnlineSales.Plugin.SendGrid/Data/SendgridDbContext.cs b/plugins/OnlineSales.Plugin.SendGrid/Data/SendgridDbContext.cs index ecb511a4..6dd8dacc 100644 --- a/plugins/OnlineSales.Plugin.SendGrid/Data/SendgridDbContext.cs +++ b/plugins/OnlineSales.Plugin.SendGrid/Data/SendgridDbContext.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Npgsql; using OnlineSales.Data; using OnlineSales.Interfaces; using OnlineSales.Plugin.SendGrid.Entities; @@ -17,8 +18,8 @@ public SendgridDbContext() { } - public SendgridDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper) - : base(options, configuration, httpContextHelper) + public SendgridDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper, NpgsqlDataSource dataSource) + : base(options, configuration, httpContextHelper, dataSource) { } diff --git a/plugins/OnlineSales.Plugin.Sms/Data/SmsDbContext.cs b/plugins/OnlineSales.Plugin.Sms/Data/SmsDbContext.cs index 93a142ec..1c4cb8c8 100644 --- a/plugins/OnlineSales.Plugin.Sms/Data/SmsDbContext.cs +++ b/plugins/OnlineSales.Plugin.Sms/Data/SmsDbContext.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Npgsql; using OnlineSales.Data; using OnlineSales.Plugin.Sms.Entities; @@ -11,8 +12,8 @@ namespace OnlineSales.Plugin.Sms.Data; public class SmsDbContext : PluginDbContextBase { - public SmsDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper) - : base(options, configuration, httpContextHelper) + public SmsDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper, NpgsqlDataSource dataSource) + : base(options, configuration, httpContextHelper, dataSource) { } diff --git a/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs b/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs index d2383219..2fdb8eb5 100644 --- a/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs +++ b/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs @@ -2,10 +2,12 @@ // Licensed under the MIT license. See LICENSE file in the samples root for full license information. // +using System.Data.Common; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.Extensions.Configuration; +using Npgsql; using OnlineSales.Data; using OnlineSales.Interfaces; using OnlineSales.Plugin.TestPlugin.Entities; @@ -23,8 +25,8 @@ public TestPluginDbContext() migrations = new SortedSet(); } - public TestPluginDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper) - : base(options, configuration, httpContextHelper) + public TestPluginDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper, NpgsqlDataSource dataSource) + : base(options, configuration, httpContextHelper, dataSource) { migrations = new SortedSet(Database.GetPendingMigrations()); } diff --git a/plugins/OnlineSales.Plugin.Vsto/Data/VstoDbContext.cs b/plugins/OnlineSales.Plugin.Vsto/Data/VstoDbContext.cs index 1ebc5f9c..8db54c2f 100644 --- a/plugins/OnlineSales.Plugin.Vsto/Data/VstoDbContext.cs +++ b/plugins/OnlineSales.Plugin.Vsto/Data/VstoDbContext.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; +using Npgsql; using OnlineSales.Data; using OnlineSales.Plugin.Vsto.Entities; @@ -16,8 +17,8 @@ public VstoDbContext() { } - public VstoDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper) - : base(options, configuration, httpContextHelper) + public VstoDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper, NpgsqlDataSource dataSource) + : base(options, configuration, httpContextHelper, dataSource) { } diff --git a/src/OnlineSales/Data/PgDbContext.cs b/src/OnlineSales/Data/PgDbContext.cs index 43d7a950..7b96be91 100644 --- a/src/OnlineSales/Data/PgDbContext.cs +++ b/src/OnlineSales/Data/PgDbContext.cs @@ -23,6 +23,7 @@ public class PgDbContext : IdentityDbContext public readonly IConfiguration Configuration; private readonly IHttpContextHelper? httpContextHelper; + private readonly NpgsqlDataSource dataSource; /// /// Initializes a new instance of the class. @@ -40,6 +41,17 @@ public PgDbContext() .AddUserSecrets(typeof(Program).Assembly) .Build(); + var postgresConfig = Configuration.GetSection("Postgres").Get(); + + if (postgresConfig == null) + { + throw new MissingConfigurationException("Postgres configuration is mandatory."); + } + + var dataSourceBuilder = new NpgsqlDataSourceBuilder(postgresConfig.ConnectionString); + dataSourceBuilder.EnableDynamicJson(); + dataSource = dataSourceBuilder.Build(); + Console.WriteLine("PgDbContext initialized"); } catch (Exception ex) @@ -49,11 +61,12 @@ public PgDbContext() } } - public PgDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper) + public PgDbContext(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper, NpgsqlDataSource dataSource) : base(options) { Configuration = configuration; this.httpContextHelper = httpContextHelper; + this.dataSource = dataSource; } public bool IsImportRequest { get; set; } @@ -201,18 +214,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { Console.WriteLine("Configuring PgDbContext..."); - var postgresConfig = Configuration.GetSection("Postgres").Get(); - - if (postgresConfig == null) - { - throw new MissingConfigurationException("Postgres configuration is mandatory."); - } - - var dataSourceBuilder = new NpgsqlDataSourceBuilder(postgresConfig.ConnectionString); - dataSourceBuilder.EnableDynamicJson(); - var dataSource = dataSourceBuilder - .Build(); - optionsBuilder.UseNpgsql( dataSource, b => b.MigrationsHistoryTable("_migrations")) diff --git a/src/OnlineSales/Data/PluginDbContextBase.cs b/src/OnlineSales/Data/PluginDbContextBase.cs index aa36b48a..bbd8c135 100644 --- a/src/OnlineSales/Data/PluginDbContextBase.cs +++ b/src/OnlineSales/Data/PluginDbContextBase.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; +using Npgsql; using OnlineSales.Interfaces; namespace OnlineSales.Data; @@ -15,8 +16,8 @@ protected PluginDbContextBase() { } - protected PluginDbContextBase(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper) - : base(options, configuration, httpContextHelper) + protected PluginDbContextBase(DbContextOptions options, IConfiguration configuration, IHttpContextHelper httpContextHelper, NpgsqlDataSource dataSource) + : base(options, configuration, httpContextHelper, dataSource) { } diff --git a/src/OnlineSales/Dockerfile b/src/OnlineSales/Dockerfile index 8d65ade7..5eb23b13 100644 --- a/src/OnlineSales/Dockerfile +++ b/src/OnlineSales/Dockerfile @@ -1,10 +1,10 @@ #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. -FROM mcr.microsoft.com/dotnet/aspnet:7.0-alpine AS base +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base WORKDIR /app EXPOSE 80 -FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build WORKDIR /src COPY ["src/OnlineSales/OnlineSales.csproj", "src/OnlineSales/"] RUN dotnet restore "src/OnlineSales/OnlineSales.csproj" diff --git a/src/OnlineSales/Program.cs b/src/OnlineSales/Program.cs index 2c1d8317..e32c70e6 100644 --- a/src/OnlineSales/Program.cs +++ b/src/OnlineSales/Program.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Mvc.Formatters; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; +using Npgsql; using OnlineSales.Configuration; using OnlineSales.Data; using OnlineSales.Formatters.Csv; @@ -46,8 +47,10 @@ public static void Main(string[] args) { builder.Configuration.AddJsonFile(path, false, true); }); - + ConfigureLogs(builder); + + ConfigurePgDataSource(builder); PluginManager.Init(builder.Configuration); builder.Configuration.AddUserSecrets(typeof(Program).Assembly); @@ -156,6 +159,22 @@ public static void Main(string[] args) app.Run(); } + private static void ConfigurePgDataSource(WebApplicationBuilder builder) + { + var postgresConfig = builder.Configuration.GetSection("Postgres").Get(); + + if (postgresConfig == null) + { + throw new MissingConfigurationException("Postgres configuration is mandatory."); + } + + var dataSourceBuilder = new NpgsqlDataSourceBuilder(postgresConfig.ConnectionString); + dataSourceBuilder.EnableDynamicJson(); + var dataSource = dataSourceBuilder.Build(); + + builder.Services.AddSingleton(dataSource); + } + private static void ConfigureImportSizeLimit(WebApplicationBuilder builder) { var maxImportSizeConfig = builder.Configuration.GetValue("ApiSettings:MaxImportSize"); From f072ce61f9fbc90874f38c12d18316272345490b Mon Sep 17 00:00:00 2001 From: peterliapin Date: Thu, 22 Feb 2024 23:02:26 +0530 Subject: [PATCH 5/6] Obsolete exception constructor type removed --- .../Exceptions/TooManyRequestsException.cs | 5 --- .../Exceptions/UnauthorizedException.cs | 5 --- src/OnlineSales/OnlineSales.csproj | 12 ------- src/OnlineSales/Program.cs | 33 +++++++++---------- 4 files changed, 16 insertions(+), 39 deletions(-) diff --git a/src/OnlineSales/Exceptions/TooManyRequestsException.cs b/src/OnlineSales/Exceptions/TooManyRequestsException.cs index bb142a0a..3da7ee83 100644 --- a/src/OnlineSales/Exceptions/TooManyRequestsException.cs +++ b/src/OnlineSales/Exceptions/TooManyRequestsException.cs @@ -22,9 +22,4 @@ public TooManyRequestsException(string? message, Exception? innerException) : base(message, innerException) { } - - protected TooManyRequestsException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } diff --git a/src/OnlineSales/Exceptions/UnauthorizedException.cs b/src/OnlineSales/Exceptions/UnauthorizedException.cs index c61c4d51..d709b836 100644 --- a/src/OnlineSales/Exceptions/UnauthorizedException.cs +++ b/src/OnlineSales/Exceptions/UnauthorizedException.cs @@ -13,9 +13,4 @@ public UnauthorizedException() : base("Failed to login") { } - - protected UnauthorizedException(SerializationInfo info, StreamingContext context) - : base(info, context) - { - } } \ No newline at end of file diff --git a/src/OnlineSales/OnlineSales.csproj b/src/OnlineSales/OnlineSales.csproj index 5d99e047..fb27333c 100644 --- a/src/OnlineSales/OnlineSales.csproj +++ b/src/OnlineSales/OnlineSales.csproj @@ -38,16 +38,10 @@ -<<<<<<< HEAD - -======= - - ->>>>>>> develop @@ -68,15 +62,9 @@ -<<<<<<< HEAD - - -======= - ->>>>>>> develop diff --git a/src/OnlineSales/Program.cs b/src/OnlineSales/Program.cs index f3190277..1795eb47 100644 --- a/src/OnlineSales/Program.cs +++ b/src/OnlineSales/Program.cs @@ -166,22 +166,6 @@ public static async Task Main(string[] args) app.Run(); } -<<<<<<< HEAD - private static void ConfigurePgDataSource(WebApplicationBuilder builder) - { - var postgresConfig = builder.Configuration.GetSection("Postgres").Get(); - - if (postgresConfig == null) - { - throw new MissingConfigurationException("Postgres configuration is mandatory."); - } - - var dataSourceBuilder = new NpgsqlDataSourceBuilder(postgresConfig.ConnectionString); - dataSourceBuilder.EnableDynamicJson(); - var dataSource = dataSourceBuilder.Build(); - - builder.Services.AddSingleton(dataSource); -======= public static async Task CreateDefaultIdentity(IServiceScope scope) { var defaultRoles = app!.Configuration.GetSection("DefaultRoles").Get()!; @@ -221,7 +205,22 @@ public static async Task CreateDefaultIdentity(IServiceScope scope) } } } ->>>>>>> develop + } + + private static void ConfigurePgDataSource(WebApplicationBuilder builder) + { + var postgresConfig = builder.Configuration.GetSection("Postgres").Get(); + + if (postgresConfig == null) + { + throw new MissingConfigurationException("Postgres configuration is mandatory."); + } + + var dataSourceBuilder = new NpgsqlDataSourceBuilder(postgresConfig.ConnectionString); + dataSourceBuilder.EnableDynamicJson(); + var dataSource = dataSourceBuilder.Build(); + + builder.Services.AddSingleton(dataSource); } private static void ConfigureImportSizeLimit(WebApplicationBuilder builder) From 6bbc3a3c9d047fc98dbc8898e66abfb8d87d6cd9 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 2 Aug 2024 05:41:40 +0000 Subject: [PATCH 6/6] style: format code with dotnet-format This commit fixes the style issues introduced in f072ce6 according to the output from dotnet-format. Details: https://github.com/peterliapin/onlinesales.core/pull/236 --- .../Data/EmailSyncDbContext.cs | 4 +- .../Data/TestPluginDbContext.cs | 4 +- .../Controllers/ActivityLogController.cs | 6 +- src/OnlineSales/Controllers/BaseController.cs | 486 +++++++++--------- src/OnlineSales/Data/EsDbContext.cs | 36 +- src/OnlineSales/Data/PluginDbContextBase.cs | 2 +- .../MissingConfigurationException.cs | 48 +- .../Exceptions/SyncEmailLogTaskException.cs | 2 +- .../Exceptions/TooManyRequestsException.cs | 2 +- src/OnlineSales/Program.cs | 4 +- .../Environment/TestAuthenticationHandler.cs | 6 +- 11 files changed, 300 insertions(+), 300 deletions(-) diff --git a/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs b/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs index 88c622b9..dd9b9ee9 100644 --- a/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs +++ b/plugins/OnlineSales.Plugin.EmailSync/Data/EmailSyncDbContext.cs @@ -27,7 +27,7 @@ public EmailSyncDbContext(DbContextOptions options, IConfiguration : base(options, configuration, httpContextHelper, dataSource) { var key = configuration.GetSection("EmailSync:EncryptionKey").Get(); - if (key == null ) + if (key == null) { throw new ArgumentNullException(key); } @@ -45,6 +45,6 @@ protected override void OnModelCreating(ModelBuilder builder) if (encryptionProvider != null) { builder.UseEncryption(encryptionProvider); - } + } } } \ No newline at end of file diff --git a/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs b/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs index 2fdb8eb5..44436f5d 100644 --- a/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs +++ b/plugins/OnlineSales.Plugin.TestPlugin/Data/TestPluginDbContext.cs @@ -2,7 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the samples root for full license information. // -using System.Data.Common; +using System.Data.Common; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; @@ -71,6 +71,6 @@ private string FindMigration(string name) throw new TestDbContextException($"Cannot find migration with name containig '{name}'"); } - return migration; + return migration; } } \ No newline at end of file diff --git a/src/OnlineSales/Controllers/ActivityLogController.cs b/src/OnlineSales/Controllers/ActivityLogController.cs index 80917227..1363ce5c 100644 --- a/src/OnlineSales/Controllers/ActivityLogController.cs +++ b/src/OnlineSales/Controllers/ActivityLogController.cs @@ -14,12 +14,12 @@ namespace OnlineSales.Controllers; [Authorize(Roles = "Admin")] [Route("api/[controller]")] public class ActivityLogController : ControllerBase -{ +{ private readonly IMapper mapper; private readonly ESOnlyQueryProviderFactory queryProviderFactory; public ActivityLogController(IMapper mapper, ESOnlyQueryProviderFactory queryProviderFactory) - { + { this.mapper = mapper; this.queryProviderFactory = queryProviderFactory; } @@ -37,4 +37,4 @@ public virtual async Task>> Get([FromQu Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); return Ok(mapper.Map>(result.Records)); } -} +} \ No newline at end of file diff --git a/src/OnlineSales/Controllers/BaseController.cs b/src/OnlineSales/Controllers/BaseController.cs index d74517ec..2ca34e8c 100644 --- a/src/OnlineSales/Controllers/BaseController.cs +++ b/src/OnlineSales/Controllers/BaseController.cs @@ -1,249 +1,249 @@ -// -// Licensed under the MIT license. See LICENSE file in the samples root for full license information. -// - -using System.Collections; -using System.Reflection; -using AutoMapper; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; -using OnlineSales.Data; -using OnlineSales.Entities; -using OnlineSales.Infrastructure; - -namespace OnlineSales.Controllers -{ - public class BaseController : ControllerBase - where T : BaseEntityWithId, new() - where TC : class - where TU : class - where TD : class - { - protected readonly DbSet dbSet; - protected readonly PgDbContext dbContext; - protected readonly IMapper mapper; - protected readonly QueryProviderFactory queryProviderFactory; - - public BaseController(PgDbContext dbContext, IMapper mapper, EsDbContext esDbContext, QueryProviderFactory queryProviderFactory) - { - this.dbContext = dbContext; - this.mapper = mapper; - - dbSet = dbContext.Set(); - this.queryProviderFactory = queryProviderFactory; - } - - // GET api/{entity}s/5 - [HttpGet("{id}")] - // [EnableQuery] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] - public virtual async Task> GetOne(int id) - { - var result = await FindOrThrowNotFound(id); - - var resultConverted = mapper.Map(result); - - return Ok(resultConverted); - } - - // POST api/{entity}s - [HttpPost] - [ProducesResponseType(StatusCodes.Status201Created)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] - public virtual async Task> Post([FromBody] TC value) - { - var newValue = mapper.Map(value); - var result = await dbSet.AddAsync(newValue); - await dbContext.SaveChangesAsync(); - - var resultsToClient = mapper.Map(newValue); - - return CreatedAtAction(nameof(GetOne), new { id = result.Entity.Id }, resultsToClient); - } - - // PUT api/{entity}s/5 - [HttpPatch("{id}")] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] - public virtual async Task> Patch(int id, [FromBody] TU value) - { - var existingEntity = await FindOrThrowNotFound(id); - return await Patch(existingEntity, value); - } - - // DELETE api/{entity}s/5 - [HttpDelete("{id}")] - [ProducesResponseType(StatusCodes.Status204NoContent)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] - public virtual async Task Delete(int id) - { - var existingEntity = await FindOrThrowNotFound(id); - - dbContext.Remove(existingEntity); - - await dbContext.SaveChangesAsync(); - - return NoContent(); - } - - [HttpGet] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] - public virtual async Task>> Get([FromQuery] string? query) - { - var qp = queryProviderFactory.BuildQueryProvider(); - - var result = await qp.GetResult(); - Response.Headers.Append(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); - Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); - var res = mapper.Map>(result.Records); - RemoveSecondLevelObjects(res); - return Ok(res); - } - - [HttpGet("export")] - [Produces("text/csv", "text/json")] - [ProducesResponseType(StatusCodes.Status401Unauthorized)] - [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] - public virtual async Task>> Export([FromQuery] string? query) +// +// Licensed under the MIT license. See LICENSE file in the samples root for full license information. +// + +using System.Collections; +using System.Reflection; +using AutoMapper; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using OnlineSales.Data; +using OnlineSales.Entities; +using OnlineSales.Infrastructure; + +namespace OnlineSales.Controllers +{ + public class BaseController : ControllerBase + where T : BaseEntityWithId, new() + where TC : class + where TU : class + where TD : class + { + protected readonly DbSet dbSet; + protected readonly PgDbContext dbContext; + protected readonly IMapper mapper; + protected readonly QueryProviderFactory queryProviderFactory; + + public BaseController(PgDbContext dbContext, IMapper mapper, EsDbContext esDbContext, QueryProviderFactory queryProviderFactory) + { + this.dbContext = dbContext; + this.mapper = mapper; + + dbSet = dbContext.Set(); + this.queryProviderFactory = queryProviderFactory; + } + + // GET api/{entity}s/5 + [HttpGet("{id}")] + // [EnableQuery] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public virtual async Task> GetOne(int id) + { + var result = await FindOrThrowNotFound(id); + + var resultConverted = mapper.Map(result); + + return Ok(resultConverted); + } + + // POST api/{entity}s + [HttpPost] + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public virtual async Task> Post([FromBody] TC value) + { + var newValue = mapper.Map(value); + var result = await dbSet.AddAsync(newValue); + await dbContext.SaveChangesAsync(); + + var resultsToClient = mapper.Map(newValue); + + return CreatedAtAction(nameof(GetOne), new { id = result.Entity.Id }, resultsToClient); + } + + // PUT api/{entity}s/5 + [HttpPatch("{id}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public virtual async Task> Patch(int id, [FromBody] TU value) + { + var existingEntity = await FindOrThrowNotFound(id); + return await Patch(existingEntity, value); + } + + // DELETE api/{entity}s/5 + [HttpDelete("{id}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public virtual async Task Delete(int id) + { + var existingEntity = await FindOrThrowNotFound(id); + + dbContext.Remove(existingEntity); + + await dbContext.SaveChangesAsync(); + + return NoContent(); + } + + [HttpGet] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public virtual async Task>> Get([FromQuery] string? query) + { + var qp = queryProviderFactory.BuildQueryProvider(); + + var result = await qp.GetResult(); + Response.Headers.Append(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); + Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); + var res = mapper.Map>(result.Records); + RemoveSecondLevelObjects(res); + return Ok(res); + } + + [HttpGet("export")] + [Produces("text/csv", "text/json")] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status500InternalServerError)] + public virtual async Task>> Export([FromQuery] string? query) { - var qp = queryProviderFactory.BuildQueryProvider(int.MaxValue); - - var result = await qp.GetResult(); - Response.Headers.Append(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); - Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); - var res = mapper.Map>(result.Records); - RemoveSecondLevelObjects(res); + var qp = queryProviderFactory.BuildQueryProvider(int.MaxValue); + + var result = await qp.GetResult(); + Response.Headers.Append(ResponseHeaderNames.TotalCount, result.TotalCount.ToString()); + Response.Headers.Append(ResponseHeaderNames.AccessControlExposeHeader, ResponseHeaderNames.TotalCount); + var res = mapper.Map>(result.Records); + RemoveSecondLevelObjects(res); return Ok(res); } - protected async Task FindOrThrowNotFound(int id) - { - var existingEntity = await (from p in dbSet - where p.Id == id - select p).FirstOrDefaultAsync(); - - if (existingEntity == null) - { - throw new EntityNotFoundException(typeof(T).Name, id.ToString()); - } - - return existingEntity; - } - - protected async Task> Patch(T existingEntity, TU value) - { - mapper.Map(value, existingEntity); - await dbContext.SaveChangesAsync(); - - var resultsToClient = mapper.Map(existingEntity); - - return Ok(resultsToClient); - } - - private static void RemoveSecondLevelObjects(IList data) - { - var refs = SecondLevelDTOs.Data; - - foreach (var item in data) - { - foreach (var r in refs) - { - var propertyObject = r.Key.GetValue(item); - if (propertyObject != null) - { - if (r.Key.PropertyType.GetInterface("IEnumerable") != null && r.Key.PropertyType.IsGenericType) - { - var e = propertyObject as IEnumerable; - foreach (var obj in e!) - { - foreach (var p in r.Value) - { - p.SetValue(obj, null); - } - } - } - else - { - foreach (var p in r.Value) - { - p.SetValue(propertyObject, null); - } - } - } - } - } - } - - private sealed class SecondLevelDTOs - { - public static readonly Dictionary> Data = InitReferences(); - - private static Dictionary> InitReferences() - { - bool IsNullableProperty(PropertyInfo pi) - { - var context = new NullabilityInfoContext(); - var info = context.Create(pi); - return info.WriteState == NullabilityState.Nullable; - } - - bool IsDto(Type type) - { - return type.IsClass && type.Namespace != null && type.Namespace!.StartsWith("OnlineSales.DTOs"); - } - - bool IsNeedToSave(PropertyInfo pi) - { - return IsNullableProperty(pi) && - (IsDto(pi.PropertyType) || (pi.PropertyType.GetInterface("IEnumerable") != null && pi.PropertyType.IsGenericType && IsDto(pi.PropertyType.GetGenericArguments()[0]))); - } - - Type GetType(PropertyInfo pi) - { - if (pi.PropertyType.GetInterface("IEnumerable") != null && pi.PropertyType.IsGenericType) - { - return pi.PropertyType.GetGenericArguments()[0]; - } - else - { - return pi.PropertyType; - } - } - - var res = new Dictionary>(); - var properties = typeof(TD).GetProperties(); - foreach (var property in properties) - { - var pType = GetType(property); - var nestedProperties = pType.GetProperties(); - foreach (var nestedProperty in nestedProperties.Where(np => IsNeedToSave(np))) - { - List temp; - if (res.TryGetValue(property, out temp!)) - { - temp.Add(nestedProperty); - } - else - { - temp = new List() { nestedProperty }; - res.Add(property, temp); - } - } - } - - return res; + protected async Task FindOrThrowNotFound(int id) + { + var existingEntity = await (from p in dbSet + where p.Id == id + select p).FirstOrDefaultAsync(); + + if (existingEntity == null) + { + throw new EntityNotFoundException(typeof(T).Name, id.ToString()); + } + + return existingEntity; + } + + protected async Task> Patch(T existingEntity, TU value) + { + mapper.Map(value, existingEntity); + await dbContext.SaveChangesAsync(); + + var resultsToClient = mapper.Map(existingEntity); + + return Ok(resultsToClient); + } + + private static void RemoveSecondLevelObjects(IList data) + { + var refs = SecondLevelDTOs.Data; + + foreach (var item in data) + { + foreach (var r in refs) + { + var propertyObject = r.Key.GetValue(item); + if (propertyObject != null) + { + if (r.Key.PropertyType.GetInterface("IEnumerable") != null && r.Key.PropertyType.IsGenericType) + { + var e = propertyObject as IEnumerable; + foreach (var obj in e!) + { + foreach (var p in r.Value) + { + p.SetValue(obj, null); + } + } + } + else + { + foreach (var p in r.Value) + { + p.SetValue(propertyObject, null); + } + } + } + } + } + } + + private sealed class SecondLevelDTOs + { + public static readonly Dictionary> Data = InitReferences(); + + private static Dictionary> InitReferences() + { + bool IsNullableProperty(PropertyInfo pi) + { + var context = new NullabilityInfoContext(); + var info = context.Create(pi); + return info.WriteState == NullabilityState.Nullable; + } + + bool IsDto(Type type) + { + return type.IsClass && type.Namespace != null && type.Namespace!.StartsWith("OnlineSales.DTOs"); + } + + bool IsNeedToSave(PropertyInfo pi) + { + return IsNullableProperty(pi) && + (IsDto(pi.PropertyType) || (pi.PropertyType.GetInterface("IEnumerable") != null && pi.PropertyType.IsGenericType && IsDto(pi.PropertyType.GetGenericArguments()[0]))); + } + + Type GetType(PropertyInfo pi) + { + if (pi.PropertyType.GetInterface("IEnumerable") != null && pi.PropertyType.IsGenericType) + { + return pi.PropertyType.GetGenericArguments()[0]; + } + else + { + return pi.PropertyType; + } + } + + var res = new Dictionary>(); + var properties = typeof(TD).GetProperties(); + foreach (var property in properties) + { + var pType = GetType(property); + var nestedProperties = pType.GetProperties(); + foreach (var nestedProperty in nestedProperties.Where(np => IsNeedToSave(np))) + { + List temp; + if (res.TryGetValue(property, out temp!)) + { + temp.Add(nestedProperty); + } + else + { + temp = new List() { nestedProperty }; + res.Add(property, temp); + } + } + } + + return res; } - } - } + } + } } \ No newline at end of file diff --git a/src/OnlineSales/Data/EsDbContext.cs b/src/OnlineSales/Data/EsDbContext.cs index df39d8e6..559fa05c 100644 --- a/src/OnlineSales/Data/EsDbContext.cs +++ b/src/OnlineSales/Data/EsDbContext.cs @@ -2,8 +2,8 @@ // Licensed under the MIT license. See LICENSE file in the samples root for full license information. // -using Nest; -using OnlineSales.Configuration; +using Nest; +using OnlineSales.Configuration; using OnlineSales.DataAnnotations; using OnlineSales.Elastic; using OnlineSales.Entities; @@ -20,18 +20,18 @@ public class EsDbContext : ElasticDbContext public EsDbContext(IConfiguration configuration) { - elasticConfig = configuration.GetSection("Elastic").Get(); - - if (elasticConfig == null) - { - throw new MissingConfigurationException("Elastic configuration is mandatory."); - } - - var connectionSettings = new ConnectionSettings(new Uri(elasticConfig.Url)); - - connectionSettings.DefaultMappingFor(m => m - .IndexName($"{elasticConfig!.IndexPrefix}-logs")); - + elasticConfig = configuration.GetSection("Elastic").Get(); + + if (elasticConfig == null) + { + throw new MissingConfigurationException("Elastic configuration is mandatory."); + } + + var connectionSettings = new ConnectionSettings(new Uri(elasticConfig.Url)); + + connectionSettings.DefaultMappingFor(m => m + .IndexName($"{elasticConfig!.IndexPrefix}-logs")); + var assembly = typeof(EsDbContext).Assembly; entityTypes = @@ -42,7 +42,7 @@ public EsDbContext(IConfiguration configuration) var migrationsIndexName = ElasticHelper.GetIndexName(elasticConfig!.IndexPrefix, typeof(ElasticMigration)); - connectionSettings.DefaultMappingFor(m => m + connectionSettings.DefaultMappingFor(m => m .IndexName(migrationsIndexName)); foreach (var entityType in entityTypes) @@ -51,8 +51,8 @@ public EsDbContext(IConfiguration configuration) connectionSettings.DefaultMappingFor(entityType, m => m .IndexName(indexName)); - } - + } + elasticClient = new ElasticClient(connectionSettings); } @@ -60,5 +60,5 @@ public EsDbContext(IConfiguration configuration) public override string IndexPrefix => elasticConfig!.IndexPrefix; - protected override List EntityTypes => entityTypes; + protected override List EntityTypes => entityTypes; } \ No newline at end of file diff --git a/src/OnlineSales/Data/PluginDbContextBase.cs b/src/OnlineSales/Data/PluginDbContextBase.cs index bbd8c135..0a8f572f 100644 --- a/src/OnlineSales/Data/PluginDbContextBase.cs +++ b/src/OnlineSales/Data/PluginDbContextBase.cs @@ -2,7 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the samples root for full license information. // -using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Npgsql; using OnlineSales.Interfaces; diff --git a/src/OnlineSales/Exceptions/MissingConfigurationException.cs b/src/OnlineSales/Exceptions/MissingConfigurationException.cs index b10bf4ad..f7e22d77 100644 --- a/src/OnlineSales/Exceptions/MissingConfigurationException.cs +++ b/src/OnlineSales/Exceptions/MissingConfigurationException.cs @@ -1,25 +1,25 @@ -// -// Licensed under the MIT license. See LICENSE file in the samples root for full license information. -// - -using System.Runtime.Serialization; - -namespace OnlineSales.Exceptions; - -[Serializable] -public class MissingConfigurationException : Exception -{ - public MissingConfigurationException() - { - } - - public MissingConfigurationException(string? message) - : base(message) - { - } - - public MissingConfigurationException(string? message, Exception? innerException) - : base(message, innerException) - { - } +// +// Licensed under the MIT license. See LICENSE file in the samples root for full license information. +// + +using System.Runtime.Serialization; + +namespace OnlineSales.Exceptions; + +[Serializable] +public class MissingConfigurationException : Exception +{ + public MissingConfigurationException() + { + } + + public MissingConfigurationException(string? message) + : base(message) + { + } + + public MissingConfigurationException(string? message, Exception? innerException) + : base(message, innerException) + { + } } \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs b/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs index e441ee9a..ba2bf05b 100644 --- a/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs +++ b/src/OnlineSales/Exceptions/SyncEmailLogTaskException.cs @@ -23,4 +23,4 @@ public SyncEmailLogTaskException(string? message, Exception? innerException) { } } -} +} \ No newline at end of file diff --git a/src/OnlineSales/Exceptions/TooManyRequestsException.cs b/src/OnlineSales/Exceptions/TooManyRequestsException.cs index 3da7ee83..5c124d45 100644 --- a/src/OnlineSales/Exceptions/TooManyRequestsException.cs +++ b/src/OnlineSales/Exceptions/TooManyRequestsException.cs @@ -22,4 +22,4 @@ public TooManyRequestsException(string? message, Exception? innerException) : base(message, innerException) { } -} +} \ No newline at end of file diff --git a/src/OnlineSales/Program.cs b/src/OnlineSales/Program.cs index 1795eb47..119fc953 100644 --- a/src/OnlineSales/Program.cs +++ b/src/OnlineSales/Program.cs @@ -50,7 +50,7 @@ public static async Task Main(string[] args) { builder.Configuration.AddJsonFile(path, false, true); }); - + ConfigureLogs(builder); ConfigurePgDataSource(builder); @@ -92,7 +92,7 @@ public static async Task Main(string[] args) builder.Services.AddDbContext(); builder.Services.AddSingleton(); - + ConfigureQuartz(builder); ConfigureImageUpload(builder); ConfigureFileUpload(builder); diff --git a/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs b/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs index c55bdd16..824ab9fe 100644 --- a/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs +++ b/tests/OnlineSales.Tests/Environment/TestAuthenticationHandler.cs @@ -2,8 +2,8 @@ // Licensed under the MIT license. See LICENSE file in the samples root for full license information. // -using System.Security.Claims; -using System.Text.Encodings.Web; +using System.Security.Claims; +using System.Text.Encodings.Web; using Microsoft.AspNetCore.Authentication; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -30,7 +30,7 @@ protected override Task HandleAuthenticateAsync() var principal = new ClaimsPrincipal(identity); var ticket = new AuthenticationTicket(principal, SchemeName); - result = AuthenticateResult.Success(ticket); + result = AuthenticateResult.Success(ticket); } else {