From a7b43428919b650db6cfd9223689948f4968662d Mon Sep 17 00:00:00 2001
From: Lucca Santangelo <luccasant95@gmail.com>
Date: Tue, 10 Nov 2020 21:25:16 -0300
Subject: [PATCH] enviroment configuration

---
 Tsi1.Api/Tsi1.Api/Startup.cs                  | 32 +++++++++++++------
 Tsi1.Api/Tsi1.Api/appsettings.json            |  1 -
 .../DesignTimeDbContextFactory.cs             |  8 +----
 3 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs
index f095493..b15b91c 100644
--- a/Tsi1.Api/Tsi1.Api/Startup.cs
+++ b/Tsi1.Api/Tsi1.Api/Startup.cs
@@ -31,25 +31,35 @@ namespace Tsi1.Api
 {
     public class Startup
     {
-        public Startup(IConfiguration configuration)
+        public Startup(IConfiguration configuration, IWebHostEnvironment env)
         {
             Configuration = configuration;
+            _env = env;
         }
 
         public IConfiguration Configuration { get; }
+        private readonly IWebHostEnvironment _env;
 
         // This method gets called by the runtime. Use this method to add services to the container.
         public void ConfigureServices(IServiceCollection services)
         {
-            services.AddControllers();
+            string postgreSqlSection;
+            string mongoDbSection;
+            if (_env.IsProduction())
+            {
+                postgreSqlSection = "PostgreSqlCloud";
+                mongoDbSection = "Tsi1DatabaseSettingsCloud";
+            }
+            else
+            {
+                postgreSqlSection = "PostgreSql";
+                mongoDbSection = "Tsi1DatabaseSettings";
+            }
 
+            services.AddControllers();
+            
             services.AddSignalR();
 
-            var isElasticCloud = bool.Parse(Configuration.GetSection("IsElasticCloud").Value);
-
-            var postgreSqlSection = isElasticCloud ? "PostgreSqlCloud" : "PostgreSql";
-            var mongoDbSection = isElasticCloud ? "Tsi1DatabaseSettingsCloud" : "Tsi1DatabaseSettings";
-
             var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get<JwtTokenConfig>();
 
             services.AddDbContext<Tsi1Context>(x => x.UseNpgsql(Configuration.GetConnectionString(postgreSqlSection)));
@@ -167,12 +177,16 @@ namespace Tsi1.Api
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
-        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
+        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Tsi1Context context)
         {
+            if (env.IsProduction())
+            {
+                context.Database.Migrate();
+            }
+
             if (env.IsDevelopment())
             {
                 app.UseDeveloperExceptionPage();
-                
             }
 
             app.UseSwagger();
diff --git a/Tsi1.Api/Tsi1.Api/appsettings.json b/Tsi1.Api/Tsi1.Api/appsettings.json
index 48f2b50..aeffabd 100644
--- a/Tsi1.Api/Tsi1.Api/appsettings.json
+++ b/Tsi1.Api/Tsi1.Api/appsettings.json
@@ -1,5 +1,4 @@
 {
-  "IsElasticCloud": true,
   "ConnectionStrings": {
     "PostgreSql": "Host=localhost;Database=tsi1;Username=postgres;Password=111111",
     "PostgreSqlCloud": "Host=postgres;Database=tsi1;Username=postgres;Password=postgres"
diff --git a/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs b/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs
index bbddd4c..781545a 100644
--- a/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs
+++ b/Tsi1.Api/Tsi1.DataLayer/DesignTimeDbContextFactory.cs
@@ -1,10 +1,7 @@
 using Microsoft.EntityFrameworkCore;
 using Microsoft.EntityFrameworkCore.Design;
 using Microsoft.Extensions.Configuration;
-using System;
-using System.Collections.Generic;
 using System.IO;
-using System.Text;
 
 namespace Tsi1.DataLayer
 {
@@ -17,10 +14,7 @@ namespace Tsi1.DataLayer
                 .AddJsonFile(@Directory.GetCurrentDirectory() + "/../Tsi1.Api/appsettings.json")
                 .Build();
 
-            var isElasticCloud = bool.Parse(configuration.GetSection("IsElasticCloud").Value);
-            var databaseSection = isElasticCloud ? "PostgreSqlCloud" : "PostgreSql";
-
-            var connectionString = configuration.GetConnectionString(databaseSection);
+            var connectionString = configuration.GetConnectionString("PostgreSql");
             var builder = new DbContextOptionsBuilder<Tsi1Context>();
             builder.UseNpgsql(connectionString);
 
-- 
GitLab