Skip to content

Commit 577ff28

Browse files
committed
Atualização de versão e melhorias na classe Problem
* Atualização de versão - Alterado `SPPreview` de `-preview-4.5` para `-preview-4.6`. * Melhoria na classe Problem - Adicionada a função `ReplaceProperty` para substituir propriedades. * Novas extensões para HttpContext - Criada a classe `HttpContextExtensions` com método `ToProblemDetails` para conversão de problemas.
1 parent 379450f commit 577ff28

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111
<PropertyGroup>
1212
<SPVer>1.0.0</SPVer>
13-
<SPPreview>-preview-4.5</SPPreview>
13+
<SPPreview>-preview-4.6</SPPreview>
1414
</PropertyGroup>
1515
<PropertyGroup>
1616
<FluentValidationVer>12.0.0</FluentValidationVer>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Options;
5+
using RoyalCode.SmartProblems.Conversions;
6+
using ProblemDetailsOptions = RoyalCode.SmartProblems.Descriptions.ProblemDetailsOptions;
7+
8+
namespace RoyalCode.SmartProblems;
9+
10+
/// <summary>
11+
/// Extensions for <see cref="HttpContext"/> to work with <see cref="Problem"/> and <see cref="Problems"/>.
12+
/// </summary>
13+
public static class HttpContextExtensions
14+
{
15+
/// <summary>
16+
/// Convert the <paramref name="problems"/> to <see cref="ProblemDetails"/> using the
17+
/// <see cref="ProblemDetailsOptions"/> registered in the <see cref="IServiceCollection"/>
18+
/// getting it from the <see cref="HttpContext.RequestServices"/>.
19+
/// </summary>
20+
/// <param name="context">The current <see cref="HttpContext"/>.</param>
21+
/// <param name="problems">The result to be converted.</param>
22+
/// <returns>A new instance of <see cref="ProblemDetails"/>.</returns>
23+
public static ProblemDetails ToProblemDetails(this HttpContext context, Problems problems)
24+
{
25+
var options = context.RequestServices.GetRequiredService<IOptions<ProblemDetailsOptions>>().Value;
26+
return ProblemDetailsConverter.ToProblemDetails(problems, options);
27+
}
28+
}

src/RoyalCode.SmartProblems/Problem.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ public Problem ChainProperty(string? parentProperty, int index)
140140
return this;
141141
}
142142

143+
/// <summary>
144+
/// Replaces the current property with a new one.
145+
/// </summary>
146+
/// <param name="newProperty">The new property name.</param>
147+
/// <returns>The same instance of the problem.</returns>
148+
public Problem ReplaceProperty(string? newProperty)
149+
{
150+
_property = newProperty;
151+
return this;
152+
}
153+
143154
/// <inheritdoc />
144155
public override string ToString() => ToStringFactory(this);
145156

0 commit comments

Comments
 (0)