diff --git a/api/swagger.yaml b/api/swagger.yaml index 17ef452..8415b09 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -652,13 +652,13 @@ components: gender: name: gender in: query - description: preferred gender of the dancers + description: required preferred gender of the dancers schema: type: string range: name: range in: query - description: Range [km] in which the search for dancers should be done + description: Range [km] in which the search for dancers should be done, defaults to 20 km schema: type: string schemas: diff --git a/src/main/java/net/dancier/dancer/dancers/DancerController.java b/src/main/java/net/dancier/dancer/dancers/DancerController.java index be631f8..40382aa 100644 --- a/src/main/java/net/dancier/dancer/dancers/DancerController.java +++ b/src/main/java/net/dancier/dancer/dancers/DancerController.java @@ -30,10 +30,10 @@ public class DancerController { public ResponseEntity> get( @CurrentUser AuthenticatedUser authenticatedUser, @RequestParam Gender gender, - @RequestParam int range + @RequestParam(defaultValue = "20") int range ) { log.info("Fetching list of dancers in {} km range with gender {} for user {}", range, gender, authenticatedUser.getUserId()); - return ResponseEntity.ok(dancerService.getDancersList(authenticatedUser, gender, range)); + return ResponseEntity.ok(dancerService.getDancerList(authenticatedUser, gender, range)); } @PostMapping("") diff --git a/src/main/java/net/dancier/dancer/dancers/DancerService.java b/src/main/java/net/dancier/dancer/dancers/DancerService.java index 2d0a280..8803e7b 100644 --- a/src/main/java/net/dancier/dancer/dancers/DancerService.java +++ b/src/main/java/net/dancier/dancer/dancers/DancerService.java @@ -40,11 +40,15 @@ public HashMap getDancerMap(DancerIdsDto dancerIdsDto) { return dancers; } - public List getDancersList(AuthenticatedUser authenticatedUser, Gender gender, int range) { + public List getDancerList(AuthenticatedUser authenticatedUser, Gender gender, int range) { Dancer dancer = loadByUserId(authenticatedUser.getUserId()); - Double longitudeRange = (double)range/112; - Double latitudeRange = range/75.78; + + // 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; diff --git a/src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java b/src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java index d18de15..b427b37 100644 --- a/src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java +++ b/src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java @@ -4,22 +4,18 @@ import org.junit.jupiter.api.Test; import org.springframework.security.test.context.support.WithUserDetails; import org.springframework.test.context.jdbc.Sql; -import org.springframework.test.web.servlet.ResultActions; - import java.util.List; - -import static net.dancier.dancer.core.model.Gender.FEMALE; 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") - @Sql(value = {"/dancers/data.sql"}) void getDancersShouldReturnFilteredProfiles() throws Exception { mockMvc @@ -40,4 +36,34 @@ void getDancersShouldReturnFilteredProfiles() throws Exception { .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()); + } }