mirror of
https://github.com/Govor-team/Govor.git
synced 2026-09-23 10:48:58 +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:
@@ -1,9 +1,16 @@
|
||||
namespace Govor.Domain.Common;
|
||||
|
||||
|
||||
public record Error(string Code, string Message)
|
||||
public record Error(string Code, string Message, ErrorType Type, Dictionary<string, string[]>? Errors = null)
|
||||
{
|
||||
public static readonly Error None = new(string.Empty, string.Empty);
|
||||
public static readonly Error Null = new("NULL", "Value cannot be null.");
|
||||
public static Error NotFound(string code, string message) => new(code, message, ErrorType.NotFound);
|
||||
|
||||
public static Error Validation(string code, string message, Dictionary<string, string[]>? errors = null) =>
|
||||
new(code, message, ErrorType.Validation, errors);
|
||||
|
||||
public static Error Conflict(string code, string message) => new(code, message, ErrorType.Conflict);
|
||||
public static Error Unauthorized(string code, string message) => new(code, message, ErrorType.Unauthorized);
|
||||
public static Error Forbidden(string code, string message) => new(code, message, ErrorType.Forbidden);
|
||||
public static Error Failure(string code, string message) => new(code, message, ErrorType.Failure);
|
||||
|
||||
public override string ToString() => $"{Code}: {Message}";
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace Govor.Domain.Common;
|
||||
|
||||
public enum ErrorType
|
||||
{
|
||||
Failure = 0, // (400 Bad Request)
|
||||
Validation = 1, // (400 Bad Request / 422 Unprocessable)
|
||||
NotFound = 2, // (404 Not Found)
|
||||
Conflict = 3, // (409 Conflict)
|
||||
Unauthorized = 4, // (401 Unauthorized)
|
||||
Forbidden = 5 // (403 Forbidden)
|
||||
}
|
||||
@@ -1,49 +1,20 @@
|
||||
using SmartRes;
|
||||
|
||||
namespace Govor.Domain.Common;
|
||||
|
||||
public class Result
|
||||
public static class Result
|
||||
{
|
||||
protected Result(bool isSuccess, Error error)
|
||||
{
|
||||
if (isSuccess && error != Error.None || !isSuccess && error == Error.None)
|
||||
{
|
||||
throw new ArgumentException("Invalid error state", nameof(error));
|
||||
}
|
||||
// Для методов, возвращающих значение
|
||||
public static Result<T, Error> Success<T>(T value) => Result<T, Error>.Success(value);
|
||||
public static Result<T, Error> Failure<T>(Error error) => Result<T, Error>.Failure(error);
|
||||
|
||||
IsSuccess = isSuccess;
|
||||
Error = error;
|
||||
}
|
||||
|
||||
public bool IsSuccess { get; }
|
||||
public bool IsFailure => !IsSuccess;
|
||||
public Error Error { get; }
|
||||
|
||||
public static Result Success() => new(true, Error.None);
|
||||
public static Result Failure(Error error) => new(false, error);
|
||||
// Для методов, которые раньше возвращали просто Result (без T)
|
||||
public static Result<Unit, Error> Success() => Result<Unit, Error>.Success(Unit.Value);
|
||||
public static Result<Unit, Error> Failure(Error error) => Result<Unit, Error>.Failure(error);
|
||||
|
||||
public static Result Failure(Exception ex) => new(false, new Error(ex.GetType().Name, ex.Message));
|
||||
|
||||
public static implicit operator Result(Error error) => Failure(error);
|
||||
}
|
||||
|
||||
public class Result<T> : Result
|
||||
{
|
||||
private readonly T? _value;
|
||||
|
||||
private Result(T? value, bool isSuccess, Error error) : base(isSuccess, error)
|
||||
{
|
||||
_value = value;
|
||||
}
|
||||
|
||||
public T Value => IsSuccess
|
||||
? _value!
|
||||
: throw new InvalidOperationException("The value of a failure result cannot be accessed.");
|
||||
|
||||
public static Result<T> Success(T value) => new(value, true, Error.None);
|
||||
public static new Result<T> Failure(Error error) => new(default, false, error);
|
||||
public static new Result<T> Failure(Exception ex) => new(default, false, new Error(ex.GetType().Name, ex.Message));
|
||||
|
||||
public static implicit operator Result<T>(T value) => Success(value);
|
||||
|
||||
public static implicit operator Result<T>(Error error) => Failure(error);
|
||||
public static implicit operator T(Result<T> result) => result.Value;
|
||||
public static Result<Unit, Error> Failure(Exception ex) =>
|
||||
Result<Unit, Error>.Failure(new Error(ex.GetType().Name, ex.Message, ErrorType.Failure));
|
||||
|
||||
public static Result<T, Error> Failure<T>(Exception ex) =>
|
||||
Result<T, Error>.Failure(new Error(ex.GetType().Name, ex.Message, ErrorType.Failure));
|
||||
}
|
||||
@@ -1,14 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
|
||||
<PackageReference Include="Microting.EntityFrameworkCore.MySql" Version="10.0.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="SmartRes">
|
||||
<HintPath>..\libs\SmartRes.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user