Skip to content

Commit

Permalink
WIP, searching
Browse files Browse the repository at this point in the history
  • Loading branch information
haxwell committed Oct 4, 2024
1 parent 6b417a2 commit 2787d93
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/savvato/tribeapp/dto/PhraseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public class PhraseDTO {

@Schema(example = "UNICEF")
public String noun;

public boolean equals(String str) {
return adverb + " " + verb + " " + preposition + " " + noun == str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public interface ConnectService {

List<ConnectOutgoingMessageDTO> getAllConnectionsForAUser(Long userId);

List<ConnectOutgoingMessageDTO> getConnectionsWhereUserWasTheRequestingUser(Long userId);

List<ConnectOutgoingMessageDTO> getConnectionsWhereUserWasToBeConnectedWith(Long userId);

Optional<String> getQRCodeString(long userId);

Optional<String> storeQRCodeString(long userId);
Expand Down
44 changes: 30 additions & 14 deletions src/main/java/com/savvato/tribeapp/services/ConnectServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.savvato.tribeapp.services;

import com.savvato.tribeapp.constants.Constants;
import com.savvato.tribeapp.controllers.annotations.controllers.ConnectAPIController.Connect;
import com.savvato.tribeapp.controllers.dto.ConnectRequest;
import com.savvato.tribeapp.controllers.dto.ConnectionRemovalRequest;
import com.savvato.tribeapp.dto.ConnectOutgoingMessageDTO;
Expand Down Expand Up @@ -108,7 +109,28 @@ public GenericResponseDTO connect(Long requestingUserId, Long toBeConnectedWithU
}

@Override
public List<ConnectOutgoingMessageDTO> getAllConnectionsForAUser(Long userId) {
public List<ConnectOutgoingMessageDTO> getConnectionsWhereUserWasTheRequestingUser(Long userId) {
List<ConnectOutgoingMessageDTO> outgoingMessages = new ArrayList<>();

List<Connection> connectionsWhenUserIsRequestingConnection = connectionsRepository.findAllByRequestingUserId(userId);
for (Connection connection : connectionsWhenUserIsRequestingConnection) {
ConnectOutgoingMessageDTO outgoingMessageDTO = ConnectOutgoingMessageDTO.builder()
.connectionSuccess(true)
.to(UsernameConnectionStatusDTO.builder()
.userId(connection.getToBeConnectedWithUserId())
.username(userRepository.findById(connection.getToBeConnectedWithUserId()).get().getName())
.userConnectionStatus(Constants.TO_BE_CONNECTED_WITH_USER)
.build())
.message("")
.build();
outgoingMessages.add(outgoingMessageDTO);
}

return outgoingMessages;
}

@Override
public List<ConnectOutgoingMessageDTO> getConnectionsWhereUserWasToBeConnectedWith(Long userId) {
List<ConnectOutgoingMessageDTO> outgoingMessages = new ArrayList<>();

List<Connection> connectionsWhenUserIsToBeConnectedWith = connectionsRepository.findAllByToBeConnectedWithUserId(userId);
Expand All @@ -125,22 +147,16 @@ public List<ConnectOutgoingMessageDTO> getAllConnectionsForAUser(Long userId) {
outgoingMessages.add(outgoingMessage);
}

List<Connection> connectionsWhenUserIsRequestingConnection = connectionsRepository.findAllByRequestingUserId(userId);
for (Connection connection : connectionsWhenUserIsRequestingConnection) {
ConnectOutgoingMessageDTO outgoingMessageDTO = ConnectOutgoingMessageDTO.builder()
.connectionSuccess(true)
.to(UsernameConnectionStatusDTO.builder()
.userId(connection.getToBeConnectedWithUserId())
.username(userRepository.findById(connection.getToBeConnectedWithUserId()).get().getName())
.userConnectionStatus(Constants.TO_BE_CONNECTED_WITH_USER)
.build())
.message("")
.build();
outgoingMessages.add(outgoingMessageDTO);
}
return outgoingMessages;
}

@Override
public List<ConnectOutgoingMessageDTO> getAllConnectionsForAUser(Long userId) {
List<ConnectOutgoingMessageDTO> outgoingMessages = getConnectionsWhereUserWasToBeConnectedWith(userId);
outgoingMessages.addAll(getConnectionsWhereUserWasTheRequestingUser(userId));
return outgoingMessages;
}

@Override
public GenericResponseDTO removeConnection(Long requestingUserId, Long connectedWithUserId) {

Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/savvato/tribeapp/services/SearchServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,70 @@
package com.savvato.tribeapp.services;

import com.savvato.tribeapp.dto.AttributeDTO;
import com.savvato.tribeapp.dto.ConnectOutgoingMessageDTO;
import com.savvato.tribeapp.dto.SearchResponseDTO;
import org.aspectj.weaver.ast.Literal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Service
public class SearchServiceImpl implements SearchService {

@Autowired ConnectService connectService;

@Autowired AttributesService attributesService;

@Override
public Optional<SearchResponseDTO> search(Long requestingUserId, List<String> phrases) {

// get all connections which either are, or have a connection to another user which has, the given phrases
// return the connections in a SearchResponseDTO

// first get all connections as a requesting user,
List<ConnectOutgoingMessageDTO> requestingUserConnections = connectService.getConnectionsWhereUserWasTheRequestingUser(requestingUserId);

depthFirstSearch(requestingUserConnections, requestingUserId, phrases, 0);

// do a depth first search to find all connections that have the given phrases

return Optional.empty();
}

final int MAX_DEPTH = 4;
private boolean depthFirstSearch(List<ConnectOutgoingMessageDTO> connections, Long userId, List<String> phrases, int depth) {
// do a depth first search to find all connections that have the given phrases

Iterator<ConnectOutgoingMessageDTO> iterator = connections.iterator();

while (iterator.hasNext()) {
ConnectOutgoingMessageDTO connection = iterator.next();
if (depth < MAX_DEPTH) {
// get requested connections for this user
List<ConnectOutgoingMessageDTO> connectionsForUser = connectService.getConnectionsWhereUserWasTheRequestingUser(connection.to.userId);
depthFirstSearch(connectionsForUser, connection.to.userId, phrases, depth + 1);
}
}

// no more connections, so check this connection for the phrases
Optional<List<AttributeDTO>> attributesByUserId = attributesService.getAttributesByUserId(userId);
if (attributesByUserId.isPresent()) {
List<AttributeDTO> attributes = attributesByUserId.get();
for (String phrase : phrases) {
for (AttributeDTO attribute : attributes) {
if (attribute.phrase.equals(phrase)) {
// found a match
return true;

// WILO: If a match is found we should return the path to it.
}
}
}
}

return false;
}
}

0 comments on commit 2787d93

Please sign in to comment.