-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCommandFunction.cs
More file actions
95 lines (81 loc) · 3.91 KB
/
CommandFunction.cs
File metadata and controls
95 lines (81 loc) · 3.91 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
using System.Net;
using System.Text.Json;
using CommandQuery.SystemTextJson;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace CommandQuery.AzureFunctions
{
/// <inheritdoc />
public class CommandFunction : ICommandFunction
{
private readonly ICommandProcessor _commandProcessor;
private readonly ILogger<CommandFunction> _logger;
private readonly JsonSerializerOptions? _options;
/// <summary>
/// Initializes a new instance of the <see cref="CommandFunction"/> class.
/// </summary>
/// <param name="commandProcessor">An <see cref="ICommandProcessor"/>.</param>
/// <param name="logger">An <see cref="ILogger{T}"/>.</param>
/// <param name="options"><see cref="JsonSerializerOptions"/> to control the behavior during deserialization of <see cref="HttpRequestData.Body"/> and serialization of <see cref="HttpResponseData.Body"/>.</param>
public CommandFunction(ICommandProcessor commandProcessor, ILogger<CommandFunction> logger, JsonSerializerOptions? options = null)
{
_commandProcessor = commandProcessor;
_logger = logger;
_options = options;
}
/// <inheritdoc />
public async Task<HttpResponseData> HandleAsync(string commandName, HttpRequestData req, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(req);
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Handle {Command}", commandName);
}
try
{
var result = await _commandProcessor.ProcessAsync(commandName, await req.ReadAsStringAsync().ConfigureAwait(false), _options, cancellationToken).ConfigureAwait(false);
if (result == CommandResult.None)
{
return req.CreateResponse(HttpStatusCode.OK);
}
return await req.OkAsync(result.Value, _options).ConfigureAwait(false);
}
catch (Exception exception)
{
var payload = await req.ReadAsStringAsync().ConfigureAwait(false);
_logger.LogError(exception, "Handle command failed: {Command}, {Payload}", commandName, payload);
return exception.IsHandled()
? await req.BadRequestAsync(exception, _options).ConfigureAwait(false)
: await req.InternalServerErrorAsync(exception, _options).ConfigureAwait(false);
}
}
/// <inheritdoc />
public async Task<IActionResult> HandleAsync(string commandName, HttpRequest req, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(req);
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Handle {Command}", commandName);
}
try
{
var result = await _commandProcessor.ProcessAsync(commandName, await req.ReadAsStringAsync().ConfigureAwait(false), _options, cancellationToken).ConfigureAwait(false);
if (result == CommandResult.None)
{
return new OkResult();
}
return new OkObjectResult(result.Value);
}
catch (Exception exception)
{
var payload = await req.ReadAsStringAsync().ConfigureAwait(false);
_logger.LogError(exception, "Handle command failed: {Command}, {Payload}", commandName, payload);
return exception.IsHandled()
? new BadRequestObjectResult(exception.ToError())
: new ObjectResult(exception.ToError()) { StatusCode = StatusCodes.Status500InternalServerError };
}
}
}
}