Skip to content

Commit

Permalink
fix: EnsureDatabase Minimal API e connection strings
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoPires committed Apr 1, 2022
1 parent ca879b4 commit d9583bf
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 68 deletions.
Binary file modified src/building-blocks/DevStore.WebAPI.Core/Identity/JwtConfig.cs
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static async Task EnsureSeedData(IServiceProvider serviceProvider)
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();

//var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var ssoContext = scope.ServiceProvider.GetRequiredService<BillingContext>();

await DbHealthChecker.TestConnection(ssoContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
}
},
"ConnectionStrings": {
//"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSBilling;Trusted_Connection=True;MultipleActiveResultSets=true"
"DefaultConnection": "Server=.;Initial Catalog=DSBilling;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSBilling;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"MessageQueueConnection": {
"MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static class DbMigrationHelpers
{
/// <summary>
/// Generate migrations before running this method, you can use command bellow:
/// Nuget package manager: Add-Migration DbInit -context PagamentosContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c PagamentosContext
/// Nuget package manager: Add-Migration DbInit -context CatalogContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c CatalogContext
/// </summary>
public static async Task EnsureSeedData(IServiceScope serviceScope)
{
Expand All @@ -29,7 +29,6 @@ public static async Task EnsureSeedData(IServiceProvider serviceProvider)
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();

//var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var context = scope.ServiceProvider.GetRequiredService<CatalogContext>();

await DbHealthChecker.TestConnection(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
}
},
"ConnectionStrings": {
//"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSCatalog;Trusted_Connection=True;MultipleActiveResultSets=true"
"DefaultConnection": "Server=.;Initial Catalog=DSCatalog;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSCatalog;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"MessageQueueConnection": {
"MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static class DbMigrationHelpers
{
/// <summary>
/// Generate migrations before running this method, you can use command bellow:
/// Nuget package manager: Add-Migration DbInit -context PagamentosContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c PagamentosContext
/// Nuget package manager: Add-Migration DbInit -context CustomerContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c CustomerContext
/// </summary>
public static async Task EnsureSeedData(IServiceScope serviceScope)
{
Expand All @@ -27,7 +27,6 @@ public static async Task EnsureSeedData(IServiceProvider serviceProvider)
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();

//var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var ssoContext = scope.ServiceProvider.GetRequiredService<CustomerContext>();

await DbHealthChecker.TestConnection(ssoContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
}
},
"ConnectionStrings": {
//"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Clientes;Trusted_Connection=True;MultipleActiveResultSets=true"
"DefaultConnection": "Server=.;Initial Catalog=DSCustomers;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSCustomers;Trusted_Connection=True;MultipleActiveResultSets=true",
},
"MessageQueueConnection": {
"MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static class DbMigrationHelpers
{
/// <summary>
/// Generate migrations before running this method, you can use command bellow:
/// Nuget package manager: Add-Migration DbInit -context PagamentosContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c PagamentosContext
/// Nuget package manager: Add-Migration DbInit -context ApplicationDbContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c ApplicationDbContext
/// </summary>
public static async Task EnsureSeedData(IServiceScope serviceScope)
{
Expand All @@ -27,7 +27,6 @@ public static async Task EnsureSeedData(IServiceProvider serviceProvider)
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();

//var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var ssoContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

await DbHealthChecker.TestConnection(ssoContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
}
},
"ConnectionStrings": {
//"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Users;Trusted_Connection=True;MultipleActiveResultSets=true"
"DefaultConnection": "Server=.;Initial Catalog=DSUsers;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSUsers;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"MessageQueueConnection": {
"MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static class DbMigrationHelpers
{
/// <summary>
/// Generate migrations before running this method, you can use command bellow:
/// Nuget package manager: Add-Migration DbInit -context PagamentosContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c PagamentosContext
/// Nuget package manager: Add-Migration DbInit -context OrdersContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c OrdersContext
/// </summary>
public static async Task EnsureSeedData(IServiceScope serviceScope)
{
Expand All @@ -27,7 +27,6 @@ public static async Task EnsureSeedData(IServiceProvider serviceProvider)
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();

//var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var ssoContext = scope.ServiceProvider.GetRequiredService<OrdersContext>();

await DbHealthChecker.TestConnection(ssoContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
//"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Orders;Trusted_Connection=True;MultipleActiveResultSets=true"
"DefaultConnection": "Server=.;Initial Catalog=DSOrders;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSOrders;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"MessageQueueConnection": {
"MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10"
Expand Down
16 changes: 4 additions & 12 deletions src/services/DevStore.ShoppingCart.API/Configuration/ApiConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DevStore.ShoppingCart.API.Services.gRPC;
using DevStore.WebAPI.Core.Identity;
using Microsoft.EntityFrameworkCore;

namespace DevStore.ShoppingCart.API.Configuration
Expand All @@ -11,8 +10,6 @@ public static void AddApiConfiguration(this IServiceCollection services, IConfig
services.AddDbContext<Data.ShoppingCartContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

services.AddControllers();

services.AddGrpc();

services.AddCors(options =>
Expand All @@ -26,7 +23,7 @@ public static void AddApiConfiguration(this IServiceCollection services, IConfig
});
}

public static void UseApiConfiguration(this IApplicationBuilder app, IWebHostEnvironment env)
public static void UseApiConfiguration(this WebApplication app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
Expand All @@ -35,17 +32,12 @@ public static void UseApiConfiguration(this IApplicationBuilder app, IWebHostEnv

app.UseHttpsRedirection();

app.UseRouting();

app.UseCors("Total");

app.UseAuthConfiguration();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapGrpcService<ShoppingCartGrpcService>().RequireCors("Total");
});
app.MapGrpcService<ShoppingCartGrpcService>().RequireCors("Total");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
using DevStore.WebAPI.Core.Configuration;
using DevStore.ShoppingCart.API.Data;
using DevStore.WebAPI.Core.Configuration;

namespace DevStore.ShoppingCart.API.Configuration
{

public static class DbMigrationHelpers
{
/// <summary>
/// Generate migrations before running this method, you can use command bellow:
/// Nuget package manager: Add-Migration DbInit -context PagamentosContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c PagamentosContext
/// Nuget package manager: Add-Migration DbInit -context ShoppingCartContext
/// Dotnet CLI: dotnet ef migrations add DbInit -c ShoppingCartContext
/// </summary>
public static async Task EnsureSeedData(IServiceScope serviceScope)
{
var services = serviceScope.ServiceProvider;
await EnsureSeedData(services);
}

public static async Task EnsureSeedData(IServiceProvider serviceProvider)
public static async Task EnsureSeedData(WebApplication app)
{
using var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
var env = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();

//var configuration = scope.ServiceProvider.GetRequiredService<IConfiguration>();
var context = scope.ServiceProvider.GetRequiredService<Data.ShoppingCartContext>();
using var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<ShoppingCartContext>();

await DbHealthChecker.TestConnection(context);

if (env.IsDevelopment())
if (app.Environment.IsDevelopment())
{
await context.Database.EnsureCreatedAsync();
}
}

}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DevStore.WebAPI.Core.User;
using DevStore.ShoppingCart.API.Data;
using DevStore.WebAPI.Core.User;

namespace DevStore.ShoppingCart.API.Configuration
{
Expand All @@ -8,7 +9,7 @@ public static void RegisterServices(this IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<IAspNetUser, AspNetUser>();
services.AddScoped<Data.ShoppingCartContext>();
services.AddScoped<ShoppingCartContext>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public static class SwaggerConfig
{
public static void AddSwaggerConfiguration(this IServiceCollection services)
{
services.AddEndpointsApiExplorer();

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo()
Expand Down Expand Up @@ -44,13 +46,10 @@ public static void AddSwaggerConfiguration(this IServiceCollection services)
});
}

public static void UseSwaggerConfiguration(this IApplicationBuilder app)
public static void UseSwaggerConfiguration(this WebApplication app)
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
});
app.UseSwaggerUI();
}
}
}
14 changes: 8 additions & 6 deletions src/services/DevStore.ShoppingCart.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

#region Configure Pipeline

var context = app.Services.GetRequiredService<ShoppingCartContext>();
var user = app.Services.GetRequiredService<IAspNetUser>();
var context = app.Services.CreateScope().ServiceProvider.GetRequiredService<ShoppingCartContext>();
var user = app.Services.CreateScope().ServiceProvider.GetRequiredService<IAspNetUser>();
var Errors = new List<string>();

app.UseSwaggerConfiguration();
Expand All @@ -48,6 +48,8 @@

MapActions(app);

DbMigrationHelpers.EnsureSeedData(app).Wait();

app.Run();

#endregion
Expand All @@ -61,7 +63,7 @@ void MapActions(WebApplication app)
.WithName("GetShoppingCart")
.WithTags("ShoppingCart");

app.MapPost("/fornecedor", [Authorize] async (
app.MapPost("/shopping-cart", [Authorize] async (
CartItem item) =>
{
var shoppingCart = await GetShoppingCartClient();
Expand All @@ -82,7 +84,7 @@ void MapActions(WebApplication app)
.WithName("AddItem")
.WithTags("ShoppingCart");

app.MapPut("/fornecedor/{productId}", [Authorize] async (
app.MapPut("/shopping-cart/{productId}", [Authorize] async (
Guid productId,
CartItem item) =>
{
Expand All @@ -107,7 +109,7 @@ void MapActions(WebApplication app)
.WithName("UpdateItem")
.WithTags("ShoppingCart");

app.MapDelete("/fornecedor/{productId}", [Authorize] async (
app.MapDelete("/shopping-cart/{productId}", [Authorize] async (
Guid productId) =>
{
var cart = await GetShoppingCartClient();
Expand All @@ -133,7 +135,7 @@ void MapActions(WebApplication app)
.WithName("RemoveItem")
.WithTags("ShoppingCart");

app.MapPost("/fornecedor/apply-voucher", [Authorize] async (
app.MapPost("/shopping-cart/apply-voucher", [Authorize] async (
Voucher voucher) =>
{
var cart = await GetShoppingCartClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=.;Initial Catalog=DSShoppingCart;Persist Security Info=False;User ID=sa;Password=@Password1;MultipleActiveResultSets=False;Connection Timeout=30;"
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=DSShoppingCart;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"MessageQueueConnection": {
"MessageBus": "host=localhost:5672;publisherConfirms=true;timeout=10"
Expand Down

0 comments on commit d9583bf

Please sign in to comment.