mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Implemented insert; added filter to query
This commit is contained in:
@ -1,16 +1,25 @@
|
|||||||
package com.kingsrook.qqq.backend.javalin;
|
package com.kingsrook.qqq.backend.javalin;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import com.kingsrook.qqq.backend.core.actions.InsertAction;
|
||||||
import com.kingsrook.qqq.backend.core.actions.MetaDataAction;
|
import com.kingsrook.qqq.backend.core.actions.MetaDataAction;
|
||||||
import com.kingsrook.qqq.backend.core.actions.QueryAction;
|
import com.kingsrook.qqq.backend.core.actions.QueryAction;
|
||||||
import com.kingsrook.qqq.backend.core.actions.TableMetaDataAction;
|
import com.kingsrook.qqq.backend.core.actions.TableMetaDataAction;
|
||||||
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
|
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.InsertRequest;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.InsertResult;
|
||||||
import com.kingsrook.qqq.backend.core.model.actions.MetaDataRequest;
|
import com.kingsrook.qqq.backend.core.model.actions.MetaDataRequest;
|
||||||
import com.kingsrook.qqq.backend.core.model.actions.MetaDataResult;
|
import com.kingsrook.qqq.backend.core.model.actions.MetaDataResult;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.QQueryFilter;
|
||||||
import com.kingsrook.qqq.backend.core.model.actions.QueryRequest;
|
import com.kingsrook.qqq.backend.core.model.actions.QueryRequest;
|
||||||
import com.kingsrook.qqq.backend.core.model.actions.QueryResult;
|
import com.kingsrook.qqq.backend.core.model.actions.QueryResult;
|
||||||
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataRequest;
|
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataRequest;
|
||||||
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataResult;
|
import com.kingsrook.qqq.backend.core.model.actions.TableMetaDataResult;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||||
import com.kingsrook.qqq.backend.core.utils.ExceptionUtils;
|
import com.kingsrook.qqq.backend.core.utils.ExceptionUtils;
|
||||||
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
|
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
|
||||||
@ -148,7 +157,36 @@ public class QJavalinImplementation
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
private static void dataInsert(Context context)
|
private static void dataInsert(Context context)
|
||||||
{
|
{
|
||||||
context.result("{\"insertResult\":{}}");
|
try
|
||||||
|
{
|
||||||
|
String table = context.pathParam("table");
|
||||||
|
List<QRecord> recordList = new ArrayList<>();
|
||||||
|
QRecord record = new QRecord();
|
||||||
|
record.setTableName(table);
|
||||||
|
recordList.add(record);
|
||||||
|
|
||||||
|
Map<?, ?> map = context.bodyAsClass(Map.class);
|
||||||
|
for(Map.Entry<?, ?> entry : map.entrySet())
|
||||||
|
{
|
||||||
|
if(StringUtils.hasContent(String.valueOf(entry.getValue())))
|
||||||
|
{
|
||||||
|
record.setValue(String.valueOf(entry.getKey()), (Serializable) entry.getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InsertRequest insertRequest = new InsertRequest(qInstance);
|
||||||
|
insertRequest.setTableName(table);
|
||||||
|
insertRequest.setRecords(recordList);
|
||||||
|
|
||||||
|
InsertAction insertAction = new InsertAction();
|
||||||
|
InsertResult insertResult = insertAction.execute(insertRequest);
|
||||||
|
|
||||||
|
context.result(JsonUtils.toJson(insertResult));
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
handleException(context, e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -164,7 +202,19 @@ public class QJavalinImplementation
|
|||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
**
|
*
|
||||||
|
* Filter parameter is a serialized QQueryFilter object, that is to say:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* filter=
|
||||||
|
* {"criteria":[
|
||||||
|
* {"fieldName":"id","operator":"EQUALS","values":[1]},
|
||||||
|
* {"fieldName":"name","operator":"IN","values":["Darin","James"]}
|
||||||
|
* ],
|
||||||
|
* "orderBys":[
|
||||||
|
* {"fieldName":"age","isAscending":true}
|
||||||
|
* ]}
|
||||||
|
* </pre>
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
static void dataQuery(Context context)
|
static void dataQuery(Context context)
|
||||||
{
|
{
|
||||||
@ -175,6 +225,12 @@ public class QJavalinImplementation
|
|||||||
queryRequest.setSkip(integerQueryParam(context, "skip"));
|
queryRequest.setSkip(integerQueryParam(context, "skip"));
|
||||||
queryRequest.setLimit(integerQueryParam(context, "limit"));
|
queryRequest.setLimit(integerQueryParam(context, "limit"));
|
||||||
|
|
||||||
|
String filter = stringQueryParam(context, "filter");
|
||||||
|
if(filter != null)
|
||||||
|
{
|
||||||
|
queryRequest.setFilter(JsonUtils.toObject(filter, QQueryFilter.class));
|
||||||
|
}
|
||||||
|
|
||||||
QueryAction queryAction = new QueryAction();
|
QueryAction queryAction = new QueryAction();
|
||||||
QueryResult queryResult = queryAction.execute(queryRequest);
|
QueryResult queryResult = queryAction.execute(queryRequest);
|
||||||
|
|
||||||
@ -269,4 +325,21 @@ public class QJavalinImplementation
|
|||||||
|
|
||||||
return (null);
|
return (null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Returns String if context has a valid query parameter by the given name,
|
||||||
|
* Returns null if no param (or empty value).
|
||||||
|
*******************************************************************************/
|
||||||
|
private static String stringQueryParam(Context context, String name) throws NumberFormatException
|
||||||
|
{
|
||||||
|
String value = context.queryParam(name);
|
||||||
|
if(StringUtils.hasContent(value))
|
||||||
|
{
|
||||||
|
return (value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package com.kingsrook.qqq.backend.javalin;
|
package com.kingsrook.qqq.backend.javalin;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
|
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
|
||||||
import kong.unirest.HttpResponse;
|
import kong.unirest.HttpResponse;
|
||||||
import kong.unirest.Unirest;
|
import kong.unirest.Unirest;
|
||||||
@ -130,4 +135,55 @@ class QJavalinImplementationTest
|
|||||||
JSONObject values0 = record0.getJSONObject("values");
|
JSONObject values0 = record0.getJSONObject("values");
|
||||||
assertTrue(values0.has("firstName"));
|
assertTrue(values0.has("firstName"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
public void test_dataQueryWithFilter()
|
||||||
|
{
|
||||||
|
String filterJson = "{\"criteria\":[{\"fieldName\":\"firstName\",\"operator\":\"EQUALS\",\"values\":[\"Tim\"]}]}";
|
||||||
|
HttpResponse<String> response = Unirest.get(BASE_URL + "/data/person?filter=" + URLEncoder.encode(filterJson, StandardCharsets.UTF_8)).asString();
|
||||||
|
|
||||||
|
assertEquals(200, response.getStatus());
|
||||||
|
JSONObject jsonObject = JsonUtils.toJSONObject(response.getBody());
|
||||||
|
assertTrue(jsonObject.has("records"));
|
||||||
|
JSONArray records = jsonObject.getJSONArray("records");
|
||||||
|
assertEquals(1, records.length());
|
||||||
|
JSONObject record0 = records.getJSONObject(0);
|
||||||
|
JSONObject values0 = record0.getJSONObject("values");
|
||||||
|
assertEquals("Tim", values0.getString("firstName"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
public void test_dataInsert()
|
||||||
|
{
|
||||||
|
Map<String, Serializable> body = new HashMap<>();
|
||||||
|
body.put("firstName", "Bobby");
|
||||||
|
body.put("lastName", "Hull");
|
||||||
|
|
||||||
|
HttpResponse<String> response = Unirest.post(BASE_URL + "/data/person")
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(body)
|
||||||
|
.asString();
|
||||||
|
|
||||||
|
assertEquals(200, response.getStatus());
|
||||||
|
JSONObject jsonObject = JsonUtils.toJSONObject(response.getBody());
|
||||||
|
assertTrue(jsonObject.has("records"));
|
||||||
|
JSONArray records = jsonObject.getJSONArray("records");
|
||||||
|
assertEquals(1, records.length());
|
||||||
|
JSONObject record0 = records.getJSONObject(0);
|
||||||
|
assertTrue(record0.has("values"));
|
||||||
|
assertEquals("person", record0.getString("tableName"));
|
||||||
|
assertTrue(record0.has("primaryKey"));
|
||||||
|
JSONObject values0 = record0.getJSONObject("values");
|
||||||
|
assertTrue(values0.has("firstName"));
|
||||||
|
assertEquals("Bobby", values0.getString("firstName"));
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user