using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Prefab.Endpoints;
///
/// Shared helpers for building consistent HTTP problem responses across modules.
///
public static class ApiProblemDetails
{
///
/// Creates a 404 payload with standard metadata and resource identifiers.
///
/// Logical name of the missing resource (for example, "Product").
/// Identifier value supplied by the caller.
/// Human-readable explanation for logging or the client.
public static ProblemDetails NotFound(string resource, string identifier, string detail) =>
new()
{
Title = "Resource not found.",
Detail = detail,
Type = "https://prefab.dev/problems/not-found",
Status = StatusCodes.Status404NotFound,
Extensions =
{
["resource"] = resource,
["identifier"] = identifier
}
};
///
/// Creates a 409 payload used when a request cannot be completed due to a resource conflict.
///
/// Human-readable explanation of the conflict.
/// Logical name of the resource involved, when known.
/// Identifier value supplied by the caller, when known.
public static ProblemDetails Conflict(string detail, string? resource = null, string? identifier = null)
{
var problem = new ProblemDetails
{
Title = "Request conflict.",
Detail = detail,
Type = "https://prefab.dev/problems/conflict",
Status = StatusCodes.Status409Conflict
};
if (!string.IsNullOrWhiteSpace(resource))
{
problem.Extensions["resource"] = resource;
}
if (!string.IsNullOrWhiteSpace(identifier))
{
problem.Extensions["identifier"] = identifier;
}
return problem;
}
}