diff --git a/src/Infrastructure/Extensions/SqlSugarSetup.cs b/src/Infrastructure/Extensions/SqlSugarSetup.cs new file mode 100644 index 0000000..0975be7 --- /dev/null +++ b/src/Infrastructure/Extensions/SqlSugarSetup.cs @@ -0,0 +1,79 @@ +using Infrastructure.Options; +using Infrastructure.Repository; +using Microsoft.AspNetCore.Hosting; +using Serilog; +using SqlSugar; +using SqlSugar.Extensions; + +namespace Infrastructure.Extensions; + +public static class SqlSugarSetup +{ + public static IServiceCollection AddDefaultSqlSugarSetup( + this IServiceCollection services, + IConfiguration configuration, + IWebHostEnvironment hostEnvironment) + { + ArgumentNullException.ThrowIfNull(services); + + ArgumentNullException.ThrowIfNull(configuration); + + ArgumentNullException.ThrowIfNull(hostEnvironment); + + var sqlSugarOptions = configuration.GetSection(SqlSugarOptions.Name).Get(); + if (sqlSugarOptions is null || !sqlSugarOptions.IsEnable) + { + return services; + } + + services.TryAddScoped(typeof(IRepositoryBase<>), typeof(RepositoryBase<>)); + services.TryAddScoped(); + if (sqlSugarOptions.SnowFlake?.IsEnable ?? false) + { + var workerId = configuration["SNOWFLAKES_WORKERID"]?.ObjToInt() ?? sqlSugarOptions.SnowFlake?.WorkerId; + ArgumentNullException.ThrowIfNull(workerId); + SnowFlakeSingle.WorkId = (int)workerId; + } + + var server = configuration["DB_HOST"] ?? sqlSugarOptions.Server; + ArgumentException.ThrowIfNullOrEmpty(server); + + var port = configuration["DB_PORT"] ?? sqlSugarOptions.Port.ToString(); + ArgumentException.ThrowIfNullOrEmpty(port); + + var database = configuration["DB_DATABASE"] ?? sqlSugarOptions.Database; + ArgumentException.ThrowIfNullOrEmpty(database); + + var user = configuration["DB_USER"] ?? sqlSugarOptions.User; + ArgumentException.ThrowIfNullOrEmpty(user); + + var password = configuration["DB_PASSWORD"] ?? sqlSugarOptions.Password; + ArgumentException.ThrowIfNullOrEmpty(password); + + var connectionString = $"server={server};port={port};database={database};userid={user};password={password};"; + + var connectionConfig = new ConnectionConfig() + { + DbType = DbType.PostgreSQL, + ConnectionString = connectionString, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + MoreSettings = new ConnMoreSettings() + { + PgSqlIsAutoToLower = false, + PgSqlIsAutoToLowerCodeFirst = false, + } + }; + + var sugarScope = new SqlSugarScope(connectionConfig, config => + { + config.QueryFilter.AddTableFilter(d => !d.IsDeleted); + if (hostEnvironment.IsDevelopment() || hostEnvironment.IsStaging()) + { + config.Aop.OnLogExecuting = (sql, parameters) => { Log.Logger.Information(sql); }; + } + }); + services.AddSingleton(sugarScope); + return services; + } +} \ No newline at end of file diff --git a/src/Infrastructure/IDeleteable.cs b/src/Infrastructure/IDeleteable.cs new file mode 100644 index 0000000..427d7b4 --- /dev/null +++ b/src/Infrastructure/IDeleteable.cs @@ -0,0 +1,6 @@ +namespace Infrastructure; + +public interface IDeletable +{ + bool IsDeleted { get; set; } +} \ No newline at end of file