Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Createconfigureservicesextensions #61

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions Inflow.Api/Extensions/ConfigureServicesExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<ICategoryRepository, CategoryRepository>();
services.AddScoped<IProductRepository, ProductRepository>();
services.AddScoped<ICommonRepository, CommonRepository>();
services.AddScoped<ICustomerRepository, CustomerRepository>();
services.AddScoped<ISaleRepository, SaleRepository>();
services.AddScoped<ISaleItemRepository, SaleItemRepository>();
services.AddScoped<ISupplierRepository, SupplierRepository>();
services.AddScoped<ISupplyRepository, SupplyRepository>();
services.AddScoped<ISupplyItemRepository, SupplyItemRepository>();

services.AddScoped<ICategoryService, CategoryService>();
services.AddScoped<IProductService, ProductService>();
services.AddScoped<ICustomerService, CustomerService>();
services.AddScoped<ISaleService, SaleService>();
services.AddScoped<ISaleItemService, SaleItemService>();
services.AddScoped<ISupplierService, SupplierService>();
services.AddScoped<ISupplyService, SupplyService>();
services.AddScoped<ISupplyItemService, SupplyItemService>();
services.AddScoped<IDashboardService, DashboardService>();
services.AddSingleton<IEmailSender, EmailSender>();

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<InflowDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DiyorMarketConection")));

return services;
}
}
}

6 changes: 6 additions & 0 deletions Inflow.Api/Inflow.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Bogus" Version="35.5.0" />
<PackageReference Include="ClosedXML" Version="0.102.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
Expand All @@ -20,13 +21,18 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PdfSharpCore" Version="1.3.63" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="25.1.38" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Inflow.Domain\Inflow.Domain.csproj" />
<ProjectReference Include="..\Inflow.Infrastructure\Inflow.Infrastructure.csproj" />
<ProjectReference Include="..\Inflow.Service\Inflow.Service.csproj" />
</ItemGroup>

</Project>
49 changes: 46 additions & 3 deletions Inflow.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -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<FileExtensionContentTypeProvider>();
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<ErrorHandlerMiddleware>();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
Expand All @@ -18,6 +60,7 @@

app.UseHttpsRedirection();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
Expand Down
1 change: 1 addition & 0 deletions Inflow.Domain/Inflow.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Inflow.Infrastructure/Inflow.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Inflow.Service/Inflow.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading