-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from dancier/search-implementation
Search implementation
- Loading branch information
Showing
14 changed files
with
299 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 0 additions & 15 deletions
15
src/main/java/net/dancier/dancer/core/DancerRepository.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/net/dancier/dancer/dancers/DancerRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package net.dancier.dancer.dancers; | ||
|
||
import net.dancier.dancer.core.model.Dancer; | ||
import net.dancier.dancer.core.model.Gender; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface DancerRepository extends JpaRepository<Dancer, UUID> { | ||
|
||
Optional<Dancer> findByUserId(UUID userid); | ||
|
||
Boolean existsByDancerName(String dancerName); | ||
|
||
List<Dancer> findFirst500ByGenderAndLongitudeBetweenAndLatitudeBetween( | ||
Gender gender, | ||
double lowerLongitude, | ||
double upperLongitude, | ||
double lowerLatitude, | ||
double upperLatitude | ||
); | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/net/dancier/dancer/dancers/DancerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package net.dancier.dancer.dancers; | ||
|
||
import net.dancier.dancer.chat.dto.DancerDto; | ||
import net.dancier.dancer.chat.dto.DancerIdsDto; | ||
import net.dancier.dancer.core.dto.PublicProfileDto; | ||
import net.dancier.dancer.core.exception.NotFoundException; | ||
import net.dancier.dancer.core.model.Dancer; | ||
import net.dancier.dancer.core.model.Gender; | ||
import net.dancier.dancer.security.AuthenticatedUser; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
public class DancerService { | ||
|
||
@Autowired | ||
DancerRepository dancerRepository; | ||
|
||
public Dancer loadByUserId(UUID userId) { | ||
return dancerRepository.findByUserId(userId).orElseThrow(() -> new NotFoundException("No Profile found for given user.")); | ||
} | ||
|
||
public List<Dancer> getAllDancer() { | ||
return dancerRepository.findAll(); | ||
} | ||
|
||
public HashMap<UUID, DancerDto> getDancerMap(DancerIdsDto dancerIdsDto) { | ||
|
||
HashMap<UUID, DancerDto> dancers = new HashMap<>(); | ||
|
||
dancerRepository.findAllById(dancerIdsDto.getDancerIds()) | ||
.stream() | ||
.map(DancerDto::fromDancer) | ||
.forEach(dancerDto -> dancers.put(dancerDto.getId(), dancerDto)); | ||
|
||
return dancers; | ||
} | ||
|
||
public List<PublicProfileDto> getDancerList(AuthenticatedUser authenticatedUser, Gender gender, int range) { | ||
|
||
Dancer dancer = loadByUserId(authenticatedUser.getUserId()); | ||
|
||
// 1° in longitude in Germany (latitude 47) are 75,78 km | ||
double longitudeRange = range/75.78; | ||
// 1° in longitude are 112,12 km | ||
double latitudeRange = range/112.12; | ||
|
||
double upperLatitude = dancer.getLatitude() + latitudeRange; | ||
double lowerLatitude = dancer.getLatitude() - latitudeRange; | ||
double upperLongitude = dancer.getLongitude() + longitudeRange; | ||
double lowerLongitude = dancer.getLongitude() - longitudeRange; | ||
|
||
List<Dancer> resultList = dancerRepository.findFirst500ByGenderAndLongitudeBetweenAndLatitudeBetween( | ||
gender, lowerLongitude, upperLongitude, lowerLatitude, upperLatitude); | ||
|
||
return resultList.stream() | ||
.map(PublicProfileDto::of) | ||
.filter(d -> d.getId() != dancer.getId()) | ||
.toList(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/java/net/dancier/dancer/recommendation/RecommendationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package net.dancier.dancer.dancers; | ||
|
||
import net.dancier.dancer.AbstractPostgreSQLEnabledTest; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.security.test.context.support.WithUserDetails; | ||
import org.springframework.test.context.jdbc.Sql; | ||
import java.util.List; | ||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.hamcrest.Matchers.isA; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@Sql(value = {"/dancers/data.sql"}) | ||
public class DancerControllerTest extends AbstractPostgreSQLEnabledTest { | ||
|
||
@Test | ||
@WithUserDetails("user-with-a-profile@dancier.net") | ||
void getDancersShouldReturnFilteredProfiles() throws Exception { | ||
|
||
mockMvc | ||
.perform(get("/dancers") | ||
.param("range", "20") | ||
.param("gender", "FEMALE") | ||
) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*", isA(List.class))) | ||
.andExpect(jsonPath("$.*", hasSize(1))) | ||
.andExpect(jsonPath("$[0].id").value("503ffad4-148b-4af1-8365-62315ff89b9f")) | ||
.andExpect(jsonPath("$[0].gender").value("FEMALE")) | ||
.andExpect(jsonPath("$[0].dancerName").value("perfect_dancer")) | ||
.andExpect(jsonPath("$[0].aboutMe").value("Hi")) | ||
.andExpect(jsonPath("$[0].age").isNotEmpty()) | ||
.andExpect(jsonPath("$[0].size").value("178")) | ||
.andExpect(jsonPath("$[0].city").value("Dortmund")) | ||
.andExpect(jsonPath("$[0].country").value("GER")); | ||
|
||
} | ||
|
||
@Test | ||
@WithUserDetails("user-with-a-profile@dancier.net") | ||
void getDancersUsingDefaultRange() throws Exception { | ||
|
||
mockMvc | ||
.perform(get("/dancers") | ||
.param("gender", "FEMALE") | ||
) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$.*", isA(List.class))) | ||
.andExpect(jsonPath("$.*", hasSize(1))) | ||
.andExpect(jsonPath("$[0].id").value("503ffad4-148b-4af1-8365-62315ff89b9f")) | ||
.andExpect(jsonPath("$[0].gender").value("FEMALE")) | ||
.andExpect(jsonPath("$[0].dancerName").value("perfect_dancer")) | ||
.andExpect(jsonPath("$[0].aboutMe").value("Hi")) | ||
.andExpect(jsonPath("$[0].age").isNotEmpty()) | ||
.andExpect(jsonPath("$[0].size").value("178")) | ||
.andExpect(jsonPath("$[0].city").value("Dortmund")) | ||
.andExpect(jsonPath("$[0].country").value("GER")); | ||
|
||
} | ||
|
||
@Test | ||
@WithUserDetails("user-with-a-profile@dancier.net") | ||
void shouldFailIfGenderIsNotSet() throws Exception { | ||
mockMvc .perform(get("/dancers") | ||
.param("range", "20") | ||
).andExpect(status().isBadRequest()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/test/java/net/dancier/dancer/recommendation/EndToEndRecommendationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.