Skip to content

Commit

Permalink
added GET dancers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jans510 committed Dec 9, 2024
1 parent 5bc580a commit 6cdf296
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 30 deletions.
15 changes: 0 additions & 15 deletions src/main/java/net/dancier/dancer/core/DancerRepository.java

This file was deleted.

1 change: 1 addition & 0 deletions src/main/java/net/dancier/dancer/core/ProfileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.dancier.dancer.core.model.DanceProfile;
import net.dancier.dancer.core.model.Dancer;
import net.dancier.dancer.core.util.ModelMapper;
import net.dancier.dancer.dancers.DancerRepository;
import net.dancier.dancer.location.ZipCode;
import net.dancier.dancer.location.ZipCodeRepository;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.dancier.dancer.chat;
package net.dancier.dancer.dancers;

import lombok.RequiredArgsConstructor;
import net.dancier.dancer.chat.dto.DancerDto;
import net.dancier.dancer.chat.dto.DancerIdsDto;
import net.dancier.dancer.core.DancerService;
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 @@ -12,8 +13,7 @@
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;
import java.util.*;

import static net.dancier.dancer.authentication.Constants.ROLE_USER;

Expand All @@ -25,6 +25,17 @@ public class DancerController {

private final DancerService dancerService;

@GetMapping("")
@Secured(ROLE_USER)
public ResponseEntity<List<PublicProfileDto>> get(
@CurrentUser AuthenticatedUser authenticatedUser,
@RequestParam Gender gender,
@RequestParam 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));
}

@PostMapping("")
@Secured(ROLE_USER)
public ResponseEntity<HashMap<UUID, DancerDto>> post(
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/net/dancier/dancer/dancers/DancerRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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
);

List<Dancer> findFirst500ByLongitudeBetween(
double lowerLongitude,
double upperLongitude
);

List<Dancer> findFirst500ByGender(
Gender gender
);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package net.dancier.dancer.core;
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;

Expand Down Expand Up @@ -36,4 +39,23 @@ public HashMap<UUID, DancerDto> getDancerMap(DancerIdsDto dancerIdsDto) {

return dancers;
}

public List<PublicProfileDto> getDancersList(AuthenticatedUser authenticatedUser, Gender gender, int range) {

Dancer dancer = loadByUserId(authenticatedUser.getUserId());
Double longitudeRange = (double)range/112;
Double latitudeRange = range/75.78;
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.dancier.dancer.recommendation;

import lombok.RequiredArgsConstructor;
import net.dancier.dancer.core.DancerRepository;
import net.dancier.dancer.dancers.DancerRepository;
import net.dancier.dancer.core.model.Dancer;
import net.dancier.dancer.recommendation.model.BaseRecommendation;
import net.dancier.dancer.recommendation.model.RecommendationWrapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.RequiredArgsConstructor;
import net.dancier.dancer.authentication.model.User;
import net.dancier.dancer.authentication.repository.UserRepository;
import net.dancier.dancer.core.DancerRepository;
import net.dancier.dancer.dancers.DancerRepository;
import net.dancier.dancer.core.model.Dancer;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@
import net.dancier.dancer.AbstractPostgreSQLEnabledTest;
import net.dancier.dancer.chat.client.ChatServiceClient;
import net.dancier.dancer.chat.dto.*;
import net.dancier.dancer.core.DancerRepository;
import net.dancier.dancer.dancers.DancerRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.ResultActions;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/net/dancier/dancer/dancers/DancerControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 org.springframework.test.web.servlet.ResultActions;
import org.springframework.util.MultiValueMap;

import java.util.UUID;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class DancerControllerTest extends AbstractPostgreSQLEnabledTest {


UUID userId = UUID.fromString("55bbf334-6649-11ed-8f65-5b299f0e161f");

@Test
@WithUserDetails("user-with-a-profile@dancier.net")
void getDancersShouldNotReturnOwnProfile() throws Exception {

ResultActions result = mockMvc
.perform(get("/dancers")
.param("range", "20")
.param("gender", "MALE")
)
.andExpect(status().isOk());

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.dancier.dancer.recommendation;

import net.dancier.dancer.AbstractPostgreSQLEnabledTest;
import net.dancier.dancer.core.DancerRepository;
import net.dancier.dancer.dancers.DancerRepository;
import net.dancier.dancer.core.model.Dancer;
import net.dancier.dancer.recommendation.dto.RecommendationDto;
import org.junit.jupiter.api.BeforeEach;
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ SELECT '55bbf334-6649-11ed-8f65-5b299f0e161f',
WHERE name IN ('ROLE_USER', 'ROLE_HUMAN');

INSERT
INTO dancer(user_id, id)
VALUES ('55bbf334-6649-11ed-8f65-5b299f0e161f', '11065e54-664a-11ed-872e-1b1eb88b44b6');
INTO dancer(user_id, id, dancer_name, size, birth_date, gender, country, city, longitude, latitude, about_me)
VALUES ('55bbf334-6649-11ed-8f65-5b299f0e161f', '11065e54-664a-11ed-872e-1b1eb88b44b6', 'good_dancer', '178', '2000-11-11', 'MALE', 'GER', 'Dortmund', '7.1075023', '51.4429498', 'Hi');

-- one admin
-- no profile attached
Expand Down

0 comments on commit 6cdf296

Please sign in to comment.