-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add OcpiValidator base class These validators will use a specific OCPI version to validate it's entities * Working on OcpiValidator * Forward current OcpiVersion to OcpiValidators * Working on validators for different OCPI versions * Improve sample app * Validator improvements
- Loading branch information
1 parent
6b7129c
commit 0c5fc6e
Showing
36 changed files
with
375 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
sample/OCPI.Net.Sample/Controllers/OcpiLocationsReceiverController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using OCPI.Contracts; | ||
|
||
namespace OCPI.Sample.Controllers; | ||
|
||
[OcpiEndpoint(OcpiModule.Locations, "Receiver", "2.2, 2.2.1")] | ||
[Route("2.2/locations")] | ||
[Route("2.2.1/locations")] | ||
[OcpiAuthorize] | ||
public class OcpiLocationsReceiverController : OcpiController | ||
{ | ||
[HttpGet("{countryCode}/{partyId}/{locationId}")] | ||
public IActionResult Get( | ||
[FromRoute] string countryCode, | ||
[FromRoute] string partyId, | ||
[FromRoute] string locationId) | ||
{ | ||
return OcpiOk(SampleLocation); | ||
} | ||
|
||
[HttpPut("{countryCode}/{partyId}/{locationId}")] | ||
public IActionResult Put( | ||
[FromRoute] string countryCode, | ||
[FromRoute] string partyId, | ||
[FromRoute] string locationId, | ||
[FromBody] OcpiLocation location) | ||
{ | ||
// Use a built-in OCPI validator | ||
OcpiValidate(location); | ||
|
||
// Your Location PUT logic | ||
|
||
return OcpiOk(location); | ||
} | ||
|
||
[HttpPatch("{countryCode}/{partyId}/{locationId}")] | ||
public IActionResult Patch( | ||
[FromRoute] string countryCode, | ||
[FromRoute] string partyId, | ||
[FromRoute] string locationId, | ||
[FromBody] OcpiLocation location) | ||
{ | ||
// Use a built-in OCPI validator | ||
// Validator behavior will be appropriate to PATCH | ||
// Since this is a PATCH method | ||
// (not throw errors on missing fields) | ||
OcpiValidate(location); | ||
|
||
// Your Location PATCH logic | ||
|
||
return OcpiOk(location); | ||
} | ||
|
||
private static OcpiLocation SampleLocation => new() | ||
{ | ||
CountryCode = CountryCode.Belgium, | ||
PartyId = "BEC", | ||
Id = "LOC1", | ||
Publish = true, | ||
Name = "Gent Zuid", | ||
Address = "F.Rooseveltlaan 3A", | ||
City = "Gent", | ||
PostalCode = "9000", | ||
Country = "BEL", | ||
Coordinates = new OcpiGeolocation | ||
{ | ||
Latitude = "51.047599", | ||
Longitude = "3.729944" | ||
}, | ||
ParkingType = ParkingType.OnStreet, | ||
TimeZone = "Europe/Brussels", | ||
LastUpdated = DateTime.UtcNow | ||
}; | ||
} |
62 changes: 62 additions & 0 deletions
62
sample/OCPI.Net.Sample/Controllers/OcpiLocationsSenderController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using OCPI.Contracts; | ||
|
||
namespace OCPI.Sample.Controllers; | ||
|
||
[OcpiEndpoint(OcpiModule.Locations, "Sender", "2.2, 2.2.1")] | ||
[Route("2.2/locations")] | ||
[Route("2.2.1/locations")] | ||
[OcpiAuthorize] | ||
public class OcpiLocationsSenderController : OcpiController | ||
{ | ||
[HttpGet] | ||
public IActionResult GetPage([FromQuery] OcpiPageRequest pageRequest) | ||
{ | ||
// Set maximum Limit value | ||
// Required for OCPI.Net PageResult handling | ||
SetMaxLimit(pageRequest, 100); | ||
|
||
// Process GetPage using Offset, Limit, DateFrom, DateTo from pageRequest | ||
|
||
// You can use BitzArt.Pagination nuget package | ||
// https://github.com/BitzArt/Pagination | ||
// to handle Offset and Limit (does not handle DateFrom/DateTo), | ||
// or implement your own OcpiPageRequest handling logic. | ||
|
||
// Example: | ||
var databaseLocations = Enumerable.Range(1, 100).Select(x => | ||
{ | ||
var location = SampleLocation; | ||
location.Id = $"SampleLocation-{x}"; | ||
return location; | ||
}); | ||
|
||
// This will handle Offset and Limit but not DateFrom or DateTo: | ||
var result = databaseLocations.ToPage(pageRequest); | ||
|
||
// Returning a PageResult in OcpiOk will process the paginated response | ||
// and add appropriate page response headers. | ||
return OcpiOk(result); | ||
} | ||
|
||
private static OcpiLocation SampleLocation => new() | ||
{ | ||
CountryCode = CountryCode.Belgium, | ||
PartyId = "BEC", | ||
Id = "LOC1", | ||
Publish = true, | ||
Name = "Gent Zuid", | ||
Address = "F.Rooseveltlaan 3A", | ||
City = "Gent", | ||
PostalCode = "9000", | ||
Country = "BEL", | ||
Coordinates = new OcpiGeolocation | ||
{ | ||
Latitude = "51.047599", | ||
Longitude = "3.729944" | ||
}, | ||
ParkingType = ParkingType.OnStreet, | ||
TimeZone = "Europe/Brussels", | ||
LastUpdated = DateTime.UtcNow | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/OCPI.Net.Validation/Utility/GetCurrentOcpiVersionExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using BitzArt.EnumToMemberValue; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace OCPI; | ||
|
||
internal static class GetCurrentOcpiVersionExtension | ||
{ | ||
public static OcpiVersion GetCurrentOcpiVersion(this HttpRequest request) | ||
{ | ||
var path = request.Path.Value!.TrimStart('/'); | ||
var versionLength = path.IndexOf('/'); | ||
if (versionLength == -1) versionLength = path.Length; | ||
|
||
var versionString = path[..versionLength]; | ||
|
||
var result = versionString.ToEnum<OcpiVersion>(); | ||
return result; | ||
} | ||
} |
18 changes: 0 additions & 18 deletions
18
src/OCPI.Net.Validation/ValidationExtensions/OcpiEnumValidationExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.