Skip to content

Commit

Permalink
adding delete follows
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Apr 3, 2017
1 parent e45ee93 commit 3bd4121
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A Neo4j Based Twitter Clone Backend
:GET /v1/users/{username}/followers
:GET /v1/users/{username}/following
:POST /v1/users/{username}/follows/{username2}
:DELETE /v1/users/{username}/follows/{username2}
:GET /v1/users/{username}/posts
:POST /v1/users/{username}/posts {status:''}
:POST /v1/users/{username}/posts/{username2}/{time}
Expand Down
34 changes: 31 additions & 3 deletions src/main/java/com/maxdemarzi/users/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.neo4j.graphdb.*;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.*;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.io.IOException;
Expand Down Expand Up @@ -143,6 +141,36 @@ public Response createFollows(@PathParam("username") final String username,
return Response.ok().entity(objectMapper.writeValueAsString(results)).build();
}

@DELETE
@Path("/{username}/follows/{username2}")
public Response removeFollows(@PathParam("username") final String username,
@PathParam("username2") final String username2,
@Context GraphDatabaseService db) throws IOException {
Map<String, Object> results;
try (Transaction tx = db.beginTx()) {
Node user = findUser(username, db);
Node user2 = findUser(username2, db);

if (user.getDegree(RelationshipTypes.FOLLOWS, Direction.OUTGOING)
< user2.getDegree(RelationshipTypes.FOLLOWS, Direction.INCOMING)) {
for (Relationship r1: user.getRelationships(Direction.OUTGOING, RelationshipTypes.FOLLOWS) ) {
if (r1.getEndNode().equals(user2)) {
r1.delete();
}
}
} else {
for (Relationship r1 : user2.getRelationships(Direction.INCOMING, RelationshipTypes.FOLLOWS)) {
if (r1.getStartNode().equals(user)) {
r1.delete();
}
}
}

tx.success();
}
return Response.noContent().build();
}

public static Node findUser(String username, @Context GraphDatabaseService db) {
Node user = db.findNode(Labels.User, USERNAME, username);
if (user == null) { throw UserExceptions.userNotFound;}
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/maxdemarzi/users/RemoveFollowsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.maxdemarzi.users;

import com.sun.jersey.api.client.UniformInterfaceException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.neo4j.harness.junit.Neo4jRule;
import org.neo4j.test.server.HTTP;

public class RemoveFollowsTest {
@Rule
public Neo4jRule neo4j = new Neo4jRule()
.withFixture(FIXTURE)
.withExtension("/v1", Users.class);

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void shouldRemoveFollows() {
HTTP.POST(neo4j.httpURI().resolve("/v1/schema/create").toString());
thrown.expect(UniformInterfaceException.class);
HTTP.request("DELETE", neo4j.httpURI().resolve("/v1/users/maxdemarzi/follows/jexp").toString(), null);
}
private static final String FIXTURE =
"CREATE (max:User {username:'maxdemarzi', " +
"email: 'max@neo4j.com', " +
"name: 'Max De Marzi'," +
"password: 'swordfish'})" +
"CREATE (jexp:User {username:'jexp', " +
"email: 'michael@neo4j.com', " +
"name: 'Michael Hunger'," +
"password: 'tunafish'})" +
"CREATE (max)-[:FOLLOWS {time:1490140299}]->(jexp)";
}

0 comments on commit 3bd4121

Please sign in to comment.