NonInventPurchasingSystem/CPRNIMS.WebApi/Common/ServiceExtensions.cs
2026-01-20 07:44:30 +08:00

172 lines
6.7 KiB
C#

using CPRNIMS.Infrastructure.Database;
using CPRNIMS.Infrastructure.Entities.Account;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using CPRNIMS.Domain.Services.Account;
using CPRNIMS.Domain.Contracts.Account;
using CPRNIMS.Domain.Services;
using CPRNIMS.Domain.Contracts.Items;
using CPRNIMS.Domain.Contracts.PR;
using CPRNIMS.Domain.Contracts.Canvass;
using CPRNIMS.Domain.Contracts.SMTP;
using CPRNIMS.Domain.Services.SMTP;
using CPRNIMS.Infrastructure.Helper;
using CPRNIMS.Domain.Contracts.PO;
using CPRNIMS.Domain.Services.PO;
using CPRNIMS.Domain.Contracts.Finance;
using CPRNIMS.Domain.Services.Finance;
using CPRNIMS.Domain.Contracts.Inventory;
using CPRNIMS.Domain.Services.Inventory;
using CPRNIMS.Domain.Contracts.Receiving;
using CPRNIMS.Domain.Services.Receiving;
namespace CPRNIMS.WebApi.Common
{
public static class ServiceExtensions
{
public static void AddApplicationServices(this IServiceCollection services, WebApplicationBuilder builder)
{
AddIdentityServices(services);
AddDbContexts(builder, services);
AddMvcServices(services);
AddAuthenticationServices(builder, services);
AddCorsServices(services);
AddOtherServices(services);
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddHttpsRedirection(options =>
{
options.HttpsPort = 2023; // The default HTTPS port
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v2", new OpenApiInfo
{
Version = "v2",
Title = "LLI API",
Description = "ASP.NET Core Web API",
//TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "Rowell Soriano",
Email = string.Empty,
Url = new Uri("http://lloydlab.com/"),
},
License = new OpenApiLicense
{
Name = "Use under LLI",
Url = new Uri("http://lloydlab.com/"),
}
});
});
}
private static void AddIdentityServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<NonInventoryDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<UserClaimsManager>();
}
private static void AddDbContexts(WebApplicationBuilder builder, IServiceCollection services)
{
var defaultConn = builder.Configuration.GetConnectionString("DefaultConnection");
var localConn = builder.Configuration.GetConnectionString("LocalPurchConn");
services.AddDbContext<NonInventoryDbContext>(options =>
options.UseSqlServer(defaultConn, sql =>
{
sql.EnableRetryOnFailure(5, TimeSpan.FromHours(2), null);
sql.CommandTimeout(2);
}));
services.AddDbContext<PurchLocalDbContext>(options =>
options.UseSqlServer(localConn, sql =>
{
sql.EnableRetryOnFailure(5, TimeSpan.FromHours(2), null);
sql.CommandTimeout(2);
}));
services.AddDbContext<AuhorizationDbContext>(options =>
options.UseSqlServer(defaultConn, sql =>
{
sql.EnableRetryOnFailure(5, TimeSpan.FromHours(2), null);
sql.CommandTimeout(2);
}));
}
private static void AddMvcServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = true;
});
}
private static void AddAuthenticationServices(WebApplicationBuilder builder, IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = builder.Configuration["JWT:ValidAudience"],
ValidIssuer = builder.Configuration["JWT:ValidIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWT:Secret"]))
};
});
}
private static void AddCorsServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
}
private static void AddOtherServices(IServiceCollection services)
{
services.AddScoped<IDepartment, Department>();
services.AddScoped<IAttachment, Domain.Services.Account.Attachment>();
services.AddScoped<IItem, Domain.Services.Items.Item>();
services.AddScoped<IPRequest, Domain.Services.PR.PRequest>();
services.AddScoped<ICanvass, Domain.Services.Canvass.Canvass>();
services.AddScoped<IRR, RR>();
services.AddScoped<IReceiving, Receiving>();
services.AddScoped <ISMTP, SMTP>();
services.AddScoped<IInventory, Inventory>();
services.AddScoped<IControllerAccess, Domain.Services.Account.ControllerAccess>();
services.AddScoped<ErrorMessageService>();
services.AddTransient<IPurchaseOrder, PurchaseOrder>();
services.AddScoped<IForgotPassword,Domain.Services.Account.ForgotPassword>();
services.AddScoped<IAccount, Account>();
services.AddScoped<SMTPHelper>();
}
}
}