in Amazon.QLDB.DMVSample.Api/Functions/AddSecondaryOwnerFunction.cs [58:114]
public APIGatewayProxyResponse FunctionHandler(APIGatewayProxyRequest request, ILambdaContext context)
{
VehicleRegistration vehicleRegistration = JsonConvert.DeserializeObject<VehicleRegistration>(request.Body);
return this.qldbDriver.Execute(transactionExecutor =>
{
context.Logger.Log($"Checking vehicle registration already exists for VIN {vehicleRegistration.Vin}.");
if (!CheckIfVinAlreadyExists(transactionExecutor, vehicleRegistration.Vin))
{
context.Logger.Log($"Vehicle registration does not exist for VIN {vehicleRegistration.Vin}, returning not found.");
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.NotFound
};
}
context.Logger.Log($"Vehicle registration already exists, checking what's changed between current value and request.");
IEnumerable<string> existingSecondaryOwners = GetSecondaryOwners(transactionExecutor, vehicleRegistration.Vin);
IEnumerable<string> newSecondaryOwnersGovIds = vehicleRegistration.Owners.SecondaryOwners
.Select(x => x.PersonId)
.Except(existingSecondaryOwners);
if (!newSecondaryOwnersGovIds.Any())
{
context.Logger.Log($"Nothing changed, returning not modified.");
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.NotModified
};
}
string secondaryOwnerPersonDocumentId = this.tableMetadataService.GetDocumentId(transactionExecutor, PersonTableName, "GovId", newSecondaryOwnersGovIds.First());
if (!CheckIfSecondaryOwnerExists(existingSecondaryOwners, secondaryOwnerPersonDocumentId))
{
context.Logger.Log($"Secondary owner does not exists with document ID {secondaryOwnerPersonDocumentId}, adding secondary owner.");
IIonValue ionVin = this.valueFactory.NewString(vehicleRegistration.Vin);
IIonValue ionSecondaryOwner = ConvertOwnerToIonValue(new Owner { PersonId = secondaryOwnerPersonDocumentId });
IResult result = transactionExecutor.Execute($"FROM VehicleRegistration AS v WHERE v.VIN = ? " +
"INSERT INTO v.Owners.SecondaryOwners VALUE ?", ionVin, ionSecondaryOwner);
context.Logger.Log($"Secondary owner added with document ID {secondaryOwnerPersonDocumentId}, returning OK.");
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.OK
};
}
else
{
context.Logger.Log($"Secondary owner already exists, returning not modified.");
return new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.NotModified
};
}
});
}