You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
API_SQLuedo/API_SQLuedo/API/Program.cs

155 lines
5.0 KiB

using API;
using API.Service;
using Asp.Versioning;
using DbContextLib;
using DbDataManager.Service;
using Dto;
using Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Shared;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IUserService<UserEntity>, UserDataService>();
builder.Services.AddScoped<IUserService<UserDto>, UserDataServiceApi>();
builder.Services.AddScoped<IBlackListService<BlackListEntity>, BlackListDataService>();
builder.Services.AddScoped<IBlackListService<BlackListDto>, BlackListDataServiceAPI>();
builder.Services.AddScoped<IInquiryService<InquiryEntity>, InquiryDataService>();
builder.Services.AddScoped<IInquiryService<InquiryDto>, InquiryDataServiceApi>();
builder.Services.AddScoped<ISolutionService<SolutionEntity>, SolutionDataService>();
builder.Services.AddScoped<ISolutionService<SolutionDto>, SolutionDataServiceAPI>();
builder.Services.AddScoped<IInquiryTableService<InquiryTableEntity>, InquiryTableDataService>();
builder.Services.AddScoped<IInquiryTableService<InquiryTableDto>, InquiryTableDataServiceAPI>();
builder.Services.AddScoped<IParagraphService<ParagraphEntity>, ParagraphDataService>();
builder.Services.AddScoped<IParagraphService<ParagraphDto>, ParagraphDataServiceApi>();
builder.Services.AddScoped<ISuccessService<SuccessEntity>, SuccessDataService>();
builder.Services.AddScoped<ISuccessService<SuccessDto>, SuccessDataServiceApi>();
builder.Services.AddScoped<ILessonService<LessonEntity>, LessonDataService>();
builder.Services.AddScoped<ILessonService<LessonDto>, LessonDataServiceApi>();
builder.Services.AddScoped<INotepadService<NotepadEntity>, NotepadDataService>();
builder.Services.AddScoped<INotepadService<NotepadDto>, NotepadDataServiceAPI>();
builder.Services.AddDbContext<DbContext, UserDbContext>();
builder.Services.AddDbContext<WebAPIDbContext>(options => options.UseInMemoryDatabase("appDb"));
builder.Services.AddIdentityApiEndpoints<IdentityUser>().AddEntityFrameworkStores<WebAPIDbContext>();
builder.Services.AddAuthorization();
builder.Services.AddApiVersioning(o =>
{
o.AssumeDefaultVersionWhenUnspecified = true;
o.DefaultApiVersion = new ApiVersion(1);
o.ReportApiVersions = true;
}).AddApiExplorer(
options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
//builder.Services.AddIdentityApiEndpoints<IdentityUser>()
// .AddEntityFrameworkStores<UserDbContext>();
builder.Services.AddSwaggerGen(option =>
{
option.SwaggerDoc("v1", new OpenApiInfo { Title = "Demo API", Version = "v1" });
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter a valid token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer"
});
option.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] { }
}
});
});
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder.WithOrigins("https://localhost:7171")
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
app.UseCors("AllowSpecificOrigin");
// Création de l'utilisateur admin pour la base de données Identity
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var userManager = services.GetRequiredService<UserManager<IdentityUser>>();
try
{
var user = new IdentityUser { UserName = "admin@example.com", Email = "admin@example.com" };
var result = await userManager.CreateAsync(user, "Mdp_1234");
if (result.Succeeded)
{
Console.WriteLine("Utilisateur admin créé avec succès.");
}
else
{
foreach (var error in result.Errors)
{
Console.WriteLine($"Erreur lors de la création de l'utilisateur : {error.Description}");
}
}
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "Une erreur s'est produite lors de la création de l'utilisateur.");
}
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapIdentityApi<IdentityUser>();
app.Run();