Skip to content

Commit

Permalink
User can remove user frmo connection
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ikpefua committed May 28, 2021
1 parent 9ea5f7e commit 8e38973
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/michael/controller/ConnectionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,24 @@ public ResponseEntity<?> store(@PathVariable Long connectionId, HttpSession sess
return new ResponseEntity<>(messageResponse, new HttpHeaders(), HttpStatus.CREATED);

}

@DeleteMapping(path = "{connectionId}")
public ResponseEntity<?> destroy(@PathVariable Long connectionId, HttpSession session) {
User owner = authUser(session);
if (owner == null) throw new UserException("User Not Found. Please Login");
User connection = userService.getUserById(connectionId);
if (connection == null) throw new UserException("User Not Found. Check User Id");

boolean isConnected = connectionService.checkIfUserAlreadyExistInConnection(owner, connection);
if (!isConnected) {
throw new ConnectionException("Connection Not In List");
}

connectionService.removeConnection(owner, connection);

MessageResponse messageResponse = new MessageResponse("Connection Removed!!!");

return new ResponseEntity<>(messageResponse, new HttpHeaders(), HttpStatus.OK);
}

}
9 changes: 9 additions & 0 deletions src/main/java/com/michael/service/ConnectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ public List<User> getConnections(User owner) {

return null;
}

@Override
public void removeConnection(User owner, User connection) {
Optional<Connection> connectionOptional = connectionRepository.findConnectionByOwnerAndConnecton(owner, connection);
if (connectionOptional.isPresent()) {
Connection connection1 = connectionOptional.get();
connectionRepository.delete(connection1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface IConnectionService {
boolean checkIfUserAlreadyExistInConnection(User owner, User connection);

List<User> getConnections(User owner);

void removeConnection(User owner, User connection);
}
Binary file modified target/classes/com/michael/controller/ConnectionController.class
Binary file not shown.
Binary file modified target/classes/com/michael/service/ConnectionService.class
Binary file not shown.
Binary file not shown.

0 comments on commit 8e38973

Please sign in to comment.