Created
May 12, 2025 18:35
-
-
Save dimenus/d7c230213e90c23d13c54e686cf99329 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
using System; | |
using System.Data.Common; | |
using System.Net.Mime; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using FastEndpoints; | |
using i3Hcs.ModelApi; | |
using i3v.Esi.OData; | |
using i3v.Esi.OData.Serialization; | |
using Microsoft.OData.UriParser; | |
namespace ODataSomething; | |
internal sealed class GetRuntimeEntitySetEndpoint( | |
RuntimeModelService _modelService, | |
ODataJsonResponseWriter _jsonResponseWriter) | |
: Endpoint<GetRuntimeEntitySetRequest>, IAsyncDisposable | |
{ | |
private DbConnection? _dbConnection; | |
private DbDataReader? _dbReader; | |
public async ValueTask DisposeAsync() | |
{ | |
if (_dbReader != null) await _dbReader.DisposeAsync(); | |
if (_dbConnection != null) await _dbConnection.DisposeAsync(); | |
} | |
public override void Configure() | |
{ | |
Get("/{EntitySet}", | |
"/{EntitySet}({ObjectId})", | |
"/{EntitySet}({ObjectId})/{PropertyName}"); | |
Group<ODataGroup>(); | |
} | |
public override async Task HandleAsync(GetRuntimeEntitySetRequest req, CancellationToken ct) | |
{ | |
var request = HttpContext.Request; | |
var edm_model = _modelService.GetModel(); | |
var entity_set = edm_model.FindDeclaredEntitySet(req.EntitySet); | |
if (entity_set == null) { | |
await SendNotFoundAsync(ct); | |
return; | |
} | |
var request_path = HcsODataUtils.GetCanonicalRequestPath(request); | |
var service_path = new Uri(request_path, HcsODataUtils.GetODataRouteServicePath(request_path, entity_set)); | |
var parse_map = HcsODataUtils.GetEntitySetParseMap(edm_model, request_path, service_path, | |
new ODataUriParserSettings { | |
MaximumExpansionDepth = 1, | |
MaximumExpansionCount = 4 | |
}); | |
var odata_query = ODataEntitySetSelectQueryBuilder.CreateSelectQuery(parse_map, _modelService); | |
_dbConnection = _modelService.CreateModelDbConnection(); | |
await _dbConnection.OpenAsync(ct); | |
long? total_row_count = parse_map.IncludeRowCount | |
? ODataEntitySetSelectQueryBuilder.GetRowCount(_dbConnection, parse_map, _modelService) | |
: null; | |
var sql_cmd = odata_query.Command; | |
sql_cmd.Connection = _dbConnection; | |
_dbReader = await sql_cmd.ExecuteReaderAsync(ct); | |
if (odata_query.HasKeysetSegment && !await _dbReader.ReadAsync(ct)) { | |
await SendNotFoundAsync(ct); | |
} else { | |
await SendResultAsync(new BodyWriterResult(MediaTypeNames.Application.Json, | |
_ => _jsonResponseWriter.WriteODataJson(HttpContext.Response.BodyWriter, _dbReader, odata_query, | |
new Uri(service_path, "/EntitySet"), total_row_count))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment