Skip to content

Commit

Permalink
added review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jans510 committed Dec 22, 2024
1 parent a4f0826 commit f02600a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
4 changes: 2 additions & 2 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class DancerController {
public ResponseEntity<List<PublicProfileDto>> 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("")
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/dancier/dancer/dancers/DancerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ public HashMap<UUID, DancerDto> getDancerMap(DancerIdsDto dancerIdsDto) {
return dancers;
}

public List<PublicProfileDto> getDancersList(AuthenticatedUser authenticatedUser, Gender gender, int range) {
public List<PublicProfileDto> 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;
Expand Down
36 changes: 31 additions & 5 deletions src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("[email protected]")
@Sql(value = {"/dancers/data.sql"})
void getDancersShouldReturnFilteredProfiles() throws Exception {

mockMvc
Expand All @@ -40,4 +36,34 @@ void getDancersShouldReturnFilteredProfiles() throws Exception {
.andExpect(jsonPath("$[0].country").value("GER"));

}

@Test
@WithUserDetails("[email protected]")
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("[email protected]")
void shouldFailIfGenderIsNotSet() throws Exception {
mockMvc .perform(get("/dancers")
.param("range", "20")
).andExpect(status().isBadRequest());
}
}

0 comments on commit f02600a

Please sign in to comment.