diff --git a/Inflow.Api/Extensions/ConfigureServicesExtensions.cs b/Inflow.Api/Extensions/ConfigureServicesExtensions.cs index ede9df6..d84769f 100644 --- a/Inflow.Api/Extensions/ConfigureServicesExtensions.cs +++ b/Inflow.Api/Extensions/ConfigureServicesExtensions.cs @@ -1,7 +1,70 @@ -namespace Inflow.Api.Extensions +using DiyorMarket.Infrastructure.Persistence.Repositories; +using DiyorMarket.Services; +using Inflow.Domain.Intefaces.Services; +using Inflow.Domain.Interfaces.Repositories; +using Inflow.Domain.Interfaces.Services; +using Inflow.Infrastructure; +using Microsoft.EntityFrameworkCore; +using Serilog; + +namespace Inflow.Api.Extensions { - public class ConfigureServicesExtensions + public static class ConfigureServicesExtensions { + public static IServiceCollection ConfigureRepositories(this IServiceCollection services) + { + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddSingleton(); + + services.AddControllers() + .AddNewtonsoftJson(options => + options.SerializerSettings.ReferenceLoopHandling = + Newtonsoft.Json.ReferenceLoopHandling.Ignore + ); + + return services; + } + + public static IServiceCollection ConfigureLogger(this IServiceCollection services) + { + Log.Logger = new LoggerConfiguration() + .MinimumLevel.Information() + .WriteTo.Console() + .WriteTo.File("logs/logs.txt", rollingInterval: RollingInterval.Day) + .WriteTo.File("logs/error_.txt", Serilog.Events.LogEventLevel.Error, rollingInterval: RollingInterval.Day) + .CreateLogger(); + + return services; + } + + public static IServiceCollection ConfigureDatabaseContext(this IServiceCollection services) + { + var builder = WebApplication.CreateBuilder(); + + services.AddDbContext(options => + options.UseSqlServer(builder.Configuration.GetConnectionString("DiyorMarketConection"))); + + return services; + } } } + diff --git a/Inflow.Api/Inflow.Api.csproj b/Inflow.Api/Inflow.Api.csproj index 4145a61..8c6b36a 100644 --- a/Inflow.Api/Inflow.Api.csproj +++ b/Inflow.Api/Inflow.Api.csproj @@ -11,6 +11,7 @@ + @@ -20,6 +21,10 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + + + @@ -27,6 +32,7 @@ + diff --git a/Inflow.Api/Program.cs b/Inflow.Api/Program.cs index 48863a6..977f2df 100644 --- a/Inflow.Api/Program.cs +++ b/Inflow.Api/Program.cs @@ -1,14 +1,56 @@ +using Inflow.Api.Extensions; +using Inflow.Api.Middlewares; +using Microsoft.AspNetCore.StaticFiles; +using Microsoft.IdentityModel.Tokens; +using Newtonsoft.Json.Serialization; +using Serilog; +using System.Text; + var builder = WebApplication.CreateBuilder(args); -// Add services to the container. +builder.Host.UseSerilog(); + +builder.Services.AddControllers() + .AddNewtonsoftJson(options => + { + options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; + }) + .AddXmlSerializerFormatters(); -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddSingleton(); +builder.Services.ConfigureLogger(); +builder.Services.ConfigureRepositories(); +builder.Services.ConfigureDatabaseContext(); +builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); +builder.Services.AddAuthentication("Bearer") + .AddJwtBearer(options => + { + options.TokenValidationParameters = new() + { + ValidateIssuer = true, + ValidateAudience = true, + ValidateIssuerSigningKey = true, + ValidIssuer = "anvar-api", + ValidAudience = "anvar-mobile", + IssuerSigningKey = new SymmetricSecurityKey( + Encoding.UTF8.GetBytes("anvarSekretKalitSozMalades")), + ValidateLifetime = true, + }; + }); var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var services = scope.ServiceProvider; + builder.Services.SeedDatabase(services); +} + +app.UseMiddleware(); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -18,6 +60,7 @@ app.UseHttpsRedirection(); +app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); diff --git a/Inflow.Domain/Inflow.Domain.csproj b/Inflow.Domain/Inflow.Domain.csproj index 4f1f464..dbed63f 100644 --- a/Inflow.Domain/Inflow.Domain.csproj +++ b/Inflow.Domain/Inflow.Domain.csproj @@ -15,6 +15,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Inflow.Infrastructure/Inflow.Infrastructure.csproj b/Inflow.Infrastructure/Inflow.Infrastructure.csproj index 71cc139..30129fb 100644 --- a/Inflow.Infrastructure/Inflow.Infrastructure.csproj +++ b/Inflow.Infrastructure/Inflow.Infrastructure.csproj @@ -9,6 +9,7 @@ + diff --git a/Inflow.Service/Inflow.Service.csproj b/Inflow.Service/Inflow.Service.csproj index 0e51d6a..081379c 100644 --- a/Inflow.Service/Inflow.Service.csproj +++ b/Inflow.Service/Inflow.Service.csproj @@ -9,6 +9,7 @@ +