Files
Portfolio/src/Portfolio.Api/Program.cs

105 lines
3.0 KiB
C#

using HopFrame.Core.Config;
using HopFrame.Web;
using Microsoft.EntityFrameworkCore;
using Portfolio.Api;
using Portfolio.Api.Services;
using Portfolio.Shared.Models;
using Portfolio.Shared.Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.AddServiceDefaults();
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
builder.AddNpgsqlDbContext<DatabaseContext>("data");
builder.Services.AddScoped<IProjectRepository, ProjectRepository>();
builder.Services.AddScoped<ITechnologyRepository, TechnologyRepository>();
builder.Services.AddScoped<ITimelineRepository, TimelineRepository>();
builder.Services.AddScoped<IAboutRepository, AboutRepository>();
builder.Services.AddControllers();
builder.Services.AddHopFrame(options => {
options.DisplayUserInfo(false);
options.AddDbContext<DatabaseContext>(context => {
context.Table<Project>(table => {
var langConfig = table.InnerConfig.Properties
.Single(prop => prop.Name == nameof(Project.Languages));
langConfig
.GetType()!
.GetProperty(nameof(PropertyConfig.IsRelation))!
.SetValue(langConfig, true);
langConfig
.GetType()!
.GetProperty(nameof(PropertyConfig.IsEnumerable))!
.SetValue(langConfig, true);
langConfig
.GetType()!
.GetProperty(nameof(PropertyConfig.IsRequired))!
.SetValue(langConfig, true);
table.Property(p => p.Languages)
.FormatEach<Technology>((l, _) => l.Name)
.List(false);
table.Property(p => p.Cover)
.List(false);
table.Property(p => p.Description)
.List(false)
.IsTextArea(true);
table.Property(p => p.SourceCode)
.List(false);
});
context.Table<About>(table => {
table.Property(a => a.AboutMe)
.List(false)
.IsTextArea(true);
table.Property(a => a.Future)
.List(false)
.IsTextArea(true);
});
context.Table<TimelineEntry>(table => {
table.Property(t => t.Description)
.IsTextArea(true)
.List(false);
});
});
});
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.MapOpenApi();
}
await using (var scope = app.Services.CreateAsyncScope()) {
var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
await db.Database.EnsureCreatedAsync();
}
app.MapDefaultEndpoints();
app.MapControllers();
app.UseAntiforgery();
app.MapStaticAssets();
app.MapRazorComponents<App>()
.MapHopFramePages();
app.Run();