mirror of
https://github.com/Govor-team/Govor.git
synced 2026-09-21 02:10:56 +00:00
Switch to SmartRes Result/Error, upgrade to .NET 10
Migrate codebase to use SmartRes Result<T, Error> (and Unit) across services and interfaces, replacing previous Result usage and updating many method signatures and implementations. Add ResultExtensions to convert SmartRes results to ASP.NET ActionResult and refactor AuthController to use functional Bind/Tap chains for registration/login flows. Upgrade projects to .NET 10 and bump related NuGet packages; add libs/SmartRes.dll and project references. Simplify DbContext registration to use Npgsql only with retry policies and enable detailed logging. Update Swagger/launch settings and other minor fixes (AutoMapper registration change, whitespace/exception handling, and removal of the Govor.ConsoleClient files).
This commit is contained in:
@@ -98,7 +98,7 @@ public static class ConfigurationProgramExtensions
|
||||
|
||||
|
||||
// Auto Mapper
|
||||
services.AddAutoMapper(typeof(MappingProfile));
|
||||
services.AddAutoMapper(op => { }, typeof(MappingProfile));
|
||||
|
||||
services.AddScoped<IHubUserAccessor, HubUserAccessor>();
|
||||
|
||||
@@ -111,52 +111,26 @@ public static class ConfigurationProgramExtensions
|
||||
|
||||
services.AddScoped<IProfileService, ProfileService>();
|
||||
}
|
||||
|
||||
|
||||
public static void AddGovorDbContext(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var useMySql = configuration.GetValue<bool>("UseMySql");
|
||||
|
||||
if (useMySql)
|
||||
services.AddDbContext<GovorDbContext>(options =>
|
||||
{
|
||||
services.AddDbContext<GovorDbContext>(options =>
|
||||
{
|
||||
var connectionString = configuration.GetConnectionString(nameof(GovorDbContext));
|
||||
options.UseNpgsql(
|
||||
configuration.GetConnectionString(nameof(GovorDbContext)),
|
||||
npgsqlOptions =>
|
||||
{
|
||||
// retry for transient failures
|
||||
npgsqlOptions.EnableRetryOnFailure(
|
||||
5,
|
||||
TimeSpan.FromSeconds(5),
|
||||
null);
|
||||
});
|
||||
|
||||
options.UseMySql(
|
||||
connectionString,
|
||||
new MySqlServerVersion(new Version(8, 0, 21)),
|
||||
mySqlOptions =>
|
||||
{
|
||||
mySqlOptions.EnableRetryOnFailure(
|
||||
maxRetryCount: 5,
|
||||
maxRetryDelay: TimeSpan.FromSeconds(5),
|
||||
errorNumbersToAdd: null);
|
||||
});
|
||||
|
||||
options.EnableSensitiveDataLogging();
|
||||
options.EnableDetailedErrors();
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddDbContext<GovorDbContext>(options =>
|
||||
{
|
||||
options.UseNpgsql(
|
||||
configuration.GetConnectionString(nameof(GovorDbContext)),
|
||||
npgsqlOptions =>
|
||||
{
|
||||
// retry for transient failures
|
||||
npgsqlOptions.EnableRetryOnFailure(
|
||||
maxRetryCount: 5,
|
||||
maxRetryDelay: TimeSpan.FromSeconds(5),
|
||||
errorCodesToAdd: null);
|
||||
});
|
||||
|
||||
//options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
|
||||
options.EnableSensitiveDataLogging();
|
||||
options.EnableDetailedErrors();
|
||||
});
|
||||
}
|
||||
//options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
|
||||
|
||||
options.EnableSensitiveDataLogging();
|
||||
options.EnableDetailedErrors();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Govor.Domain.Common;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SmartRes;
|
||||
|
||||
namespace Govor.API.Common.Extensions;
|
||||
|
||||
public static class ResultExtensions
|
||||
{
|
||||
public static ActionResult ToActionResult<T>(this Result<T, Error> result)
|
||||
{
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
// Если тип Unit, возвращаем 204 No Content, иначе 200 OK со значением
|
||||
return typeof(T) == typeof(Unit)
|
||||
? new StatusCodeResult(StatusCodes.Status204NoContent)
|
||||
: new OkObjectResult(result.Value);
|
||||
}
|
||||
|
||||
return GenerateProblemDetails(result.Error);
|
||||
}
|
||||
|
||||
private static ActionResult GenerateProblemDetails(Error error)
|
||||
{
|
||||
var statusCode = error.Type switch
|
||||
{
|
||||
ErrorType.NotFound => StatusCodes.Status404NotFound,
|
||||
ErrorType.Validation => StatusCodes.Status400BadRequest,
|
||||
ErrorType.Conflict => StatusCodes.Status409Conflict,
|
||||
ErrorType.Unauthorized => StatusCodes.Status401Unauthorized,
|
||||
ErrorType.Forbidden => StatusCodes.Status403Forbidden,
|
||||
_ => StatusCodes.Status400BadRequest
|
||||
};
|
||||
|
||||
var problemDetails = new ProblemDetails
|
||||
{
|
||||
Status = statusCode,
|
||||
Title = GetTitleForErrorType(error.Type),
|
||||
Detail = error.Message,
|
||||
};
|
||||
|
||||
problemDetails.Extensions.Add("errorCode", error.Code);
|
||||
|
||||
if (error.Errors is not null)
|
||||
{
|
||||
problemDetails.Extensions.Add("errors", error.Errors);
|
||||
}
|
||||
|
||||
return new ObjectResult(problemDetails) { StatusCode = statusCode };
|
||||
}
|
||||
|
||||
private static string GetTitleForErrorType(ErrorType type) => type switch
|
||||
{
|
||||
ErrorType.NotFound => "Not Found",
|
||||
ErrorType.Validation => "Validation Error",
|
||||
ErrorType.Conflict => "Conflict",
|
||||
ErrorType.Unauthorized => "Unauthorized",
|
||||
ErrorType.Forbidden => "Forbidden",
|
||||
_ => "Bad Request"
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user