From 31e89c8dba145334cc9f3033731b1ccbe64eddfc Mon Sep 17 00:00:00 2001 From: Thomas Withiam Date: Sun, 3 Feb 2019 20:18:11 -0500 Subject: [PATCH] Added logging, comment editing and comment deleting --- .../cosi/wishlist/database/Database.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/edu/clarkson/cosi/wishlist/database/Database.java b/src/edu/clarkson/cosi/wishlist/database/Database.java index 7cc03a4..cc02c6a 100644 --- a/src/edu/clarkson/cosi/wishlist/database/Database.java +++ b/src/edu/clarkson/cosi/wishlist/database/Database.java @@ -65,12 +65,13 @@ public class Database implements IDatabase{ if(action.getJson().isString("name"))name = action.getJson().getString("name"); if(action.getJson().isString("text"))query = action.getJson().getString("text"); JSONArray arry = new JSONArray(); - ArrayList> res = queryAll("Match (n:Comment)-[:By]->(p:User) Where toLower(n.text) CONTAINS $text and toLower(p.name) CONTAINS $name Return n.text as text, p.name as name", new Object[][] {{"name", name.toLowerCase()}, {"text", query.toLowerCase()}}, db); + ArrayList> res = queryAll("Match (n:Comment)-[:By]->(p:User) Where toLower(n.text) CONTAINS $text and toLower(p.name) CONTAINS $name Return n.text as text, p.name as name, ID(n) as id", new Object[][] {{"name", name.toLowerCase()}, {"text", query.toLowerCase()}}, db); for(Map iter : res) { Object tex = iter.get("text"); Object nm = iter.get("name"); - if(tex instanceof String && nm instanceof String) { - arry.addJSONObject(new JSONObject().setString("text", (String) tex).setString("name", (String) nm)); + Object id = iter.get("id"); + if(tex instanceof String && nm instanceof String && id instanceof Long) { + arry.addJSONObject(new JSONObject().setString("text", (String) tex).setString("name", (String) nm).setLong("id", (Long) id)); } } return new JSONObject().setBoolean("success", true).setJSONArray("comments", arry); @@ -79,7 +80,20 @@ public class Database implements IDatabase{ if(!action.getJson().isString("name") || !action.getJson().isString("text"))return new JSONObject().setString("error", "both name and text must be specified for submitting a comment").setString("errorType", "Formatting error"); String name = action.getJson().getString("name"); //name String text = action.getJson().getString("text"); //comment body - query("Merge (p:User{name: $name}) With p Create (n:Comment{text: $text})-[:By]->(p)", new Object[][] {{"name", name}, {"text", text}}, db); + query("Merge (p:User{name: $name}) With p Create (n:Comment{text: $text})-[:By]->(p) Create (l:Log:Creation{name: $name, text: $text})", new Object[][] {{"name", name}, {"text", text}}, db); + return new JSONObject().setBoolean("success", true); + }); + actionHandlers.put("editComment", (Action action, GraphDatabaseService database)->{ + if(!action.getJson().isLong("id") || !action.getJson().isString("newText"))return new JSONObject().setString("error", "both comment ID and text must be specified for editing a comment").setString("errorType", "Formatting error"); + String newText = action.getJson().getString("newText"); + Long id = action.getJson().getLong("id"); + query("Match (n:Comment)-[:By]->(p:User) Where ID(n) = $id With n, p Create (l:Log:Modification{id: $id, oldText: n.text, newText: $newText, originalPoster: p.name}) With n Set n.text = $newText", new Object[][] {{"newText", newText}, {"id", id}}, db); + return new JSONObject().setBoolean("success", true); + }); + actionHandlers.put("deleteComment", (Action action, GraphDatabaseService database)->{ + if(!action.getJson().isLong("id"))return new JSONObject().setString("error", "ID of comment not specified").setString("errorType", "Formatting error"); + Long id = action.getJson().getLong("id"); + query("Match (n:Comment)-[:By]->(p:User) Where ID(n) = $id Create (l:Log:Deletion{id: $id, commentText: n.text, originalPoster: p.name}) With n Detach Delete n", new Object[][] {{"id", id}}, db); return new JSONObject().setBoolean("success", true); }); }