diff --git a/Directory.Packages.props b/Directory.Packages.props index 7b88527..1425903 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -6,12 +6,14 @@ + + - + @@ -43,7 +45,7 @@ - + diff --git a/src/HelloShop.AppHost/HelloShop.AppHost.csproj b/src/HelloShop.AppHost/HelloShop.AppHost.csproj index 5139d68..a0f22dd 100644 --- a/src/HelloShop.AppHost/HelloShop.AppHost.csproj +++ b/src/HelloShop.AppHost/HelloShop.AppHost.csproj @@ -17,6 +17,7 @@ + diff --git a/src/HelloShop.AppHost/Program.cs b/src/HelloShop.AppHost/Program.cs index 4ca7d13..2a4aba6 100644 --- a/src/HelloShop.AppHost/Program.cs +++ b/src/HelloShop.AppHost/Program.cs @@ -6,6 +6,14 @@ using HelloShop.AppHost.Extensions; var builder = DistributedApplication.CreateBuilder(args); +var postgreUser = builder.AddParameter("postgreUser", secret: true); +var postgrePassword = builder.AddParameter("postgrePassword", secret: true); +var postgres = builder.AddPostgres("postgres", postgreUser, postgrePassword, port: 5432).WithPgAdmin() + .WithLifetime(ContainerLifetime.Persistent); +var identitydb = postgres.AddDatabase("identitydb"); +var productdb = postgres.AddDatabase("productdb"); +var orderingdb = postgres.AddDatabase("orderingdb"); + var cache = builder.AddRedis("cache", port: 6380).WithLifetime(ContainerLifetime.Persistent).WithPersistence(); var rabbitmqUser = builder.AddParameter("rabbitmqUser", secret: true); @@ -13,12 +21,14 @@ var rabbitmqPassword = builder.AddParameter("rabbitmqPassword", secret: true); var rabbitmq = builder.AddRabbitMQ("rabbitmq", rabbitmqUser, rabbitmqPassword).WithLifetime(ContainerLifetime.Persistent).WithManagementPlugin(); var identityService = builder.AddProject("identityservice") + .WithReference(identitydb).WaitFor(identitydb) .WithDaprSidecar(); DaprSidecarOptions daprSidecarOptions = new() { ResourcesPaths = ["DaprComponents"] }; var orderingService = builder.AddProject("orderingservice") .WithReference(identityService) + .WithReference(orderingdb).WaitFor(orderingdb) .WithDaprSidecar(options => { options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache); @@ -26,6 +36,7 @@ var orderingService = builder.AddProject("or var productService = builder.AddProject("productservice") .WithReference(identityService).WaitFor(identityService) + .WithReference(productdb).WaitFor(productdb) .WithDaprSidecar(options => { options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache); diff --git a/src/HelloShop.AppHost/appsettings.json b/src/HelloShop.AppHost/appsettings.json index dba66c0..ec31b2e 100644 --- a/src/HelloShop.AppHost/appsettings.json +++ b/src/HelloShop.AppHost/appsettings.json @@ -7,6 +7,8 @@ } }, "Parameters": { + "postgreUser": "postgres", + "postgrePassword": "postgres", "rabbitmqUser": "guest", "rabbitmqPassword": "guest" } diff --git a/src/HelloShop.IdentityService/Authentication/CustomJwtBearerHandler.cs b/src/HelloShop.IdentityService/Authentication/CustomJwtBearerHandler.cs index 842f401..c21313d 100644 --- a/src/HelloShop.IdentityService/Authentication/CustomJwtBearerHandler.cs +++ b/src/HelloShop.IdentityService/Authentication/CustomJwtBearerHandler.cs @@ -12,13 +12,13 @@ using System.Text.Encodings.Web; namespace HelloShop.IdentityService.Authentication; -public class CustomJwtBearerHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder,TimeProvider timeProvider) : SignInAuthenticationHandler(options, logger, encoder) +public class CustomJwtBearerHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, TimeProvider timeProvider) : SignInAuthenticationHandler(options, logger, encoder) { protected override Task HandleAuthenticateAsync() => throw new NotImplementedException(); protected override async Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties? properties) { - var utcNow =timeProvider.GetUtcNow(); + var utcNow = timeProvider.GetUtcNow(); JwtSecurityTokenHandler tokenHandler = new(); diff --git a/src/HelloShop.IdentityService/Authorization/LocalPermissionChecker.cs b/src/HelloShop.IdentityService/Authorization/LocalPermissionChecker.cs index 5c664af..2390e1f 100644 --- a/src/HelloShop.IdentityService/Authorization/LocalPermissionChecker.cs +++ b/src/HelloShop.IdentityService/Authorization/LocalPermissionChecker.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Distributed; namespace HelloShop.IdentityService.Authorization; -public class LocalPermissionChecker(IHttpContextAccessor httpContextAccessor, IdentityServiceDbContext dbContext, IDistributedCache distributedCache,TimeProvider timeProvider) : PermissionChecker(httpContextAccessor, distributedCache, timeProvider) +public class LocalPermissionChecker(IHttpContextAccessor httpContextAccessor, IdentityServiceDbContext dbContext, IDistributedCache distributedCache, TimeProvider timeProvider) : PermissionChecker(httpContextAccessor, distributedCache, timeProvider) { public override async Task IsGrantedAsync(int roleId, string name, string? resourceType = null, string? resourceId = null) { diff --git a/src/HelloShop.IdentityService/Constants/DbConstants.cs b/src/HelloShop.IdentityService/Constants/DbConstants.cs index c81a85d..ede2313 100644 --- a/src/HelloShop.IdentityService/Constants/DbConstants.cs +++ b/src/HelloShop.IdentityService/Constants/DbConstants.cs @@ -5,7 +5,7 @@ namespace HelloShop.IdentityService.Constants { public class DbConstants { - public const string ConnectionStringName = "IdentityDatabase"; + public const string ConnectionStringName = "identitydb"; public const string MigrationsHistoryTableName = "migrations_history"; } diff --git a/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj b/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj index 9d85574..e94b890 100644 --- a/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj +++ b/src/HelloShop.IdentityService/HelloShop.IdentityService.csproj @@ -8,10 +8,10 @@ + - runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/HelloShop.IdentityService/Infrastructure/IdentityServiceDbContext.cs b/src/HelloShop.IdentityService/Infrastructure/IdentityServiceDbContext.cs index d8e6023..8e749c6 100644 --- a/src/HelloShop.IdentityService/Infrastructure/IdentityServiceDbContext.cs +++ b/src/HelloShop.IdentityService/Infrastructure/IdentityServiceDbContext.cs @@ -15,11 +15,5 @@ namespace HelloShop.IdentityService.Infrastructure base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - base.OnConfiguring(optionsBuilder); - optionsBuilder.UseSnakeCaseNamingConvention(); - } } } diff --git a/src/HelloShop.IdentityService/Program.cs b/src/HelloShop.IdentityService/Program.cs index 44a5004..dca06e9 100644 --- a/src/HelloShop.IdentityService/Program.cs +++ b/src/HelloShop.IdentityService/Program.cs @@ -11,6 +11,7 @@ using HelloShop.ServiceDefaults.Extensions; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; +using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure; using System.Text; var builder = WebApplication.CreateBuilder(args); @@ -21,9 +22,10 @@ builder.AddServiceDefaults(); builder.Services.AddControllers(); -builder.Services.AddDbContext(options => +builder.AddNpgsqlDbContext(connectionName: DbConstants.ConnectionStringName, configureDbContextOptions: options => { - options.UseNpgsql(builder.Configuration.GetConnectionString(DbConstants.ConnectionStringName), x => x.MigrationsHistoryTable(DbConstants.MigrationsHistoryTableName)); + new NpgsqlDbContextOptionsBuilder(options).MigrationsHistoryTable(DbConstants.MigrationsHistoryTableName); + options.UseSnakeCaseNamingConvention(); }); builder.Services.AddIdentity(options => diff --git a/src/HelloShop.IdentityService/appsettings.json b/src/HelloShop.IdentityService/appsettings.json index c972fa3..10f68b8 100644 --- a/src/HelloShop.IdentityService/appsettings.json +++ b/src/HelloShop.IdentityService/appsettings.json @@ -5,8 +5,5 @@ "Microsoft.AspNetCore": "Warning" } }, - "ConnectionStrings": { - "IdentityDatabase": "Host=localhost;Port=5432;Database=identity;Username=postgres;Password=postgres" - }, "AllowedHosts": "*" } diff --git a/src/HelloShop.OrderingService/Constants/DbConstants.cs b/src/HelloShop.OrderingService/Constants/DbConstants.cs index 73f802b..4cb89cc 100644 --- a/src/HelloShop.OrderingService/Constants/DbConstants.cs +++ b/src/HelloShop.OrderingService/Constants/DbConstants.cs @@ -5,9 +5,9 @@ namespace HelloShop.OrderingService.Constants { public class DbConstants { - public const string MasterConnectionStringName = "OrderingDatabaseMaster"; + public const string MasterConnectionStringName = "orderingdb"; - public const string SlaveConnectionStringName = "OrderingDatabaseSlave"; + public const string SlaveConnectionStringName = "orderingdb"; public const string MigrationsHistoryTableName = "migrations_history"; } diff --git a/src/HelloShop.OrderingService/Extensions/Extensions.cs b/src/HelloShop.OrderingService/Extensions/Extensions.cs index d4319a7..d8b6da9 100644 --- a/src/HelloShop.OrderingService/Extensions/Extensions.cs +++ b/src/HelloShop.OrderingService/Extensions/Extensions.cs @@ -12,6 +12,7 @@ using HelloShop.ServiceDefaults.DistributedEvents.DaprBuildingBlocks; using HelloShop.ServiceDefaults.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; +using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure; using System.Reflection; using System.Text; @@ -34,9 +35,9 @@ namespace HelloShop.OrderingService.Extensions builder.Services.AddDataSeedingProviders(); - builder.Services.AddDbContext(options => + builder.AddNpgsqlDbContext(connectionName: DbConstants.MasterConnectionStringName, configureDbContextOptions: options => { - options.UseNpgsql(builder.Configuration.GetConnectionString(DbConstants.MasterConnectionStringName), x => x.MigrationsHistoryTable(DbConstants.MigrationsHistoryTableName)); + new NpgsqlDbContextOptionsBuilder(options).MigrationsHistoryTable(DbConstants.MigrationsHistoryTableName); options.UseSnakeCaseNamingConvention(); }); diff --git a/src/HelloShop.OrderingService/HelloShop.OrderingService.csproj b/src/HelloShop.OrderingService/HelloShop.OrderingService.csproj index 2b5e395..cf58f42 100644 --- a/src/HelloShop.OrderingService/HelloShop.OrderingService.csproj +++ b/src/HelloShop.OrderingService/HelloShop.OrderingService.csproj @@ -8,14 +8,13 @@ + - runtime; build; native; contentfiles; analyzers; buildtransitive all - \ No newline at end of file diff --git a/src/HelloShop.OrderingService/Workers/GracePeriodWorker.cs b/src/HelloShop.OrderingService/Workers/GracePeriodWorker.cs index 461cd5f..ed476ac 100644 --- a/src/HelloShop.OrderingService/Workers/GracePeriodWorker.cs +++ b/src/HelloShop.OrderingService/Workers/GracePeriodWorker.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore; namespace HelloShop.OrderingService.Workers { - public class GracePeriodWorker(IServiceScopeFactory serviceScopeFactory, ILogger logger,TimeProvider timeProvider) : BackgroundService + public class GracePeriodWorker(IServiceScopeFactory serviceScopeFactory, ILogger logger, TimeProvider timeProvider) : BackgroundService { protected async override Task ExecuteAsync(CancellationToken stoppingToken) { diff --git a/src/HelloShop.OrderingService/appsettings.json b/src/HelloShop.OrderingService/appsettings.json index 69890c1..10f68b8 100644 --- a/src/HelloShop.OrderingService/appsettings.json +++ b/src/HelloShop.OrderingService/appsettings.json @@ -5,9 +5,5 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*", - "ConnectionStrings": { - "OrderingDatabaseMaster": "Host=localhost;Port=5432;Database=ordering;Username=postgres;Password=postgres", - "OrderingDatabaseSlave": "Host=localhost;Port=5432;Database=ordering;Username=postgres;Password=postgres" - } + "AllowedHosts": "*" } diff --git a/src/HelloShop.ProductService/Constants/DbConstants.cs b/src/HelloShop.ProductService/Constants/DbConstants.cs index d57f27f..cda3390 100644 --- a/src/HelloShop.ProductService/Constants/DbConstants.cs +++ b/src/HelloShop.ProductService/Constants/DbConstants.cs @@ -5,7 +5,7 @@ namespace HelloShop.ProductService.Constants; public static class DbConstants { - public const string ConnectionStringName = "ProductDatabase"; + public const string ConnectionStringName = "productdb"; public const string MigrationsHistoryTableName = "migrations_history"; } diff --git a/src/HelloShop.ProductService/HelloShop.ProductService.csproj b/src/HelloShop.ProductService/HelloShop.ProductService.csproj index 4bb66d2..f5c2d27 100644 --- a/src/HelloShop.ProductService/HelloShop.ProductService.csproj +++ b/src/HelloShop.ProductService/HelloShop.ProductService.csproj @@ -8,13 +8,12 @@ + - runtime; build; native; contentfiles; analyzers; buildtransitive all - \ No newline at end of file diff --git a/src/HelloShop.ProductService/Program.cs b/src/HelloShop.ProductService/Program.cs index 5c102bd..8e2448e 100644 --- a/src/HelloShop.ProductService/Program.cs +++ b/src/HelloShop.ProductService/Program.cs @@ -9,6 +9,7 @@ using HelloShop.ServiceDefaults.DistributedLocks; using HelloShop.ServiceDefaults.Extensions; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; +using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure; using System.Text; var builder = WebApplication.CreateBuilder(args); @@ -29,9 +30,9 @@ builder.Services.AddAuthentication().AddJwtBearer(options => }); // Add extensions services to the container. -builder.Services.AddDbContext(options => +builder.AddNpgsqlDbContext(connectionName: DbConstants.ConnectionStringName, configureDbContextOptions: options => { - options.UseNpgsql(builder.Configuration.GetConnectionString(DbConstants.ConnectionStringName), x => x.MigrationsHistoryTable(DbConstants.MigrationsHistoryTableName)); + new NpgsqlDbContextOptionsBuilder(options).MigrationsHistoryTable(DbConstants.MigrationsHistoryTableName); options.UseSnakeCaseNamingConvention(); }); builder.Services.AddHttpClient().AddHttpContextAccessor().AddDistributedMemoryCache(); diff --git a/src/HelloShop.ProductService/appsettings.json b/src/HelloShop.ProductService/appsettings.json index 11a1190..10f68b8 100644 --- a/src/HelloShop.ProductService/appsettings.json +++ b/src/HelloShop.ProductService/appsettings.json @@ -5,8 +5,5 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*", - "ConnectionStrings": { - "ProductDatabase": "Host=localhost;Port=5432;Database=product;Username=postgres;Password=postgres" - } + "AllowedHosts": "*" } diff --git a/src/HelloShop.ServiceDefaults/Authorization/FakePermissionChecker.cs b/src/HelloShop.ServiceDefaults/Authorization/FakePermissionChecker.cs index 5fc239b..00d6b47 100644 --- a/src/HelloShop.ServiceDefaults/Authorization/FakePermissionChecker.cs +++ b/src/HelloShop.ServiceDefaults/Authorization/FakePermissionChecker.cs @@ -6,7 +6,7 @@ using Microsoft.Extensions.Caching.Distributed; namespace HelloShop.ServiceDefaults.Authorization { - public class FakePermissionChecker(IHttpContextAccessor httpContextAccessor, IDistributedCache distributedCache, TimeProvider timeProvider) : PermissionChecker(httpContextAccessor, distributedCache,timeProvider) + public class FakePermissionChecker(IHttpContextAccessor httpContextAccessor, IDistributedCache distributedCache, TimeProvider timeProvider) : PermissionChecker(httpContextAccessor, distributedCache, timeProvider) { public override Task IsGrantedAsync(int roleId, string permissionName, string? resourceType = null, string? resourceId = null) => Task.FromResult(true); } diff --git a/src/HelloShop.ServiceDefaults/Authorization/PermissionChecker.cs b/src/HelloShop.ServiceDefaults/Authorization/PermissionChecker.cs index f738c21..a999474 100644 --- a/src/HelloShop.ServiceDefaults/Authorization/PermissionChecker.cs +++ b/src/HelloShop.ServiceDefaults/Authorization/PermissionChecker.cs @@ -8,7 +8,7 @@ using System.Security.Claims; namespace HelloShop.ServiceDefaults.Authorization; -public abstract class PermissionChecker(IHttpContextAccessor httpContextAccessor, IDistributedCache distributedCache,TimeProvider timeProvider) : IPermissionChecker +public abstract class PermissionChecker(IHttpContextAccessor httpContextAccessor, IDistributedCache distributedCache, TimeProvider timeProvider) : IPermissionChecker { protected HttpContext HttpContext { get; init; } = httpContextAccessor.HttpContext ?? throw new InvalidOperationException(); diff --git a/src/HelloShop.ServiceDefaults/Authorization/RemotePermissionChecker.cs b/src/HelloShop.ServiceDefaults/Authorization/RemotePermissionChecker.cs index a16de20..60cfbb3 100644 --- a/src/HelloShop.ServiceDefaults/Authorization/RemotePermissionChecker.cs +++ b/src/HelloShop.ServiceDefaults/Authorization/RemotePermissionChecker.cs @@ -9,7 +9,7 @@ using System.Net.Http.Headers; namespace HelloShop.ServiceDefaults.Authorization; -public class RemotePermissionChecker(IHttpContextAccessor httpContextAccessor, IDistributedCache distributedCache, IHttpClientFactory httpClientFactory,TimeProvider timeProvider) : PermissionChecker(httpContextAccessor, distributedCache, timeProvider) +public class RemotePermissionChecker(IHttpContextAccessor httpContextAccessor, IDistributedCache distributedCache, IHttpClientFactory httpClientFactory, TimeProvider timeProvider) : PermissionChecker(httpContextAccessor, distributedCache, timeProvider) { public override async Task IsGrantedAsync(int roleId, string permissionName, string? resourceType = null, string? resourceId = null) {