Добавил два эндпоинта в дев для инициализации и миграции бд
This commit is contained in:
parent
046e3a8f4c
commit
739825b4c1
@ -7,6 +7,7 @@ public sealed class DatabaseContext : DbContext
|
|||||||
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
||||||
: base(options)
|
: base(options)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbSet<WeatherForecast> Forecasts => Set<WeatherForecast>();
|
public DbSet<WeatherForecast> Forecasts => Set<WeatherForecast>();
|
||||||
@ -49,9 +50,6 @@ public class WeatherForecast
|
|||||||
Summary = summary;
|
Summary = summary;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WeatherForecast()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public DateOnly Date { get; set; }
|
public DateOnly Date { get; set; }
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
using HackathonPreparing.ApiService;
|
using HackathonPreparing.ApiService;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen(c =>
|
builder.Services.AddSwaggerGen(c =>
|
||||||
{
|
{
|
||||||
@ -19,23 +21,40 @@ builder.AddServiceDefaults();
|
|||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddProblemDetails();
|
builder.Services.AddProblemDetails();
|
||||||
|
|
||||||
|
|
||||||
builder.AddNpgsqlDbContext<DatabaseContext>("prod", null,
|
builder.AddNpgsqlDbContext<DatabaseContext>("prod", null,
|
||||||
optionsBuilder => optionsBuilder.UseNpgsql(npgsqlBuilder =>
|
optionsBuilder => optionsBuilder.UseNpgsql(npgsqlBuilder =>
|
||||||
npgsqlBuilder.MigrationsAssembly(typeof(Program).Assembly.GetName().Name)));
|
npgsqlBuilder.MigrationsAssembly(typeof(Program).Assembly.GetName().Name)));
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
|
||||||
|
var logger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (app.Environment.IsDevelopment())
|
if (app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
|
logger.LogWarning("!!!!!!! Call /ensure-created enpoint or /migrate if no db !!!!!!!!");
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI(c =>
|
app.UseSwaggerUI(c =>
|
||||||
{
|
{
|
||||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Weather Forecast API V1");
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Weather Forecast API V1");
|
||||||
});
|
});
|
||||||
|
app.MapGet("/ensure-created", async (DatabaseContext db) => await db.Database.EnsureCreatedAsync());
|
||||||
|
|
||||||
|
app.MapGet("/migrate", async (DatabaseContext db) => await db.Database.MigrateAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
app.UseExceptionHandler();
|
app.UseExceptionHandler();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.MapGet("/weatherforecast", async (DatabaseContext db) => await db.Forecasts.ToListAsync());
|
app.MapGet("/weatherforecast", async (DatabaseContext db) => await db.Forecasts.ToListAsync());
|
||||||
|
|
||||||
app.MapDefaultEndpoints();
|
app.MapDefaultEndpoints();
|
||||||
|
43
HackathonPreparing/HackathonPreparing/HackathonPreparing.sln
Normal file
43
HackathonPreparing/HackathonPreparing/HackathonPreparing.sln
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.5.002.0
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HackathonPreparing.ApiService", "HackathonPreparing.ApiService\HackathonPreparing.ApiService.csproj", "{DF197643-F98E-43D0-8C93-78C27D11CB26}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HackathonPreparing.AppHost", "HackathonPreparing.AppHost\HackathonPreparing.AppHost.csproj", "{DD3AB30E-1CB0-4E60-B513-7BBA4A0EA7B0}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HackathonPreparing.ServiceDefaults", "HackathonPreparing.ServiceDefaults\HackathonPreparing.ServiceDefaults.csproj", "{29EC9110-27F1-45F9-A351-8EF6A12A1D5C}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HackathonPreparing.Web", "HackathonPreparing.Web\HackathonPreparing.Web.csproj", "{164763E8-DED7-4C3C-8F36-095CB4F863C7}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{DF197643-F98E-43D0-8C93-78C27D11CB26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{DF197643-F98E-43D0-8C93-78C27D11CB26}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{DF197643-F98E-43D0-8C93-78C27D11CB26}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{DF197643-F98E-43D0-8C93-78C27D11CB26}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{DD3AB30E-1CB0-4E60-B513-7BBA4A0EA7B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{DD3AB30E-1CB0-4E60-B513-7BBA4A0EA7B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{DD3AB30E-1CB0-4E60-B513-7BBA4A0EA7B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{DD3AB30E-1CB0-4E60-B513-7BBA4A0EA7B0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{29EC9110-27F1-45F9-A351-8EF6A12A1D5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{29EC9110-27F1-45F9-A351-8EF6A12A1D5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{29EC9110-27F1-45F9-A351-8EF6A12A1D5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{29EC9110-27F1-45F9-A351-8EF6A12A1D5C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{164763E8-DED7-4C3C-8F36-095CB4F863C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{164763E8-DED7-4C3C-8F36-095CB4F863C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{164763E8-DED7-4C3C-8F36-095CB4F863C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{164763E8-DED7-4C3C-8F36-095CB4F863C7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {80C5F09A-371B-4BC1-9514-36F72E4A64F9}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
Loading…
x
Reference in New Issue
Block a user