Files
Govor/Govor.API/Common/Extensions/ResultExtensions.cs
Artemy 29cc51ee0c Add reactions, reliable push & session checks
Implements message reactions and reaction packs (domain models, services, admin HTTP API, media processing via ImageSharp and limits) and adds reliable push delivery (ChatPushNotification, ChatPushDispatcher, ChatPushWorker, improved ChatNotificationService). Enhances message reading (ReadChat API, unread counts, concurrency-safe receipts) and tightens session security (access-token session validation in JWT, hub filter, SessionAuthorization). Improves SignalR behavior (session groups, reaction/channel events, hub/error mappings). Adds EF migrations, many controller fixes (Forbid handling, password API), tests (Govor.Server.Tests), docs, CI and tooling updates (Dockerfile → .NET 10, GH Actions matrix for sqlite/postgres, Govor.Server.slnx, global.json), and small infra changes (AutoMapper bump, NullPushProvider, storage/token handling).
2026-10-03 17:42:24 +07:00

63 lines
2.1 KiB
C#

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,
ErrorType.ServerError => StatusCodes.Status500InternalServerError,
_ => 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",
ErrorType.ServerError => "Internal Server Error",
_ => "Bad Request"
};
}