From 810f23f814305f22dc9daf4380e0b803fe3ad8a8 Mon Sep 17 00:00:00 2001 From: csimonapastore Date: Fri, 21 Feb 2025 13:21:06 +0100 Subject: [PATCH] Adding new tests --- MainProject.Tests/JsonData/appsettings.json | 5 + .../JsonData/completeAppSettings.json | 5 + .../noDbConfigurationAppSettings.json | 16 +++ MainProject.Tests/Utils/ProgramUtils_Tests.cs | 87 ++++++++++++ .../Core/Database/PostgreSqlDbContext.cs | 18 +++ MainProject/MainProject.csproj | 6 +- .../20240904211920_InitialCreate.Designer.cs | 132 ++++++++++++++++++ .../20240904211920_InitialCreate.cs | 79 +++++++++++ .../Migrations/20241129231345_Try.Designer.cs | 132 ++++++++++++++++++ MainProject/Migrations/20241129231345_Try.cs | 22 +++ .../SqlServerContextModelSnapshot.cs | 129 +++++++++++++++++ .../Models/Settings/DatabaseSettings.cs | 1 + MainProject/Utils/ProgramUtils.cs | 10 ++ MainProject/appsettings.json | 3 +- README.md | 24 +++- 15 files changed, 659 insertions(+), 10 deletions(-) create mode 100644 MainProject.Tests/JsonData/noDbConfigurationAppSettings.json create mode 100644 MainProject/Core/Database/PostgreSqlDbContext.cs create mode 100644 MainProject/Migrations/20240904211920_InitialCreate.Designer.cs create mode 100644 MainProject/Migrations/20240904211920_InitialCreate.cs create mode 100644 MainProject/Migrations/20241129231345_Try.Designer.cs create mode 100644 MainProject/Migrations/20241129231345_Try.cs create mode 100644 MainProject/Migrations/SqlServerContextModelSnapshot.cs diff --git a/MainProject.Tests/JsonData/appsettings.json b/MainProject.Tests/JsonData/appsettings.json index 0e20c40..58c3663 100644 --- a/MainProject.Tests/JsonData/appsettings.json +++ b/MainProject.Tests/JsonData/appsettings.json @@ -6,6 +6,11 @@ "Version": "v1.0", "Description": "This template contains basic configuration for a .Net 8 backend" }, + "DatabaseSettings": { + "SqlServerConnectionString": "SQLSERVER_DB_SERVER", + "MongoDbConnectionString": "MONGO_DB_SERVER", + "PostgreSQLConnectionString": "POSTGRESQL_DB_SERVER" + }, "OpenApiSettings": { "TermsOfServiceUrl": "", "OpenApiContact": { diff --git a/MainProject.Tests/JsonData/completeAppSettings.json b/MainProject.Tests/JsonData/completeAppSettings.json index 38b0a1d..979fcf0 100644 --- a/MainProject.Tests/JsonData/completeAppSettings.json +++ b/MainProject.Tests/JsonData/completeAppSettings.json @@ -6,6 +6,11 @@ "Version": "v1.0", "Description": "This template contains basic configuration for a .Net 8 backend" }, + "DatabaseSettings": { + "SqlServerConnectionString": "SQLSERVER_DB_SERVER", + "MongoDbConnectionString": "MONGO_DB_SERVER", + "PostgreSQLConnectionString": "POSTGRESQL_DB_SERVER" + }, "OpenApiSettings": { "TermsOfServiceUrl": "https://github.com/csimonapastore/BasicDotnetTemplate/blob/main/LICENSE.md", "OpenApiContact": { diff --git a/MainProject.Tests/JsonData/noDbConfigurationAppSettings.json b/MainProject.Tests/JsonData/noDbConfigurationAppSettings.json new file mode 100644 index 0000000..1b13ea4 --- /dev/null +++ b/MainProject.Tests/JsonData/noDbConfigurationAppSettings.json @@ -0,0 +1,16 @@ +{ + "AppSettings" : + { + "Settings": { + "Name": "MainProject", + "Version": "v1.0", + "Description": "This template contains basic configuration for a .Net 8 backend" + }, + "DatabaseSettings": { + "SqlServerConnectionString": "", + "MongoDbConnectionString": "", + "PostgreSQLConnectionString": "" + } + } + +} \ No newline at end of file diff --git a/MainProject.Tests/Utils/ProgramUtils_Tests.cs b/MainProject.Tests/Utils/ProgramUtils_Tests.cs index 45b2581..7397366 100644 --- a/MainProject.Tests/Utils/ProgramUtils_Tests.cs +++ b/MainProject.Tests/Utils/ProgramUtils_Tests.cs @@ -231,6 +231,93 @@ public class ProgramUtils_Tests Assert.Fail($"An exception was thrown: {ex.Message}"); } } + + [TestMethod] + public void AddSqlServerContext_Valid() + { + try + { + WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); + AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData"); + ProgramUtils.AddDbContext(ref builder, appSettings); + AppSettings _appSettings = new AppSettings(); + builder.Configuration.GetSection("AppSettings").Bind(_appSettings); + var areEquals = appSettings.DatabaseSettings?.SqlServerConnectionString == _appSettings.DatabaseSettings?.SqlServerConnectionString; + Assert.IsTrue(areEquals); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + [TestMethod] + public void AddMongoDbContext_Valid() + { + try + { + WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); + AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData"); + ProgramUtils.AddDbContext(ref builder, appSettings); + AppSettings _appSettings = new AppSettings(); + builder.Configuration.GetSection("AppSettings").Bind(_appSettings); + var areEquals = appSettings.DatabaseSettings?.MongoDbConnectionString == _appSettings.DatabaseSettings?.MongoDbConnectionString; + Assert.IsTrue(areEquals); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + [TestMethod] + public void AddPostgreSqlDbContext_Valid() + { + try + { + WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); + AppSettings appSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData"); + ProgramUtils.AddDbContext(ref builder, appSettings); + AppSettings _appSettings = new AppSettings(); + builder.Configuration.GetSection("AppSettings").Bind(_appSettings); + var areEquals = appSettings.DatabaseSettings?.PostgreSQLConnectionString == _appSettings.DatabaseSettings?.PostgreSQLConnectionString; + Assert.IsTrue(areEquals); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + [TestMethod] + public void NoSqlServerContext_Empty() + { + try + { + DatabaseSettings expectedDbSettings = new DatabaseSettings() + { + SqlServerConnectionString = "" + }; + + WebApplicationBuilder builder = WebApplication.CreateBuilder(Array.Empty()); + AppSettings realAppSettings = ProgramUtils.AddConfiguration(ref builder, System.AppDomain.CurrentDomain.BaseDirectory + "/JsonData", "noApiConfigurationAppSettings.json"); + ProgramUtils.AddDbContext(ref builder, realAppSettings); + + var areEquals = expectedDbSettings.SqlServerConnectionString == realAppSettings.DatabaseSettings?.SqlServerConnectionString; + Console.WriteLine(realAppSettings.DatabaseSettings?.SqlServerConnectionString); + Assert.IsTrue(areEquals); + } + catch (Exception ex) + { + Console.WriteLine(ex.InnerException); + Assert.Fail($"An exception was thrown: {ex.Message}"); + } + } + + } diff --git a/MainProject/Core/Database/PostgreSqlDbContext.cs b/MainProject/Core/Database/PostgreSqlDbContext.cs new file mode 100644 index 0000000..90f6acb --- /dev/null +++ b/MainProject/Core/Database/PostgreSqlDbContext.cs @@ -0,0 +1,18 @@ +using Microsoft.EntityFrameworkCore; +using BasicDotnetTemplate.MainProject.Models.Database.SqlServer; + + +namespace BasicDotnetTemplate.MainProject.Core.Database +{ + public class PostgreSqlDbContext : DbContext + { + + public PostgreSqlDbContext(DbContextOptions options) + : base(options) + { + } + } +} + + + diff --git a/MainProject/MainProject.csproj b/MainProject/MainProject.csproj index b004205..7aab2b7 100644 --- a/MainProject/MainProject.csproj +++ b/MainProject/MainProject.csproj @@ -13,8 +13,8 @@ all - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -30,7 +30,7 @@ - + diff --git a/MainProject/Migrations/20240904211920_InitialCreate.Designer.cs b/MainProject/Migrations/20240904211920_InitialCreate.Designer.cs new file mode 100644 index 0000000..8c2d64c --- /dev/null +++ b/MainProject/Migrations/20240904211920_InitialCreate.Designer.cs @@ -0,0 +1,132 @@ +// +using System; +using BasicDotnetTemplate.MainProject.Core.Database; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MainProject.Migrations +{ + [DbContext(typeof(SqlServerContext))] + [Migration("20240904211920_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreationTime") + .HasColumnType("datetime2"); + + b.Property("CreationUserId") + .HasColumnType("int"); + + b.Property("DeletionTime") + .HasColumnType("datetime2"); + + b.Property("DeletionUserId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Role"); + }); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreationTime") + .HasColumnType("datetime2"); + + b.Property("CreationUserId") + .HasColumnType("int"); + + b.Property("DeletionTime") + .HasColumnType("datetime2"); + + b.Property("DeletionUserId") + .HasColumnType("int"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("int"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("int"); + + b.Property("Username") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b => + { + b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role") + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MainProject/Migrations/20240904211920_InitialCreate.cs b/MainProject/Migrations/20240904211920_InitialCreate.cs new file mode 100644 index 0000000..ed4ccbc --- /dev/null +++ b/MainProject/Migrations/20240904211920_InitialCreate.cs @@ -0,0 +1,79 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MainProject.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Role", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + CreationTime = table.Column(type: "datetime2", nullable: false), + CreationUserId = table.Column(type: "int", nullable: false), + UpdateTime = table.Column(type: "datetime2", nullable: false), + UpdateUserId = table.Column(type: "int", nullable: false), + DeletionTime = table.Column(type: "datetime2", nullable: false), + DeletionUserId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Role", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Username = table.Column(type: "nvarchar(max)", nullable: false), + FirstName = table.Column(type: "nvarchar(max)", nullable: false), + LastName = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false), + RoleId = table.Column(type: "int", nullable: false), + PasswordHash = table.Column(type: "nvarchar(max)", nullable: false), + CreationTime = table.Column(type: "datetime2", nullable: false), + CreationUserId = table.Column(type: "int", nullable: false), + UpdateTime = table.Column(type: "datetime2", nullable: false), + UpdateUserId = table.Column(type: "int", nullable: false), + DeletionTime = table.Column(type: "datetime2", nullable: false), + DeletionUserId = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + table.ForeignKey( + name: "FK_Users_Role_RoleId", + column: x => x.RoleId, + principalTable: "Role", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Users_RoleId", + table: "Users", + column: "RoleId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + + migrationBuilder.DropTable( + name: "Role"); + } + } +} diff --git a/MainProject/Migrations/20241129231345_Try.Designer.cs b/MainProject/Migrations/20241129231345_Try.Designer.cs new file mode 100644 index 0000000..c301bf9 --- /dev/null +++ b/MainProject/Migrations/20241129231345_Try.Designer.cs @@ -0,0 +1,132 @@ +// +using System; +using BasicDotnetTemplate.MainProject.Core.Database; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MainProject.Migrations +{ + [DbContext(typeof(SqlServerContext))] + [Migration("20241129231345_Try")] + partial class Try + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreationTime") + .HasColumnType("datetime2"); + + b.Property("CreationUserId") + .HasColumnType("int"); + + b.Property("DeletionTime") + .HasColumnType("datetime2"); + + b.Property("DeletionUserId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Role"); + }); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreationTime") + .HasColumnType("datetime2"); + + b.Property("CreationUserId") + .HasColumnType("int"); + + b.Property("DeletionTime") + .HasColumnType("datetime2"); + + b.Property("DeletionUserId") + .HasColumnType("int"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("int"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("int"); + + b.Property("Username") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b => + { + b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role") + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MainProject/Migrations/20241129231345_Try.cs b/MainProject/Migrations/20241129231345_Try.cs new file mode 100644 index 0000000..46addf6 --- /dev/null +++ b/MainProject/Migrations/20241129231345_Try.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MainProject.Migrations +{ + /// + public partial class Try : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/MainProject/Migrations/SqlServerContextModelSnapshot.cs b/MainProject/Migrations/SqlServerContextModelSnapshot.cs new file mode 100644 index 0000000..90248ac --- /dev/null +++ b/MainProject/Migrations/SqlServerContextModelSnapshot.cs @@ -0,0 +1,129 @@ +// +using System; +using BasicDotnetTemplate.MainProject.Core.Database; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MainProject.Migrations +{ + [DbContext(typeof(SqlServerContext))] + partial class SqlServerContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.8") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreationTime") + .HasColumnType("datetime2"); + + b.Property("CreationUserId") + .HasColumnType("int"); + + b.Property("DeletionTime") + .HasColumnType("datetime2"); + + b.Property("DeletionUserId") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Role"); + }); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CreationTime") + .HasColumnType("datetime2"); + + b.Property("CreationUserId") + .HasColumnType("int"); + + b.Property("DeletionTime") + .HasColumnType("datetime2"); + + b.Property("DeletionUserId") + .HasColumnType("int"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("RoleId") + .HasColumnType("int"); + + b.Property("UpdateTime") + .HasColumnType("datetime2"); + + b.Property("UpdateUserId") + .HasColumnType("int"); + + b.Property("Username") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.User", b => + { + b.HasOne("BasicDotnetTemplate.MainProject.Models.Database.SqlServer.Role", "Role") + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Role"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/MainProject/Models/Settings/DatabaseSettings.cs b/MainProject/Models/Settings/DatabaseSettings.cs index 517d105..3ac3be2 100644 --- a/MainProject/Models/Settings/DatabaseSettings.cs +++ b/MainProject/Models/Settings/DatabaseSettings.cs @@ -5,5 +5,6 @@ public class DatabaseSettings #nullable enable public string? SqlServerConnectionString { get; set; } public string? MongoDbConnectionString { get; set; } + public string? PostgreSQLConnectionString { get; set; } #nullable disable } \ No newline at end of file diff --git a/MainProject/Utils/ProgramUtils.cs b/MainProject/Utils/ProgramUtils.cs index 42c462e..22587c1 100644 --- a/MainProject/Utils/ProgramUtils.cs +++ b/MainProject/Utils/ProgramUtils.cs @@ -155,6 +155,16 @@ public static class ProgramUtils messages = messages + (String.IsNullOrEmpty(messages) ? "" : ", ") + "MongoDbContext"; } } + + if (!String.IsNullOrEmpty(appSettings.DatabaseSettings?.PostgreSQLConnectionString)) + { + var connectionString = appSettings.DatabaseSettings?.PostgreSQLConnectionString ?? String.Empty; + connectionString = connectionString.Replace("POSTGRESQL_DB_SERVER", Environment.GetEnvironmentVariable("POSTGRESQL_DB_SERVER")); + builder.Services.AddDbContext(options => + options.UseNpgsql(connectionString) + ); + messages = messages + (String.IsNullOrEmpty(messages) ? "" : ", ") + "PostgreSqlDbContext"; + } messages = String.IsNullOrEmpty(messages) ? "No context" : messages; Logger.Info($"[ProgramUtils][AddDbContext] {messages} added"); } diff --git a/MainProject/appsettings.json b/MainProject/appsettings.json index a61bba2..0fb5b09 100644 --- a/MainProject/appsettings.json +++ b/MainProject/appsettings.json @@ -8,7 +8,8 @@ }, "DatabaseSettings": { "SqlServerConnectionString": "SQLSERVER_DB_SERVER", - "MongoDbConnectionString": "MONGO_DB_SERVER" + "MongoDbConnectionString": "MONGO_DB_SERVER", + "PostgreSQLConnectionString": "POSTGRESQL_DB_SERVER" }, "OpenApiSettings": { "TermsOfServiceUrl": "", diff --git a/README.md b/README.md index 6ee4270..d7f0f0f 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,12 @@ - [What is BasicDotnetTemplate?](#what-is-basicdotnettemplate) - [Technologies](#technologies) - [Dotnet](#dotnet) + - [Commands](#commands) + - [Debug](#debug) + - [Entity Framework](#entity-framework) - [Sonarcloud](#sonarcloud) - [Quality gate](#quality-gate) - [Code smells](#code-smells) - - [Entity Framework](#entity-framework) ## What is BasicDotnetTemplate? BasicDotnetTemplate is a basic project written in .NET 8. It contains MainProject, a WebApi project, and MainProject.Tests written in .NET 8 that contains tests for MainProject. @@ -21,6 +23,20 @@ Every component is developed using **dotnet-core 8.0.201** and was generated wit > .NET is the free, open-source, cross-platform framework for building modern apps and powerful cloud services. Supported on Windows, Linux, and macOS. +#### Commands +##### Debug +You can use the following comands in the MainProject folder. +```bash +dotnet watch run +``` +Restarts or hot reloads the specified application, or runs a specified dotnet command, when changes in source code are detected. + +### Entity Framework +This project uses Entity Framework Core, you can find the official documentation [here](https://www.entityframeworktutorial.net/efcore/entity-framework-core.aspx). +> Entity Framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database. +> EF Core supports two development approaches 1) Code-First 2) Database-First. +> In the code-first approach, EF Core API creates the database and tables using migration based on the conventions and configuration provided in your domain classes. This approach is useful in Domain Driven Design (DDD). + ### Sonarcloud [![SonarCloud](https://sonarcloud.io/images/project_badges/sonarcloud-white.svg)](https://sonarcloud.io/summary/new_code?id=csimonapastore_BasicDotnetTemplate) @@ -49,9 +65,5 @@ This project uses **Sonar way** quality gate: > Code smells are usually not bugs; they are not technically incorrect and do not prevent the program from functioning. Instead, they indicate weaknesses in design that may slow down development or increase the risk of bugs or failures in the future. Bad code smells can be an indicator of factors that contribute to technical debt.[Tufano, Michele; Palomba, Fabio; Bavota, Gabriele; Oliveto, Rocco; Di Penta, Massimiliano; De Lucia, Andrea; Poshyvanyk, Denys (2015).] Robert C. Martin calls a list of code smells a "value system" for software craftsmanship.[Martin, Robert C. (2009). "17: Smells and Heuristics". Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall.] > -### Entity Framework -This project uses Entity Framework Core, you can find the official documentation [here](https://www.entityframeworktutorial.net/efcore/entity-framework-core.aspx). -> Entity Framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database. -> EF Core supports two development approaches 1) Code-First 2) Database-First. -> In the code-first approach, EF Core API creates the database and tables using migration based on the conventions and configuration provided in your domain classes. This approach is useful in Domain Driven Design (DDD). +