Skip to content

Commit

Permalink
Extend openApi specification, do not use boolean fields for string fi…
Browse files Browse the repository at this point in the history
…elds in osm, add geojson endpoint (v2)
  • Loading branch information
elektrolytmangel committed Apr 16, 2024
1 parent 525f8f8 commit 6b7ac58
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
7 changes: 5 additions & 2 deletions backend/DefibrillatorFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using OsmSharp;
using OsmSharp.IO.API;
Expand All @@ -35,7 +37,7 @@ public DefibrillatorFunction(ServiceConfiguration config, ICacheRepository<OsmNo
}

[FunctionName("Defibrillators_GETALL")]
[OpenApiOperation(operationId: "GetDefibrillators_V1")]
[OpenApiOperation(operationId: "GetDefibrillators_V1", tags: new[] { "Defibrillator-V1" }, Summary = "Get all defibrillators from switzerland as custom json.")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(List<OsmNode>), Description = "The OK response")]
public async Task<IActionResult> GetAll(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "defibrillator")] HttpRequestMessage req,
Expand Down Expand Up @@ -71,9 +73,10 @@ public async Task<IActionResult> GetAll(


[FunctionName("Defibrillators_POST")]
[OpenApiOperation(operationId: "CreateDefibrillator_V1")]
[OpenApiOperation(operationId: "CreateDefibrillator_V1", tags: new[] { "Defibrillator-V1" }, Summary = "Create a new defibrillator. [Soon deprecated, use V2]")]
[OpenApiRequestBody("application/json", typeof(DefibrillatorRequest))]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Created, contentType: "application/json", bodyType: typeof(DefibrillatorResponse), Description = "The OK response")]
[OpenApiSecurity("Defikarte.ch API-Key", SecuritySchemeType.ApiKey, In = OpenApiSecurityLocationType.Header, Name = "x-functions-key")]
public async Task<IActionResult> Create(
[HttpTrigger(AuthorizationLevel.Function, "Post", Route = "defibrillator")] HttpRequest req,
ILogger log)
Expand Down
9 changes: 6 additions & 3 deletions backend/DefibrillatorFunctionV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using OsmSharp;
using OsmSharp.IO.API;
Expand All @@ -35,7 +37,7 @@ public DefibrillatorFunctionV2(ServiceConfiguration config, IGeoJsonCacheReposit
}

[FunctionName("Defibrillators_GETALL_V2")]
[OpenApiOperation(operationId: "GetDefibrillators_V2")]
[OpenApiOperation(operationId: "GetDefibrillators_V2", tags: new[] { "Defibrillator-V2" }, Summary = "Get all defibrillators from switzerland as geojson.")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(FeatureCollection), Description = "The OK response")]
public async Task<IActionResult> GetAll(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v2/defibrillator")] HttpRequestMessage req,
Expand Down Expand Up @@ -71,9 +73,10 @@ public async Task<IActionResult> GetAll(


[FunctionName("Defibrillators_POST_V2")]
[OpenApiOperation(operationId: "CreateDefibrillator_V2")]
[OpenApiOperation(operationId: "CreateDefibrillator_V2", tags: new[] {"Defibrillator-V2"}, Summary = "Create a new defibrillator.")]
[OpenApiRequestBody("application/json", typeof(DefibrillatorRequestV2))]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.Created, contentType: "application/json", bodyType: typeof(DefibrillatorResponse), Description = "The OK response")]
[OpenApiSecurity("Defikarte.ch API-Key", SecuritySchemeType.ApiKey, In = OpenApiSecurityLocationType.Header, Name = "x-functions-key")]
public async Task<IActionResult> Create(
[HttpTrigger(AuthorizationLevel.Function, "Post", Route = "v2/defibrillator")] HttpRequest req,
ILogger log)
Expand Down Expand Up @@ -169,7 +172,7 @@ private static Node CreateNode(DefibrillatorRequestV2 request)
"access", request.Access
},
{
"indoor", request.Indoor ? "yes" : "no"
"indoor", request.Indoor
},
{
"description", request.Description
Expand Down
2 changes: 1 addition & 1 deletion backend/Model/DefibrillatorRequestV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class DefibrillatorRequestV2

public string EmergencyPhone { get; set; }

public bool Indoor { get; set; }
public string Indoor { get; set; }

public string Source { get; set; }

Expand Down
15 changes: 14 additions & 1 deletion backend/Validation/DefibrillatorRequestValidatorV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DefibrillatorRequestValidatorV2()
RuleFor(x => x.Description).MaximumLength(200);
RuleFor(x => x.OperatorPhone).Custom((value, context) => PhoneNumberValid(value, context)).When(x => !string.IsNullOrEmpty(x.OperatorPhone));
RuleFor(x => x.Access).Custom((value, context) => AccessValid(value, context));
RuleFor(x => x.Indoor).NotNull();
RuleFor(x => x.Indoor).NotNull().NotEmpty().Custom((value, context) => IndoorValid(value, context));
RuleFor(x => x.EmergencyPhone).NotEmpty().Custom((value, context) => PhoneNumberValid(value, context));
// opening hours validation is missing
}
Expand Down Expand Up @@ -67,5 +67,18 @@ private static void AccessValid(string access, ValidationContext<DefibrillatorRe
context.AddFailure(context.PropertyName, "Access not valid");

Check warning on line 67 in backend/Validation/DefibrillatorRequestValidatorV2.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

'ValidationContext<DefibrillatorRequestV2>.PropertyName' is obsolete: 'This property has been deprecated due to its misleading name. Use the PropertyPath property instead, which returns the same value.'

Check warning on line 67 in backend/Validation/DefibrillatorRequestValidatorV2.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

'ValidationContext<DefibrillatorRequestV2>.PropertyName' is obsolete: 'This property has been deprecated due to its misleading name. Use the PropertyPath property instead, which returns the same value.'
}
}

private static void IndoorValid(string indoor, ValidationContext<DefibrillatorRequestV2> context)
{
if (string.IsNullOrEmpty(indoor))
{
return;
}

if (!(new List<string> { "yes", "no" }).Contains(indoor))
{
context.AddFailure(context.PropertyName, "Indoor not valid");

Check warning on line 80 in backend/Validation/DefibrillatorRequestValidatorV2.cs

View workflow job for this annotation

GitHub Actions / build-and-deploy

'ValidationContext<DefibrillatorRequestV2>.PropertyName' is obsolete: 'This property has been deprecated due to its misleading name. Use the PropertyPath property instead, which returns the same value.'
}
}
}
}

0 comments on commit 6b7ac58

Please sign in to comment.