using Bibblan.Business.Clients; using Bibblan.Business.Services; using Bibblan.Models; using Bibblan.ViewModels; using Dapper; using GraphQL.Client.Abstractions; using GraphQL.Client.Http; using GraphQL.Client.Serializer.Newtonsoft; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using System.ComponentModel.DataAnnotations.Schema; using System.Net.Http; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddJsonFile($"appsettings.user.json", true, true); var configSection = builder.Configuration.GetSection(BibblanOptions.Bibblan); BibblanOptions config = new(); configSection.Bind(config); // Add services to the container. builder.Services.Configure(configSection); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddControllers(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi builder.Services.AddOpenApi(); //hardcover registration builder.Services.AddTransient();//måste vara transient builder.Services.AddScoped(s => new GraphQLHttpClient(new GraphQLHttpClientOptions { HttpMessageHandler = s.GetRequiredService(), EndPoint = new Uri(config.HardcoverApiUrl), }, new NewtonsoftJsonSerializer())); builder.Services.AddScoped(); //databases builder.Services.AddDbContext(options => options.UseSqlite($"Data Source={config.CalibreDb};Mode=ReadOnly;")); builder.Services.AddDbContext(options => options.UseNpgsql(config.BibblanConnection)); var app = builder.Build(); Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true; Dapper.SqlMapper.SetTypeMap( typeof(SeriesVm), new CustomPropertyTypeMap( typeof(SeriesVm), (type, columnName) => type.GetProperties().FirstOrDefault(prop => prop.GetCustomAttributes(false) .OfType() .Any(attr => attr.Name == columnName)))); app.UseDefaultFiles(); app.MapStaticAssets(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.MapFallbackToFile("/index.html"); app.Run();