Adding associated records to Get, Query.

This commit is contained in:
2023-03-30 16:56:18 -05:00
parent 3df4513cd1
commit 7e368c6ff9
9 changed files with 576 additions and 23 deletions

View File

@ -70,8 +70,6 @@ public class QRecordApiAdapter
{
ApiFieldMetaData apiFieldMetaData = ApiFieldMetaData.of(field);
// todo - what about display values / possible values?
String apiFieldName = ApiFieldMetaData.getEffectiveApiFieldName(field);
if(StringUtils.hasContent(apiFieldMetaData.getReplacedByFieldName()))
{
@ -82,6 +80,23 @@ public class QRecordApiAdapter
outputRecord.put(apiFieldName, record.getValue(field.getName()));
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// todo - should probably define in meta-data if an association is included in the api or not!! //
// and what its name is too... //
//////////////////////////////////////////////////////////////////////////////////////////////////
QTableMetaData table = QContext.getQInstance().getTable(tableName);
for(Association association : CollectionUtils.nonNullList(table.getAssociations()))
{
ArrayList<Map<String, Serializable>> associationList = new ArrayList<>();
outputRecord.put(association.getName(), associationList);
for(QRecord associatedRecord : CollectionUtils.nonNullList(CollectionUtils.nonNullMap(record.getAssociatedRecords()).get(association.getName())))
{
associationList.add(qRecordToApiMap(associatedRecord, association.getAssociatedTableName(), apiVersion));
}
}
return (outputRecord);
}

View File

@ -567,6 +567,7 @@ public class QJavalinApiHandler
// and throw a 400-series error (tell the user bad-request), rather than, we're doing a 500 (server error)
getInput.setPrimaryKey(primaryKey);
getInput.setIncludeAssociations(true);
GetAction getAction = new GetAction();
GetOutput getOutput = getAction.execute(getInput);
@ -616,6 +617,7 @@ public class QJavalinApiHandler
QJavalinAccessLogger.logStart("apiQuery", logPair("table", tableName));
queryInput.setTableName(tableName);
queryInput.setIncludeAssociations(true);
PermissionsHelper.checkTablePermissionThrowing(queryInput, TablePermissionSubType.READ);
@ -795,6 +797,16 @@ public class QJavalinApiHandler
output.put("pageNo", pageNo);
output.put("pageSize", pageSize);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// map record fields for api //
// note - don't put them in the output until after the count, just because that looks a little nicer, i think //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ArrayList<Map<String, Serializable>> records = new ArrayList<>();
for(QRecord record : queryOutput.getRecords())
{
records.add(QRecordApiAdapter.qRecordToApiMap(record, tableName, version));
}
/////////////////////////////
// optionally do the count //
/////////////////////////////
@ -807,14 +819,6 @@ public class QJavalinApiHandler
output.put("count", countOutput.getCount());
}
///////////////////////////////
// map record fields for api //
///////////////////////////////
ArrayList<Map<String, Serializable>> records = new ArrayList<>();
for(QRecord record : queryOutput.getRecords())
{
records.add(QRecordApiAdapter.qRecordToApiMap(record, tableName, version));
}
output.put("records", records);
QJavalinAccessLogger.logEndSuccess(logPair("recordCount", queryOutput.getRecords().size()), QJavalinAccessLogger.logPairIfSlow("filter", filter, SLOW_LOG_THRESHOLD_MS));

View File

@ -195,6 +195,29 @@ class QJavalinApiHandlerTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testGetAssociations() throws QException
{
insert1Order3Lines4LineExtrinsicsAnd1OrderExtrinsic();
HttpResponse<String> response = Unirest.get(BASE_URL + "/api/" + VERSION + "/order/1").asString();
assertEquals(HttpStatus.OK_200, response.getStatus());
JSONObject jsonObject = new JSONObject(response.getBody());
System.out.println(jsonObject.toString(3));
JSONArray orderLines = jsonObject.getJSONArray("orderLines");
assertEquals(3, orderLines.length());
JSONObject orderLine0 = orderLines.getJSONObject(0);
JSONArray lineExtrinsics = orderLine0.getJSONArray("extrinsics");
assertEquals(3, lineExtrinsics.length());
assertEquals("Size", lineExtrinsics.getJSONObject(0).getString("key"));
assertEquals("Medium", lineExtrinsics.getJSONObject(0).getString("value"));
}
/*******************************************************************************
**
*******************************************************************************/
@ -406,6 +429,30 @@ class QJavalinApiHandlerTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
@Test
void testQueryAssociations() throws QException
{
insert1Order3Lines4LineExtrinsicsAnd1OrderExtrinsic();
HttpResponse<String> response = Unirest.get(BASE_URL + "/api/" + VERSION + "/order/query?id=1").asString();
assertEquals(HttpStatus.OK_200, response.getStatus());
JSONObject jsonObject = new JSONObject(response.getBody());
System.out.println(jsonObject.toString(3));
JSONObject order0 = jsonObject.getJSONArray("records").getJSONObject(0);
JSONArray orderLines = order0.getJSONArray("orderLines");
assertEquals(3, orderLines.length());
JSONObject orderLine0 = orderLines.getJSONObject(0);
JSONArray lineExtrinsics = orderLine0.getJSONArray("extrinsics");
assertEquals(3, lineExtrinsics.length());
assertEquals("Size", lineExtrinsics.getJSONObject(0).getString("key"));
assertEquals("Medium", lineExtrinsics.getJSONObject(0).getString("value"));
}
/*******************************************************************************
**
*******************************************************************************/
@ -958,19 +1005,7 @@ class QJavalinApiHandlerTest extends BaseTest
@Test
void testDeleteAssociations() throws QException
{
InsertInput insertInput = new InsertInput();
insertInput.setTableName(TestUtils.TABLE_NAME_ORDER);
insertInput.setRecords(List.of(new QRecord().withValue("id", 1).withValue("orderNo", "ORD123").withValue("storeId", 47)
.withAssociatedRecord("orderLines", new QRecord().withValue("lineNumber", 1).withValue("sku", "BASIC1").withValue("quantity", 42)
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Size").withValue("value", "Medium"))
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Discount").withValue("value", "3.50"))
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Color").withValue("value", "Red")))
.withAssociatedRecord("orderLines", new QRecord().withValue("lineNumber", 2).withValue("sku", "BASIC2").withValue("quantity", 42)
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Size").withValue("value", "Medium")))
.withAssociatedRecord("orderLines", new QRecord().withValue("lineNumber", 3).withValue("sku", "BASIC3").withValue("quantity", 42))
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "shopifyOrderNo").withValue("value", "#1032"))
));
new InsertAction().execute(insertInput);
insert1Order3Lines4LineExtrinsicsAnd1OrderExtrinsic();
assertEquals(1, queryTable(TestUtils.TABLE_NAME_ORDER).size());
assertEquals(4, queryTable(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC).size());
@ -989,6 +1024,28 @@ class QJavalinApiHandlerTest extends BaseTest
/*******************************************************************************
**
*******************************************************************************/
private static void insert1Order3Lines4LineExtrinsicsAnd1OrderExtrinsic() throws QException
{
InsertInput insertInput = new InsertInput();
insertInput.setTableName(TestUtils.TABLE_NAME_ORDER);
insertInput.setRecords(List.of(new QRecord().withValue("id", 1).withValue("orderNo", "ORD123").withValue("storeId", 47)
.withAssociatedRecord("orderLines", new QRecord().withValue("lineNumber", 1).withValue("sku", "BASIC1").withValue("quantity", 42)
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Size").withValue("value", "Medium"))
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Discount").withValue("value", "3.50"))
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Color").withValue("value", "Red")))
.withAssociatedRecord("orderLines", new QRecord().withValue("lineNumber", 2).withValue("sku", "BASIC2").withValue("quantity", 42)
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "Size").withValue("value", "Medium")))
.withAssociatedRecord("orderLines", new QRecord().withValue("lineNumber", 3).withValue("sku", "BASIC3").withValue("quantity", 42))
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "shopifyOrderNo").withValue("value", "#1032"))
));
new InsertAction().execute(insertInput);
}
/*******************************************************************************
**
*******************************************************************************/