Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:

dancer:
Expand All @@ -13,7 +11,7 @@ services:
environment:
# https://ckinan.com/blog/remote-debug-spring-boot-docker-intellij/:
- SPRING_DATASOURCE_URL=jdbc:postgresql://dancer-db:5432/dancer
- SPRING_PROFILES_ACTIVE="dev"
- SPRING_PROFILES_ACTIVE=dev
- JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
dancer-db:
image: postgres:14
Expand All @@ -40,6 +38,8 @@ services:

prometheus:
image: prom/prometheus
depends_on:
- dancer
ports:
- "9090:9090"
volumes:
Expand Down Expand Up @@ -70,6 +70,8 @@ services:
kafka-ui:
container_name: kafka-ui
image: tchiotludo/akhq:latest
depends_on:
- kafka
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/ui"]
interval: 10s
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.dancier.dancer.core.dto;

import lombok.Data;
import net.dancier.dancer.core.model.Gender;

@Data
public class DancerSearchRequestDto {
private Gender gender;
private int range = 20;
}
2 changes: 1 addition & 1 deletion src/main/java/net/dancier/dancer/core/model/Gender.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public enum Gender {
MALE,
DIVERS,
DIVERSE,
FEMALE,
NA
}
11 changes: 5 additions & 6 deletions src/main/java/net/dancier/dancer/dancers/DancerController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package net.dancier.dancer.dancers;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import net.dancier.dancer.chat.dto.DancerDto;
import net.dancier.dancer.chat.dto.DancerIdsDto;
import net.dancier.dancer.core.dto.DancerSearchRequestDto;
import net.dancier.dancer.core.dto.PublicProfileDto;
import net.dancier.dancer.core.model.Gender;
import net.dancier.dancer.security.AuthenticatedUser;
import net.dancier.dancer.security.CurrentUser;
import org.slf4j.Logger;
Expand All @@ -29,11 +30,9 @@ public class DancerController {
@Secured(ROLE_USER)
public ResponseEntity<List<PublicProfileDto>> get(
@CurrentUser AuthenticatedUser authenticatedUser,
@RequestParam Gender gender,
@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.getDancerList(authenticatedUser, gender, range));
@Valid DancerSearchRequestDto searchRequest) {
log.info("Fetching list of dancers in {} km range with gender {} for user {}", searchRequest.getRange(), searchRequest.getGender(), authenticatedUser.getUserId());
return ResponseEntity.ok(dancerService.getDancerList(authenticatedUser, searchRequest.getGender(), searchRequest.getRange()));
}

@PostMapping("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ List<Dancer> findFirst500ByGenderAndLongitudeBetweenAndLatitudeBetween(
double upperLatitude
);

List<Dancer> findFirst500ByLongitudeBetweenAndLatitudeBetween(
double lowerLongitude,
double upperLongitude,
double lowerLatitude,
double upperLatitude
);

}
10 changes: 8 additions & 2 deletions src/main/java/net/dancier/dancer/dancers/DancerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ public List<PublicProfileDto> getDancerList(AuthenticatedUser authenticatedUser,
double upperLongitude = dancer.getLongitude() + longitudeRange;
double lowerLongitude = dancer.getLongitude() - longitudeRange;

List<Dancer> resultList = dancerRepository.findFirst500ByGenderAndLongitudeBetweenAndLatitudeBetween(
gender, lowerLongitude, upperLongitude, lowerLatitude, upperLatitude);
List<Dancer> resultList;
if (gender == null) {
resultList = dancerRepository.findFirst500ByLongitudeBetweenAndLatitudeBetween(
lowerLongitude, upperLongitude, lowerLatitude, upperLatitude);
} else {
resultList = dancerRepository.findFirst500ByGenderAndLongitudeBetweenAndLatitudeBetween(
gender, lowerLongitude, upperLongitude, lowerLatitude, upperLatitude);
}

return resultList.stream()
.map(PublicProfileDto::of)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.test.web.servlet.ResultActions;

import java.time.LocalDate;
import java.util.Date;
import java.util.Set;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -68,7 +67,7 @@ void fromVirginProfileToPopulatedProfile() throws Exception {
danceProfileDto.setLeading(Leading.FOLLOW);


profileOfCurrentUserDto.setGender(Gender.DIVERS);
profileOfCurrentUserDto.setGender(Gender.DIVERSE);
profileOfCurrentUserDto.setBirthDate(LocalDate.now());
profileOfCurrentUserDto.setAbleTo(Set.of(danceProfileDto));
profileOfCurrentUserDto.setWantsTo(Set.of(danceProfileDto));
Expand Down
39 changes: 33 additions & 6 deletions src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.dancier.dancer.dancers;

import net.dancier.dancer.AbstractPostgreSQLEnabledTest;
import org.junit.jupiter.api.DisplayName;
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.hamcrest.Matchers.*;
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;
Expand Down Expand Up @@ -61,9 +62,35 @@ void getDancersUsingDefaultRange() throws Exception {

@Test
@WithUserDetails("user-with-a-profile@dancier.net")
void shouldFailIfGenderIsNotSet() throws Exception {
mockMvc .perform(get("/dancers")
.param("range", "20")
).andExpect(status().isBadRequest());
@DisplayName("Search with no specified gender should return all genders")
void getDancers_whenGenderIsOmitted_shouldReturnAllGenders() throws Exception {
mockMvc.perform(get("/dancers")
.param("range", "20")
)
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[*].gender", containsInAnyOrder("FEMALE", "MALE")));
}

@Test
@WithUserDetails("user-with-a-profile@dancier.net")
@DisplayName("Search that matches no dancers should return an empty list")
void getDancers_whenNoDancersMatch_shouldReturnEmptyList() throws Exception {
mockMvc.perform(get("/dancers")
.param("range", "200")
.param("gender", "DIVERSE") // Assuming no DIVERSE dancers are in the data.sql
)
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(0)));
}

@Test
@DisplayName("Search without authentication should be rejected")
void getDancers_whenNotAuthenticated_shouldReturnUnauthorized() throws Exception {
mockMvc.perform(get("/dancers")
.param("range", "20")
.param("gender", "FEMALE")
)
.andExpect(status().isForbidden());
}
}
Loading