From d9332cef96f2c2eed6eec246261443006527836e Mon Sep 17 00:00:00 2001 From: lumijiez <59575049+lumijiez@users.noreply.github.com> Date: Sun, 4 May 2025 23:49:31 +0300 Subject: [PATCH] Add Startup.cs file --- .../Printbase.Application.csproj | 3 +- src/Printbase.WebApi/Program.cs | 31 ++--------- src/Printbase.WebApi/Startup.cs | 53 +++++++++++++++++++ 3 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 src/Printbase.WebApi/Startup.cs diff --git a/src/Printbase.Application/Printbase.Application.csproj b/src/Printbase.Application/Printbase.Application.csproj index a3eb15f..c4abccb 100644 --- a/src/Printbase.Application/Printbase.Application.csproj +++ b/src/Printbase.Application/Printbase.Application.csproj @@ -7,7 +7,8 @@ - + + diff --git a/src/Printbase.WebApi/Program.cs b/src/Printbase.WebApi/Program.cs index 2690657..d22ea71 100644 --- a/src/Printbase.WebApi/Program.cs +++ b/src/Printbase.WebApi/Program.cs @@ -1,36 +1,11 @@ -using System.Reflection; -using MediatR; -using Microsoft.EntityFrameworkCore; -using Printbase.Domain.Repositories; -using Printbase.Infrastructure.Database; -using Printbase.Infrastructure.Repositories; +using Printbase.WebApi; var builder = WebApplication.CreateBuilder(args); -var services = builder.Services; -var configuration = builder.Configuration; -services.AddOpenApi(); -services.AddDbContext(options => - options.UseSqlServer( - configuration.GetConnectionString("DefaultConnection"), - b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName))); - -services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); - -services.AddScoped(); -services.AddScoped(); -services.AddScoped(); -services.AddScoped(); +Startup.ConfigureServices(builder); var app = builder.Build(); -if (app.Environment.IsDevelopment()) -{ - app.MapOpenApi(); -} -else -{ - app.UseHttpsRedirection(); -} +Startup.Configure(app, app.Environment); app.Run(); \ No newline at end of file diff --git a/src/Printbase.WebApi/Startup.cs b/src/Printbase.WebApi/Startup.cs new file mode 100644 index 0000000..c304c66 --- /dev/null +++ b/src/Printbase.WebApi/Startup.cs @@ -0,0 +1,53 @@ +using System.Reflection; +using Microsoft.EntityFrameworkCore; +using Printbase.Domain.Repositories; +using Printbase.Infrastructure.Database; +using Printbase.Infrastructure.Mapping; +using Printbase.Infrastructure.Repositories; + +namespace Printbase.WebApi; + +public class Startup +{ + public static void ConfigureServices(WebApplicationBuilder builder) + { + var services = builder.Services; + + services.AddDbContext(options => + options.UseSqlServer( + builder.Configuration.GetConnectionString("DefaultConnection"), + b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName))); + + services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); + + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + + services.AddAutoMapper(typeof(ProductMappingProfile).Assembly); + + services.AddOpenApi(); + } + + public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/Error"); + app.UseHsts(); + app.UseHttpsRedirection(); + } + + app.UseRouting(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); + } +} \ No newline at end of file