Skip to content

Commit

Permalink
extracting full profiles on each request to a user
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Mar 26, 2017
1 parent 4e97fa3 commit cdc912c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
43 changes: 23 additions & 20 deletions src/main/java/com/maxdemarzi/users/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,7 @@ public Response getProfile(@PathParam("username") final String username, @Contex
Map<String, Object> results;
try (Transaction tx = db.beginTx()) {
Node user = findUser(username, db);
results = user.getAllProperties();
results.put("hash", new Md5Hash(((String)results.get(EMAIL)).toLowerCase()).toString());
results.remove(EMAIL);
results.remove(PASSWORD);
Integer following = user.getDegree(RelationshipTypes.FOLLOWS, Direction.OUTGOING);
Integer followers = user.getDegree(RelationshipTypes.FOLLOWS, Direction.INCOMING);
Integer likes = user.getDegree(RelationshipTypes.LIKES, Direction.OUTGOING);
Integer posts = user.getDegree(Direction.OUTGOING) - following - likes;
results.put("following", following);
results.put("followers", followers);
results.put("likes", likes);
results.put("posts", posts);
results = getUserAttributes(user);
tx.success();
}
return Response.ok().entity(objectMapper.writeValueAsString(results)).build();
Expand Down Expand Up @@ -103,10 +92,9 @@ public Response getFollowers(@PathParam("username") final String username, @Cont
try (Transaction tx = db.beginTx()) {
Node user = findUser(username, db);
for (Relationship r1: user.getRelationships(Direction.INCOMING, RelationshipTypes.FOLLOWS)) {
Map<String, Object> follower = r1.getStartNode().getAllProperties();
follower.remove(EMAIL);
follower.remove(PASSWORD);
results.add(follower);
Node follower = r1.getStartNode();
Map<String, Object> result = getUserAttributes(follower);
results.add(result);
}
tx.success();
}
Expand All @@ -120,10 +108,9 @@ public Response getFollowing(@PathParam("username") final String username, @Cont
try (Transaction tx = db.beginTx()) {
Node user = findUser(username, db);
for (Relationship r1: user.getRelationships(Direction.OUTGOING, RelationshipTypes.FOLLOWS)) {
Map<String, Object> following = r1.getEndNode().getAllProperties();
following.remove(EMAIL);
following.remove(PASSWORD);
results.add(following);
Node following = r1.getEndNode();
Map<String, Object> result = getUserAttributes(following);
results.add(result);
}
tx.success();
}
Expand Down Expand Up @@ -155,6 +142,22 @@ public static Node findUser(String username, @Context GraphDatabaseService db) {
return user;
}

private Map<String, Object> getUserAttributes(Node user) {
Map<String, Object> results;
results = user.getAllProperties();
results.remove(EMAIL);
results.remove(PASSWORD);
Integer following = user.getDegree(RelationshipTypes.FOLLOWS, Direction.OUTGOING);
Integer followers = user.getDegree(RelationshipTypes.FOLLOWS, Direction.INCOMING);
Integer likes = user.getDegree(RelationshipTypes.LIKES, Direction.OUTGOING);
Integer posts = user.getDegree(Direction.OUTGOING) - following - likes;
results.put("following", following);
results.put("followers", followers);
results.put("likes", likes);
results.put("posts", posts);
return results;
}

public static Node getPost(Node author, Long time) {
LocalDateTime postedDateTime = LocalDateTime.ofEpochSecond(time, 0, ZoneOffset.UTC);
RelationshipType original = RelationshipType.withName("POSTED_ON_" +
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/com/maxdemarzi/users/GetFollowersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ public void shouldGetFollowers() {

private static final ArrayList<HashMap<String, Object>> expected = new ArrayList<HashMap<String, Object>>() {{
add(new HashMap<String, Object>() {{
put("username", "jexp");
put("name", "Michael Hunger");
put("username", "jexp");
put("name", "Michael Hunger");
put("followers", 0);
put("following", 1);
put("posts", 0);
put("likes", 0);
}});
}};
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/maxdemarzi/users/GetFollowingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void shouldGetFollowing() {
add(new HashMap<String, Object>() {{
put("username", "jexp");
put("name", "Michael Hunger");
put("followers", 1);
put("following", 0);
put("posts", 0);
put("likes", 0);

}});
}};
}
3 changes: 2 additions & 1 deletion src/test/java/com/maxdemarzi/users/GetProfileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void shouldGetProfile() {
"CREATE (max:User {username:'maxdemarzi', " +
"email: 'max@neo4j.com', " +
"name: 'Max De Marzi'," +
"password: 'swordfish'})" +
"password: 'swordfish'," +
"hash: '0bd90aeb51d5982062f4f303a62df935'})" +
"CREATE (jexp:User {username:'jexp', " +
"email: 'michael@neo4j.com', " +
"name: 'Michael Hunger'," +
Expand Down

0 comments on commit cdc912c

Please sign in to comment.