using Microsoft.EntityFrameworkCore; using TheMoods.Data.Contexts; using TheMoods.Data.Initializer; var builder = WebApplication.CreateBuilder(args); // Add TenantDbContext with a default PostgreSQL connection for design-time/migrations builder.Services.AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection") ?? "Host=localhost;Database=themoods_tie;Username=themoods_ti_usr;Password=Thuytien965002")); // Add services to the container. builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); // Enable CORS for localhost and production subdomains builder.Services.AddCors(options => { options.AddDefaultPolicy(policy => { policy.SetIsOriginAllowed(origin => true) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); var app = builder.Build(); // Seed database and apply migrations on startup using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService(); // Tự động cập nhật Database (chạy các file Migration chưa chạy) context.Database.Migrate(); // Tạo dữ liệu mẫu nếu cần DbInitializer.Initialize(context); } catch (Exception ex) { var logger = services.GetRequiredService>(); logger.LogError(ex, "An error occurred while seeding or migrating the database."); } } // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseCors(); app.MapControllers(); app.Run(); record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) { public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); }