-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResultsController.cs
More file actions
114 lines (106 loc) · 3.88 KB
/
ResultsController.cs
File metadata and controls
114 lines (106 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using F23.Kernel.AspNetCore;
using F23.Kernel.Results;
using Microsoft.AspNetCore.Mvc;
namespace F23.Kernel.Examples.AspNetCore.Infrastructure;
/// <summary>
/// MVC controller demonstrating the different types of results available in F23.Kernel.
/// </summary>
[ApiController]
[Route("mvc/results")]
[Tags("Results - MVC")]
public class ResultsController : ControllerBase
{
/// <summary>
/// Demonstrates a successful result with no value.
/// </summary>
/// <returns>204 No Content</returns>
[HttpGet("success-no-value")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public IActionResult SuccessNoValue()
{
var result = Result.Success();
return result.ToActionResult();
}
/// <summary>
/// Demonstrates a successful result with a value.
/// </summary>
/// <returns>200 OK with the value in the response body</returns>
[HttpGet("success-with-value")]
[ProducesResponseType(StatusCodes.Status200OK)]
public IActionResult SuccessWithValue()
{
var data = new { message = "Operation completed successfully", timestamp = DateTime.UtcNow };
var result = Result<object>.Success(data);
return result.ToActionResult();
}
/// <summary>
/// Demonstrates a validation failed result.
/// </summary>
/// <returns>400 Bad Request with validation errors</returns>
[HttpGet("validation-failed")]
[ProducesResponseType<ValidationProblemDetails>(StatusCodes.Status400BadRequest)]
public IActionResult ValidationFailed()
{
var errors = new[]
{
new ValidationError("email", "Email address is invalid"),
new ValidationError("password", "Password must be at least 8 characters"),
new ValidationError("username", "Username is already taken"),
};
var result = Result.ValidationFailed(errors);
return result.ToActionResult();
}
/// <summary>
/// Demonstrates an unauthorized result.
/// </summary>
/// <returns>403 Forbidden</returns>
[HttpGet("unauthorized")]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status403Forbidden)]
public IActionResult UnauthorizedDemo()
{
var result = Result.Unauthorized("User does not have permission to access this resource");
return result.ToActionResult();
}
/// <summary>
/// Demonstrates a precondition failed result (not found).
/// </summary>
/// <returns>404 Not Found</returns>
[HttpGet("not-found")]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status404NotFound)]
public IActionResult NotFoundDemo()
{
var result = Result.PreconditionFailed(
PreconditionFailedReason.NotFound,
"The requested resource was not found"
);
return result.ToActionResult();
}
/// <summary>
/// Demonstrates a precondition failed result (concurrency mismatch).
/// </summary>
/// <returns>412 Precondition Failed</returns>
[HttpGet("concurrency-mismatch")]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status412PreconditionFailed)]
public IActionResult ConcurrencyMismatch()
{
var result = Result.PreconditionFailed(
PreconditionFailedReason.ConcurrencyMismatch,
"The resource has been modified by another process. Please refresh and try again."
);
return result.ToActionResult();
}
/// <summary>
/// Demonstrates a precondition failed result (conflict).
/// </summary>
/// <returns>409 Conflict</returns>
[HttpGet("conflict")]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status409Conflict)]
public IActionResult ConflictDemo()
{
var result = Result.PreconditionFailed(
PreconditionFailedReason.Conflict,
"The operation conflicts with the current state of the resource"
);
return result.ToActionResult();
}
}