#Refactor WondersController: Apply Clean Code principles (Uncle Bob style)
هذا الالتزام موجود في:
37
Program.cs
37
Program.cs
@@ -1,11 +1,13 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Serilog;
|
||||
using Serilog.Formatting.Json;
|
||||
using WondersAPI.Data;
|
||||
using WondersAPI.Models;
|
||||
using Serilog;
|
||||
using Serilog.Formatting.Json;
|
||||
using System.Reflection;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Logging setup
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddSimpleConsole(options =>
|
||||
{
|
||||
@@ -14,11 +16,11 @@ builder.Logging.AddSimpleConsole(options =>
|
||||
options.TimestampFormat = "yyyy-MM-dd HH:mm:ss ";
|
||||
});
|
||||
|
||||
|
||||
// Serilog file path
|
||||
var logFilePath = Path.Combine(Directory.GetCurrentDirectory(), "Logs", $"app-log{DateTime.Now:yyyyMMdd}.json");
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(logFilePath)!);
|
||||
|
||||
|
||||
// Serilog configuration
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
.WriteTo.File(new JsonFormatter(), logFilePath, rollingInterval: RollingInterval.Day)
|
||||
@@ -29,13 +31,20 @@ builder.Host.UseSerilog();
|
||||
builder.Services.AddDbContext<AppDbContext>(options =>
|
||||
options.UseInMemoryDatabase("WondersDb"));
|
||||
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
// Swagger + XML Comments
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
||||
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
||||
c.IncludeXmlComments(xmlPath);
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Database seeding
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
@@ -51,22 +60,24 @@ using (var scope = app.Services.CreateScope())
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(" seed-data.json not found. No data was seeded.");
|
||||
Console.WriteLine("seed-data.json not found. No data was seeded.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Logging app start
|
||||
var logger = app.Services.GetRequiredService<ILogger<Program>>();
|
||||
logger.LogInformation(" Wonders API application started successfully at {time}", DateTime.Now);
|
||||
logger.LogInformation("Wonders API application started successfully at {time}", DateTime.Now);
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
// Swagger Middleware
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(options =>
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Wonders API V1");
|
||||
options.DocumentTitle = "Wonders API Docs";
|
||||
});
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
|
المرجع في مشكلة جديدة
حظر مستخدم