Browse Source

Added logging, comment editing and comment deleting

master
Thomas Withiam 4 years ago
parent
commit
31e89c8dba
  1. 22
      src/edu/clarkson/cosi/wishlist/database/Database.java

22
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<Map<String, Object>> 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<Map<String, Object>> 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<String, Object> 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);
});
}

Loading…
Cancel
Save