diff --git a/back/Controllers/Controllers.http b/back/Controllers/Controllers.http index 01f7fc0..f5c450d 100644 --- a/back/Controllers/Controllers.http +++ b/back/Controllers/Controllers.http @@ -1,6 +1,5 @@ @Controllers_HostAddress = http://localhost:5125 -GET {{Controllers_HostAddress}}/weatherforecast/ Accept: application/json ### diff --git a/back/Controllers/Extensions/AddDbConnectionService.cs b/back/Controllers/Extensions/AddDbConnectionService.cs deleted file mode 100644 index e78595e..0000000 --- a/back/Controllers/Extensions/AddDbConnectionService.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Infrastructure; -using Microsoft.EntityFrameworkCore; - -namespace Controllers.Extensions; - -public static class DbConnectionServiceExtension -{ - public static void AddDbConnectionService(this IServiceCollection services) - { - var host = Environment.GetEnvironmentVariable("DB_HOST"); - var database = Environment.GetEnvironmentVariable("DB_NAME"); - var username = Environment.GetEnvironmentVariable("DB_USER"); - var password = Environment.GetEnvironmentVariable("DB_PASSWORD"); - var connectionString = $"Host={host};Database={database};Username={username};Password={password}"; - - services.AddDbContext(options => options.UseNpgsql(connectionString)); - services.AddSingleton, DbContextFactory>(); - } -} \ No newline at end of file diff --git a/back/Controllers/Extensions/AddDomainServicesExt.cs b/back/Controllers/Extensions/AddDomainServicesExt.cs index f68caa1..f28a69d 100644 --- a/back/Controllers/Extensions/AddDomainServicesExt.cs +++ b/back/Controllers/Extensions/AddDomainServicesExt.cs @@ -7,7 +7,7 @@ public static class AddDomainServicesExtension { public static void AddDomainServices(this IServiceCollection services) { - services.AddSingleton(); - services.AddSingleton(); + services.AddTransient(); + services.AddTransient(); } } \ No newline at end of file diff --git a/back/Controllers/Extensions/AddReposExt.cs b/back/Controllers/Extensions/AddReposExt.cs index 27d5b68..9207a8b 100644 --- a/back/Controllers/Extensions/AddReposExt.cs +++ b/back/Controllers/Extensions/AddReposExt.cs @@ -9,6 +9,6 @@ public static class AddReposExtension { public static void AddRepos(this IServiceCollection services) { - services.AddSingleton(); + services.AddTransient(); } } \ No newline at end of file diff --git a/back/Controllers/Extensions/DatabaseSetupExt.cs b/back/Controllers/Extensions/DatabaseSetupExt.cs new file mode 100644 index 0000000..35752d1 --- /dev/null +++ b/back/Controllers/Extensions/DatabaseSetupExt.cs @@ -0,0 +1,29 @@ +using Infrastructure; +using Microsoft.EntityFrameworkCore; + +namespace Controllers.Extensions; + +public static class DatabaseSetupExtension +{ + public static void AddDbConnectionService(this IServiceCollection services, IConfiguration config) + { + var connectionString = config.GetConnectionString("DefaultConnection") + ?? throw new ArgumentException("Нет строки подключения"); + services.AddDbContext(options => options.UseNpgsql(connectionString)); + services.AddSingleton, DbContextFactory>(); + } + + public static void MigrateDb(this IApplicationBuilder app) + { + try + { + using var scope = app.ApplicationServices.CreateScope(); + var context = scope.ServiceProvider.GetRequiredService(); + context.Database.Migrate(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } +} \ No newline at end of file diff --git a/back/Controllers/Program.cs b/back/Controllers/Program.cs index 1bb1553..2e20712 100644 --- a/back/Controllers/Program.cs +++ b/back/Controllers/Program.cs @@ -3,9 +3,9 @@ using Controllers.Extensions; var builder = WebApplication.CreateBuilder(args); // Add services to the container. +builder.Services.AddDbConnectionService(builder.Configuration); builder.Services.AddRepos(); builder.Services.AddDomainServices(); -builder.Services.AddDbConnectionService(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle @@ -21,10 +21,12 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } +app.MigrateDb(); + app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); -app.Run(); +await app.RunAsync(); diff --git a/back/Infrastructure/Migrations/20241125164748_User.Designer.cs b/back/Infrastructure/Migrations/20241125164748_User.Designer.cs new file mode 100644 index 0000000..9872477 --- /dev/null +++ b/back/Infrastructure/Migrations/20241125164748_User.Designer.cs @@ -0,0 +1,52 @@ +// +using System; +using Infrastructure; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Infrastructure.Migrations +{ + [DbContext(typeof(DatabaseContext))] + [Migration("20241125164748_User")] + partial class User + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Infrastructure.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Balance") + .HasColumnType("numeric"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/back/Infrastructure/Migrations/20241125164748_User.cs b/back/Infrastructure/Migrations/20241125164748_User.cs new file mode 100644 index 0000000..f35370d --- /dev/null +++ b/back/Infrastructure/Migrations/20241125164748_User.cs @@ -0,0 +1,36 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Infrastructure.Migrations +{ + /// + public partial class User : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: false), + Password = table.Column(type: "text", nullable: false), + Balance = table.Column(type: "numeric", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/back/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs b/back/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs new file mode 100644 index 0000000..1e66849 --- /dev/null +++ b/back/Infrastructure/Migrations/DatabaseContextModelSnapshot.cs @@ -0,0 +1,49 @@ +// +using System; +using Infrastructure; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Infrastructure.Migrations +{ + [DbContext(typeof(DatabaseContext))] + partial class DatabaseContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.0") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Infrastructure.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Balance") + .HasColumnType("numeric"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/back/Infrastructure/Repositories/UserRepo.cs b/back/Infrastructure/Repositories/UserRepo.cs index d6f6cb5..a075d6d 100644 --- a/back/Infrastructure/Repositories/UserRepo.cs +++ b/back/Infrastructure/Repositories/UserRepo.cs @@ -8,9 +8,9 @@ namespace Infrastructure.Repositories; public class UserRepo : IUserRepo { - public readonly DbContextFactory _factory; + public readonly IDbContextFactory _factory; - public UserRepo(DbContextFactory factory) + public UserRepo(IDbContextFactory factory) { _factory = factory; }