in Amazon.QLDB.DMVSample.Api/Functions/QueryVehicleRegistrationHistoryFunction.cs [58:110]
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
string vin = request.QueryStringParameters["VIN"];
IEnumerable<IIonValue> selectResult = this.qldbDriver.Execute(transactionExecutor =>
{
string vehicleRegistrationDocumentId = this.tableMetadataService.GetDocumentId(transactionExecutor, VehicleRegistrationTableName, "VIN", vin);
context.Logger.Log($"Getting history for vehicle registration where document ID is {vehicleRegistrationDocumentId}.");
IIonValue ionVehicleRegistrationDocumentId = this.valueFactory.NewString(vehicleRegistrationDocumentId);
IResult result = transactionExecutor.Execute("SELECT data, data.Owners.PrimaryOwner AS PrimaryOwner, data.Owners.SecondaryOwners AS SecondaryOwners, metadata.version "
+ $"FROM history(VehicleRegistration) "
+ "AS history WHERE history.metadata.id = ?", ionVehicleRegistrationDocumentId);
return result;
});
IEnumerable<VehicleRegistrationHistory> vehicles = selectResult.Select(x =>
{
IIonValue ionData = x.GetField("data");
IIonValue ionVersion = x.GetField("version");
Owners owners = new Owners
{
PrimaryOwner = new Owner { PersonId = x.GetField("PrimaryOwner").GetField("PersonId").StringValue }
};
var ionSecondaryOwners = x.GetField("SecondaryOwners") as IIonList;
foreach (var secondaryOwner in ionSecondaryOwners)
{
owners.SecondaryOwners.Add(new Owner { PersonId = secondaryOwner.GetField("PersonId").StringValue });
}
return new VehicleRegistrationHistory
{
Version = ionVersion.IntValue,
VehicleRegistration = new VehicleRegistration
{
Vin = ionData.GetField("VIN").StringValue,
LicensePlateNumber = ionData.GetField("LicensePlateNumber").StringValue,
State = ionData.GetField("State").StringValue,
PendingPenaltyTicketAmount = Convert.ToDouble(ionData.GetField("PendingPenaltyTicketAmount").DecimalValue),
ValidFromDate = DateTime.Parse(ionData.GetField("ValidFromDate").StringValue),
ValidToDate = DateTime.Parse(ionData.GetField("ValidToDate").StringValue),
Owners = owners
}
};
});
return new APIGatewayProxyResponse
{
StatusCode = vehicles.Any() ? (int)HttpStatusCode.OK : (int)HttpStatusCode.NotFound,
Body = JsonConvert.SerializeObject(vehicles, Formatting.Indented)
};
}