Skip to content

Commit b2a8cbd

Browse files
Missing Files
1 parent 8ebe08d commit b2a8cbd

36 files changed

Lines changed: 1887 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ignoredFiles>
3+
<files>
4+
<file relativePath="../../Intent.Modules.AzureFunctions.Dispatch.Services/release-notes.md" />
5+
<file relativePath="../Intent.Modules.AzureFunctions.Dispatch.Services/release-notes.md" />
6+
<file relativePath="Intent.Modules.AzureFunctions.Dispatch.Services/release-notes.md" />
7+
</files>
8+
</ignoredFiles>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using Intent.Metadata.Models;
2+
using Intent.Modules.Common;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Intent.Modules.AzureFunctions.Api
10+
{
11+
public class HttpResponseMapper : ResponseMapperBase
12+
{
13+
private readonly Dictionary<string, EndPointResponseCodeLookup> _mapper;
14+
15+
public HttpResponseMapper()
16+
{
17+
_mapper = new Dictionary<string, EndPointResponseCodeLookup>
18+
{
19+
{ SuccessResponseCodeKey.Ok, new EndPointResponseCodeLookup("200", "OK", resultExpression => string.IsNullOrEmpty(resultExpression) ? "OkResult()" : $"OkObjectResult({resultExpression})") },
20+
{ SuccessResponseCodeKey.Created, new EndPointResponseCodeLookup("201", "Created", resultExpression => string.IsNullOrEmpty(resultExpression) ? "CreatedResult(string.Empty, null)" : $"CreatedResult(string.Empty, {resultExpression})") },
21+
{ SuccessResponseCodeKey.Accepted, new EndPointResponseCodeLookup("202", "Accepted", resultExpression => $"AcceptedResult(string.Empty,{resultExpression})") },
22+
{ SuccessResponseCodeKey.NonAuthInfo, new EndPointResponseCodeLookup("203", "NonAuthoritativeInformation", resultExpression => $"ObjectResult({resultExpression}) {{ StatusCode = 203 }}") },
23+
{ SuccessResponseCodeKey.NoContent, new EndPointResponseCodeLookup("204", "NoContent", _ => "NoContentResult()") },
24+
{ SuccessResponseCodeKey.ResetContent, new EndPointResponseCodeLookup("205", "ResetContent", resultExpression => $"ObjectResult({resultExpression}) {{ StatusCode = 205 }}") },
25+
{ SuccessResponseCodeKey.PartialContent, new EndPointResponseCodeLookup("206", "PartialContent", resultExpression => $"ObjectResult({resultExpression}) {{ StatusCode = 206 }}") },
26+
{ SuccessResponseCodeKey.MultiStatus, new EndPointResponseCodeLookup("207", "MultiStatus", resultExpression => $"ObjectResult({resultExpression}) {{ StatusCode = 207 }}") },
27+
{ SuccessResponseCodeKey.AlreadyReported, new EndPointResponseCodeLookup("208", "AlreadyReported", resultExpression => $"ObjectResult({resultExpression}) {{ StatusCode = 208 }}") },
28+
{ SuccessResponseCodeKey.ImUsed, new EndPointResponseCodeLookup("226", "IMUsed", resultExpression => $"ObjectResult({resultExpression}) {{ StatusCode = 226 }}") }
29+
};
30+
}
31+
32+
public string GetResponseStatusCodeEnum(IElement operation, string defaultEnum)
33+
{
34+
return ((EndPointResponseCodeLookup?)GetResponseCodeLookup(operation))?.StatusCodeEnum ?? defaultEnum;
35+
}
36+
37+
public string GetSuccessResponseCodeOperation(IElement operation, string defaultValue, string? resultExpression)
38+
{
39+
var lookup = (EndPointResponseCodeLookup?)GetResponseCodeLookup(operation);
40+
if (lookup == null)
41+
{
42+
return defaultValue;
43+
}
44+
return lookup.ReturnOperation(resultExpression);
45+
}
46+
47+
protected override ResponseCodeLookup? GetResponseCodeLookup(string key)
48+
{
49+
return _mapper.GetValueOrDefault(key);
50+
}
51+
}
52+
53+
internal record EndPointResponseCodeLookup(string Code, string StatusCodeEnum, Func<string?, string> ReturnOperation) : ResponseCodeLookup(Code);
54+
55+
public record ResponseCodeLookup(string Code);
56+
57+
internal static class SuccessResponseCodeKey
58+
{
59+
public const string Ok = "200 (Ok)";
60+
public const string Created = "201 (Created)";
61+
public const string Accepted = "202 (Accepted)";
62+
public const string NonAuthInfo = "203 (Non-Authoritative Information)";
63+
public const string NoContent = "204 (No Content)";
64+
public const string ResetContent = "205 (Reset Content)";
65+
public const string PartialContent = "206 (Partial Content)";
66+
public const string MultiStatus = "207 (Multi-Status)";
67+
public const string AlreadyReported = "208 (Already Reported)";
68+
public const string ImUsed = "226 (IM Used)";
69+
}
70+
71+
public abstract class ResponseMapperBase
72+
{
73+
protected ResponseMapperBase()
74+
{
75+
}
76+
77+
public string GetResponseCode(IElement operation, string defaultValue)
78+
{
79+
return GetResponseCodeLookup(operation)?.Code ?? defaultValue;
80+
}
81+
82+
protected abstract ResponseCodeLookup? GetResponseCodeLookup(string key);
83+
84+
protected ResponseCodeLookup? GetResponseCodeLookup(IElement operation)
85+
{
86+
if (!operation.HasStereotype("Http Settings"))
87+
{
88+
return null;
89+
}
90+
91+
var httpSettingsElement = operation.GetStereotype("Http Settings");
92+
93+
if (!httpSettingsElement.TryGetProperty("Success Response Code", out var property))
94+
{
95+
return null;
96+
}
97+
98+
//Not specified use the default
99+
if (string.IsNullOrEmpty(property.Value))
100+
{
101+
return null;
102+
}
103+
104+
return GetResponseCodeLookup(property.Value);
105+
}
106+
}
107+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Net;
2+
using System.Text.Json;
3+
using AzureFunctions.NET8.Application.ResponseCodes.CreateResponseCode;
4+
using AzureFunctions.NET8.Domain.Common.Exceptions;
5+
using AzureFunctions.NET8.Domain.Common.Interfaces;
6+
using FluentValidation;
7+
using Intent.RoslynWeaver.Attributes;
8+
using MediatR;
9+
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Mvc;
11+
using Microsoft.Azure.Functions.Worker;
12+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
13+
using Microsoft.OpenApi.Models;
14+
15+
[assembly: DefaultIntentManaged(Mode.Fully)]
16+
[assembly: IntentTemplate("Intent.AzureFunctions.AzureFunctionClass", Version = "2.0")]
17+
18+
namespace AzureFunctions.NET8.Api.ResponseCodes
19+
{
20+
public class CreateResponseCode
21+
{
22+
private readonly IMediator _mediator;
23+
24+
public CreateResponseCode(IMediator mediator)
25+
{
26+
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
27+
}
28+
29+
[Function("ResponseCodes_CreateResponseCode")]
30+
[OpenApiOperation("CreateResponseCodeCommand", tags: new[] { "ResponseCodes" }, Description = "Create response code command")]
31+
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(CreateResponseCodeCommand))]
32+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Accepted, contentType: "application/json", bodyType: typeof(Guid))]
33+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(object))]
34+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.NotFound, contentType: "application/json", bodyType: typeof(object))]
35+
public async Task<IActionResult> Run(
36+
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "response-codes")] HttpRequest req,
37+
CancellationToken cancellationToken)
38+
{
39+
try
40+
{
41+
var command = await AzureFunctionHelper.DeserializeJsonContentAsync<CreateResponseCodeCommand>(req.Body, cancellationToken);
42+
var result = await _mediator.Send(command, cancellationToken);
43+
return new AcceptedResult(string.Empty, result);
44+
}
45+
catch (ValidationException exception)
46+
{
47+
return new BadRequestObjectResult(exception.Errors);
48+
}
49+
catch (NotFoundException exception)
50+
{
51+
return new NotFoundObjectResult(new { exception.Message });
52+
}
53+
catch (JsonException exception)
54+
{
55+
return new BadRequestObjectResult(new { exception.Message });
56+
}
57+
catch (FormatException exception)
58+
{
59+
return new BadRequestObjectResult(new { exception.Message });
60+
}
61+
}
62+
}
63+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Net;
2+
using AzureFunctions.NET8.Application.ResponseCodes.DeleteResponseCode;
3+
using AzureFunctions.NET8.Domain.Common.Exceptions;
4+
using AzureFunctions.NET8.Domain.Common.Interfaces;
5+
using FluentValidation;
6+
using Intent.RoslynWeaver.Attributes;
7+
using MediatR;
8+
using Microsoft.AspNetCore.Http;
9+
using Microsoft.AspNetCore.Mvc;
10+
using Microsoft.Azure.Functions.Worker;
11+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
12+
using Microsoft.OpenApi.Models;
13+
14+
[assembly: DefaultIntentManaged(Mode.Fully)]
15+
[assembly: IntentTemplate("Intent.AzureFunctions.AzureFunctionClass", Version = "2.0")]
16+
17+
namespace AzureFunctions.NET8.Api.ResponseCodes
18+
{
19+
public class DeleteResponseCode
20+
{
21+
private readonly IMediator _mediator;
22+
23+
public DeleteResponseCode(IMediator mediator)
24+
{
25+
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
26+
}
27+
28+
[Function("ResponseCodes_DeleteResponseCode")]
29+
[OpenApiOperation("DeleteResponseCodeCommand", tags: new[] { "ResponseCodes" }, Description = "Delete response code command")]
30+
[OpenApiParameter(name: "id", In = ParameterLocation.Path, Required = true, Type = typeof(Guid))]
31+
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.NoContent)]
32+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(object))]
33+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.NotFound, contentType: "application/json", bodyType: typeof(object))]
34+
public async Task<IActionResult> Run(
35+
[HttpTrigger(AuthorizationLevel.Function, "delete", Route = "response-codes/{id}")] HttpRequest req,
36+
Guid id,
37+
CancellationToken cancellationToken)
38+
{
39+
try
40+
{
41+
await _mediator.Send(new DeleteResponseCodeCommand(id: id), cancellationToken);
42+
return new NoContentResult();
43+
}
44+
catch (ValidationException exception)
45+
{
46+
return new BadRequestObjectResult(exception.Errors);
47+
}
48+
catch (NotFoundException exception)
49+
{
50+
return new NotFoundObjectResult(new { exception.Message });
51+
}
52+
catch (FormatException exception)
53+
{
54+
return new BadRequestObjectResult(new { exception.Message });
55+
}
56+
}
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System.Net;
2+
using AzureFunctions.NET8.Application.ResponseCodes;
3+
using AzureFunctions.NET8.Application.ResponseCodes.GetResponseCodeById;
4+
using AzureFunctions.NET8.Domain.Common.Exceptions;
5+
using AzureFunctions.NET8.Domain.Common.Interfaces;
6+
using FluentValidation;
7+
using Intent.RoslynWeaver.Attributes;
8+
using MediatR;
9+
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Mvc;
11+
using Microsoft.Azure.Functions.Worker;
12+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
13+
using Microsoft.OpenApi.Models;
14+
15+
[assembly: DefaultIntentManaged(Mode.Fully)]
16+
[assembly: IntentTemplate("Intent.AzureFunctions.AzureFunctionClass", Version = "2.0")]
17+
18+
namespace AzureFunctions.NET8.Api.ResponseCodes
19+
{
20+
public class GetResponseCodeById
21+
{
22+
private readonly IMediator _mediator;
23+
24+
public GetResponseCodeById(IMediator mediator)
25+
{
26+
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
27+
}
28+
29+
[Function("ResponseCodes_GetResponseCodeById")]
30+
[OpenApiOperation("GetResponseCodeByIdQuery", tags: new[] { "ResponseCodes" }, Description = "Get response code by id query")]
31+
[OpenApiParameter(name: "id", In = ParameterLocation.Path, Required = true, Type = typeof(Guid))]
32+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.NonAuthoritativeInformation, contentType: "application/json", bodyType: typeof(ResponseCodeDto))]
33+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(object))]
34+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.NotFound, contentType: "application/json", bodyType: typeof(object))]
35+
public async Task<IActionResult> Run(
36+
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "response-codes/{id}")] HttpRequest req,
37+
Guid id,
38+
CancellationToken cancellationToken)
39+
{
40+
try
41+
{
42+
var result = await _mediator.Send(new GetResponseCodeByIdQuery(id: id), cancellationToken);
43+
return result != null ? new ObjectResult(result) { StatusCode = 203 } : new NotFoundResult();
44+
}
45+
catch (ValidationException exception)
46+
{
47+
return new BadRequestObjectResult(exception.Errors);
48+
}
49+
catch (NotFoundException exception)
50+
{
51+
return new NotFoundObjectResult(new { exception.Message });
52+
}
53+
catch (FormatException exception)
54+
{
55+
return new BadRequestObjectResult(new { exception.Message });
56+
}
57+
}
58+
}
59+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Net;
2+
using AzureFunctions.NET8.Application.ResponseCodes;
3+
using AzureFunctions.NET8.Application.ResponseCodes.GetResponseCodes;
4+
using AzureFunctions.NET8.Domain.Common.Exceptions;
5+
using AzureFunctions.NET8.Domain.Common.Interfaces;
6+
using FluentValidation;
7+
using Intent.RoslynWeaver.Attributes;
8+
using MediatR;
9+
using Microsoft.AspNetCore.Http;
10+
using Microsoft.AspNetCore.Mvc;
11+
using Microsoft.Azure.Functions.Worker;
12+
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
13+
using Microsoft.OpenApi.Models;
14+
15+
[assembly: DefaultIntentManaged(Mode.Fully)]
16+
[assembly: IntentTemplate("Intent.AzureFunctions.AzureFunctionClass", Version = "2.0")]
17+
18+
namespace AzureFunctions.NET8.Api.ResponseCodes
19+
{
20+
public class GetResponseCodes
21+
{
22+
private readonly IMediator _mediator;
23+
24+
public GetResponseCodes(IMediator mediator)
25+
{
26+
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
27+
}
28+
29+
[Function("ResponseCodes_GetResponseCodes")]
30+
[OpenApiOperation("GetResponseCodesQuery", tags: new[] { "ResponseCodes" }, Description = "Get response codes query")]
31+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.PartialContent, contentType: "application/json", bodyType: typeof(List<ResponseCodeDto>))]
32+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.BadRequest, contentType: "application/json", bodyType: typeof(object))]
33+
[OpenApiResponseWithBody(statusCode: HttpStatusCode.NotFound, contentType: "application/json", bodyType: typeof(object))]
34+
public async Task<IActionResult> Run(
35+
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "response-codes")] HttpRequest req,
36+
CancellationToken cancellationToken)
37+
{
38+
try
39+
{
40+
var result = await _mediator.Send(new GetResponseCodesQuery(), cancellationToken);
41+
return new ObjectResult(result) { StatusCode = 206 };
42+
}
43+
catch (ValidationException exception)
44+
{
45+
return new BadRequestObjectResult(exception.Errors);
46+
}
47+
catch (NotFoundException exception)
48+
{
49+
return new NotFoundObjectResult(new { exception.Message });
50+
}
51+
catch (FormatException exception)
52+
{
53+
return new BadRequestObjectResult(new { exception.Message });
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)