Skip to content

Commit

Permalink
add username param to get tags
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Apr 18, 2017
1 parent d202602 commit ebb56fd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/main/java/com/maxdemarzi/tags/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.maxdemarzi.Labels;
import com.maxdemarzi.RelationshipTypes;
import com.maxdemarzi.users.Users;
import org.codehaus.jackson.map.ObjectMapper;
import org.neo4j.graphdb.*;

Expand All @@ -23,7 +24,9 @@
import static com.maxdemarzi.Properties.*;
import static com.maxdemarzi.Time.dateFormatter;
import static com.maxdemarzi.Time.utc;
import static com.maxdemarzi.likes.Likes.userLikesPost;
import static com.maxdemarzi.posts.Posts.getAuthor;
import static com.maxdemarzi.posts.Posts.userRepostedPost;
import static java.util.Collections.reverseOrder;

@Path("/tags")
Expand Down Expand Up @@ -73,6 +76,7 @@ private static List<Map<String, Object>> getTrends(String key) {
public Response getTags(@PathParam("hashtag") final String hashtag,
@QueryParam("limit") @DefaultValue("25") final Integer limit,
@QueryParam("since") final Long since,
@QueryParam("username") final String username,
@Context GraphDatabaseService db) throws IOException {
ArrayList<Map<String, Object>> results = new ArrayList<>();
LocalDateTime dateTime;
Expand All @@ -84,6 +88,11 @@ public Response getTags(@PathParam("hashtag") final String hashtag,
Long latest = dateTime.toEpochSecond(ZoneOffset.UTC);

try (Transaction tx = db.beginTx()) {
Node user = null;
if (username != null) {
user = Users.findUser(username, db);
}

Node tag = db.findNode(Labels.Tag, NAME, hashtag.toLowerCase());
if (tag != null) {
LocalDateTime earliestTag = LocalDateTime.ofEpochSecond((Long) tag.getProperty(TIME), 0, ZoneOffset.UTC);
Expand All @@ -109,7 +118,10 @@ public Response getTags(@PathParam("hashtag") final String hashtag,
- 1 // for the Posted Relationship Type
- post.getDegree(RelationshipTypes.LIKES)
- post.getDegree(RelationshipTypes.REPLIED_TO));

if (user != null) {
result.put(LIKED, userLikesPost(user, post));
result.put(REPOSTED, userRepostedPost(user, post));
}
results.add(result);
count++;
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/com/maxdemarzi/search/GetSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public void shouldGetSearchSince() throws InterruptedException {
put("likes", 0);
put("liked", false);
put("reposted", false);

}});
add(new HashMap<String, Object>() {{
put("status", "Hello World!");
Expand All @@ -122,7 +121,6 @@ public void shouldGetSearchSince() throws InterruptedException {
put("likes", 1);
put("liked", true);
put("reposted", false);

}});
}};
}
37 changes: 37 additions & 0 deletions src/test/java/com/maxdemarzi/tags/GetTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public void shouldGetTag() {
Assert.assertEquals(expected, actual);
}

@Test
public void shouldGetTagWithUser() {
HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString());

HTTP.Response response = HTTP.GET(neo4j.httpURI().resolve("/v1/tags/neo4j?username=maxdemarzi").toString());
ArrayList<HashMap> actual = response.content();
Assert.assertEquals(expected2, actual);
}

@Test
public void shouldGetTagLimited() {
HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString());
Expand Down Expand Up @@ -107,4 +116,32 @@ public void shouldNotGetTagNotFound() {
put("reposts", 1);
}});
}};

private static final ArrayList<HashMap<String, Object>> expected2 = new ArrayList<HashMap<String, Object>>() {{
add(new HashMap<String, Object>() {{
put("username", "laexample");
put("name", "Luke Gannon");
put("hash", "0bd90aeb51d5982062f4f303a62df935");
put("status", "How are you! #neo4j");
put("time", 1490208700);
put("likes", 1);
put("reposts", 0);
put("liked", true);
put("reposted", false);

}});
add(new HashMap<String, Object>() {{
put("username", "jexp");
put("name", "Michael Hunger");
put("hash", "0bd90aeb51d5982062f4f303a62df935");
put("status", "Hello World! #neo4j");
put("time", 1490140299);
put("likes", 1);
put("reposts", 1);
put("liked", true);
put("reposted", false);

}});
}};

}

0 comments on commit ebb56fd

Please sign in to comment.