Skip to content

Commit

Permalink
adding put commands, requiring params
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Jan 20, 2017
1 parent 1c495c7 commit 36d55ed
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 38 deletions.
33 changes: 15 additions & 18 deletions cmd/delete.java
@@ -1,9 +1,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maxdemarzi.server.Server;
import org.crsh.cli.Argument;
import org.crsh.cli.Command;
import org.crsh.cli.Named;
import org.crsh.cli.Usage;
import org.crsh.cli.*;
import org.crsh.command.BaseCommand;
import org.crsh.command.InvocationContext;
import org.jooby.Err;
Expand All @@ -22,7 +19,7 @@ public class delete extends BaseCommand {
@Named("node")
@Command
public void node(InvocationContext<ObjectName> context,
@Usage("the node id") @Argument final String id) throws Exception {
@Usage("the node id") @Required @Argument final String id) throws Exception {
Object node = Server.db.getNode(id);
if (node == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -36,9 +33,9 @@ public void node(InvocationContext<ObjectName> context,
@Named("relationship")
@Command
public void relationship(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Argument final String type,
@Usage("the starting node") @Argument final String from,
@Usage("the ending node") @Argument final String to) throws Exception {
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to) throws Exception {
Object rel = Server.db.getRelationship(type, from, to);
if (rel == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -52,8 +49,8 @@ public void relationship(InvocationContext<ObjectName> context,
@Named("node-property")
@Command
public void nodeProperty(InvocationContext<ObjectName> context,
@Usage("the node id") @Argument final String id,
@Usage("the property key") @Argument final String key) throws Exception {
@Usage("the node id") @Required @Argument final String id,
@Usage("the property key") @Required @Argument final String key) throws Exception {
HashMap<String, Object> node = Server.db.getNode(id);
if (node == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -69,10 +66,10 @@ public void nodeProperty(InvocationContext<ObjectName> context,
@Named("relationship-property")
@Command
public void relationshipProperty(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Argument final String type,
@Usage("the starting node") @Argument final String from,
@Usage("the ending node") @Argument final String to,
@Usage("the property key") @Argument final String key) throws Exception {
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to,
@Usage("the property key") @Required @Argument final String key) throws Exception {
HashMap<String, Object> rel = Server.db.getRelationship(type, from, to);
if (rel == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -88,7 +85,7 @@ public void relationshipProperty(InvocationContext<ObjectName> context,
@Named("node-properties")
@Command
public void nodeProperties(InvocationContext<ObjectName> context,
@Usage("the node id") @Argument final String id) throws Exception {
@Usage("the node id") @Required @Argument final String id) throws Exception {
Object node = Server.db.getNode(id);
if (node == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -103,9 +100,9 @@ public void nodeProperties(InvocationContext<ObjectName> context,
@Named("relationship-properties")
@Command
public void relationshipProperties(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Argument final String type,
@Usage("the starting node") @Argument final String from,
@Usage("the ending node") @Argument final String to) throws Exception {
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to) throws Exception {
Object rel = Server.db.getRelationship(type, from, to);
if (rel == null) {
throw new Err(Status.NOT_FOUND);
Expand Down
22 changes: 11 additions & 11 deletions cmd/get.java
Expand Up @@ -20,7 +20,7 @@ public class get extends BaseCommand {
@Named("node")
@Command
public void node(InvocationContext<ObjectName> context,
@Usage("the node id") @Argument final String id) throws Exception {
@Usage("the node id") @Required @Argument final String id) throws Exception {
Object node = Server.db.getNode(id);
if (node == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -33,8 +33,8 @@ public void node(InvocationContext<ObjectName> context,
@Named("node-property")
@Command
public void nodeProperty(InvocationContext<ObjectName> context,
@Usage("the node id") @Argument final String id,
@Usage("the property key") @Argument final String key) throws Exception {
@Usage("the node id") @Required @Argument final String id,
@Usage("the property key") @Required @Argument final String key) throws Exception {
HashMap<String, Object> node = Server.db.getNode(id);
if (node == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -51,7 +51,7 @@ public void nodeProperty(InvocationContext<ObjectName> context,
@Named("node-degree")
@Command
public void nodeDegree(InvocationContext<ObjectName> context,
@Usage("the node id") @Argument final String id,
@Usage("the node id") @Required @Argument final String id,
@Usage("only incoming relationships") @Option(names = {"in","incoming"}) final Boolean incoming,
@Usage("only outgoing relationships") @Option(names = {"out","outgoing"}) final Boolean outgoing,
@Usage("the relationship types") @Option(names="types") List<String> types) throws Exception {
Expand Down Expand Up @@ -85,9 +85,9 @@ public void relationshipTypes(InvocationContext<ObjectName> context) throws Exce
@Named("relationship")
@Command
public void relationship(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Argument final String type,
@Usage("the starting node") @Argument final String from,
@Usage("the ending node") @Argument final String to) throws Exception {
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to) throws Exception {
Object rel = Server.db.getRelationship(type, from, to);
if (rel == null) {
throw new Err(Status.NOT_FOUND);
Expand All @@ -100,10 +100,10 @@ public void relationship(InvocationContext<ObjectName> context,
@Named("relationship-property")
@Command
public void relationshipProperty(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Argument final String type,
@Usage("the starting node") @Argument final String from,
@Usage("the ending node") @Argument final String to,
@Usage("the property key") @Argument final String key) throws Exception {
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to,
@Usage("the property key") @Required @Argument final String key) throws Exception {
HashMap rel = Server.db.getRelationship(type, from, to);
if (rel == null) {
throw new Err(Status.NOT_FOUND);
Expand Down
13 changes: 5 additions & 8 deletions cmd/post.java
@@ -1,9 +1,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maxdemarzi.server.Server;
import org.crsh.cli.Argument;
import org.crsh.cli.Command;
import org.crsh.cli.Named;
import org.crsh.cli.Usage;
import org.crsh.cli.*;
import org.crsh.command.BaseCommand;
import org.crsh.command.InvocationContext;
import org.jooby.Err;
Expand All @@ -24,7 +21,7 @@ public class post extends BaseCommand {
@Named("node")
@Command
public void node(InvocationContext<ObjectName> context,
@Usage("the node id") @Argument final String id,
@Usage("the node id") @Required @Argument final String id,
@Usage("the properties") @Argument final String properties) throws Exception {
if (properties == null) {
Server.db.addNode(id);
Expand All @@ -43,9 +40,9 @@ public void node(InvocationContext<ObjectName> context,
@Named("relationship")
@Command
public void relationship(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Argument final String type,
@Usage("the starting node") @Argument final String from,
@Usage("the ending node") @Argument final String to,
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to,
@Usage("the properties") @Argument final String properties) throws Exception {
if (properties == null) {
Server.db.addRelationship(type, from, to);
Expand Down
101 changes: 101 additions & 0 deletions cmd/put.java
@@ -0,0 +1,101 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maxdemarzi.server.Server;
import org.crsh.cli.*;
import org.crsh.command.BaseCommand;
import org.crsh.command.InvocationContext;
import org.jooby.Err;
import org.jooby.Status;

import javax.management.ObjectName;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Usage("Put")
public class put extends BaseCommand {

private static final ObjectMapper mapper = new ObjectMapper();

@Usage("node <id> <properties> in JSON Format '{\"key\":value}'")
@Named("node")
@Command
public void node(InvocationContext<ObjectName> context,
@Usage("the node id") @Required @Argument final String id,
@Usage("the properties") @Required @Argument final String properties) throws Exception {
HashMap<String, Object> node = Server.db.getNode(id);
if (node == null) {
throw new Err(Status.NOT_FOUND);
} else {
Server.db.updateNode(id, properties);
node = Server.db.getNode(id);
context.append(mapper.writeValueAsString(node));
}
}

@Usage("relationship <type> <from> <to> <properties> in JSON Format '{\"key\":value}'")
@Named("relationship")
@Command
public void relationship(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to,
@Usage("the properties") @Argument final String properties) throws Exception {
HashMap<String, Object> rel = Server.db.getRelationship(type, from, to);
if (rel == null) {
throw new Err(Status.NOT_FOUND);
} else {
Server.db.updateRelationship(type, from, to, properties);
rel = Server.db.getRelationship(type, from, to);
context.append(mapper.writeValueAsString(rel));
}
}

@Usage("node-properties <id> <properties> in JSON Format '{\"key\":value}'")
@Named("node-properties")
@Command
public void nodeProperties(InvocationContext<ObjectName> context,
@Usage("the node id") @Required @Argument final String id,
@Usage("the properties") @Required @Argument final String properties) throws Exception {
HashMap<String, Object> node = Server.db.getNode(id);
if (node == null) {
throw new Err(Status.NOT_FOUND);
} else {
HashMap<String, Object> propertiesMap;
try {
propertiesMap = mapper.readValue(properties, HashMap.class);
} catch (IOException e) {
throw new Err(Status.BAD_REQUEST);
}
node.putAll(propertiesMap);
Server.db.updateNode(id, node);
node = Server.db.getNode(id);
context.append(mapper.writeValueAsString(node));
}
}

@Usage("relationship-properties <type> <from> <to> <properties> in JSON Format '{\"key\":value}'")
@Named("relationship-properties")
@Command
public void relationshipProperties(InvocationContext<ObjectName> context,
@Usage("the relationship type") @Required @Argument final String type,
@Usage("the starting node") @Required @Argument final String from,
@Usage("the ending node") @Required @Argument final String to,
@Usage("the properties") @Required @Argument final String properties) throws Exception {
HashMap<String, Object> rel = Server.db.getRelationship(type, from, to);
if (rel == null) {
throw new Err(Status.NOT_FOUND);
} else {
HashMap<String, Object> propertiesMap;
try {
propertiesMap = mapper.readValue(properties, HashMap.class);
} catch (IOException e) {
throw new Err(Status.BAD_REQUEST);
}
rel.putAll(propertiesMap);
Server.db.updateRelationship(type, from, to, rel);
rel = Server.db.getRelationship(type, from, to);
context.append(mapper.writeValueAsString(rel));
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/maxdemarzi/server/Node.java
Expand Up @@ -38,7 +38,7 @@ public Node() {
rsp.send(Server.db.getNode(id));
})
/*
* Create a node with Properties
* Update a node with Properties
* @param id Node ID.
* @param body Node Properties. Default is "{}".
* @return Returns <code>201</code>
Expand Down

0 comments on commit 36d55ed

Please sign in to comment.