Skip to content

Commit

Permalink
feat: 회원탈퇴, 로그아웃 시, 회원 위치 데이터 삭제 로직 추가 및 위치 데이터 유효기간 변경 (#179)
Browse files Browse the repository at this point in the history
* refactor: 주변 위치 데이터 응답 데이터 구조 변경 및 유효기간 30 초 설정 (#178)

* feat: 회원탈퇴, 로그아웃 시 저장되어 있는 위치 정보 Redis 에서 삭제 (#178)

* feat: 회원탈퇴, 로그아웃 시 저장되어 있는 위치 정보 Redis 삭제 메소드 추가 (#178)

* test: 로직 수정에 따른 단위 테스트 수정 (#178)

* refactor: UserService 로그아웃, 회원 탈퇴 시 위치 데이터 삭제 로직 제거 (#178)

* refactor: logout @transactional 제거 (#178)

* refactor: TeumTeumService 리팩토링 (#178)

* test: 회원탈퇴, 로그아웃 유닛 테스트 수정 (#178)
  • Loading branch information
choidongkuen authored Feb 11, 2024
1 parent bc220a5 commit 53d28ce
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setUserLocation(UserLocation userLocation, Long duration) {
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(e);
}
setDataWithExpiration(key,value,duration);
setDataWithExpiration(key, value, duration);
}

public Set<UserLocation> getAllUserLocations() {
Expand All @@ -69,4 +69,8 @@ public Set<UserLocation> getAllUserLocations() {
}
}).collect(Collectors.toSet());
}

public void deleteUserLocation(Long id) {
deleteData(HASH_KEY + id);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package net.teumteum.teum_teum.domain.response;

import java.util.List;
import java.util.Set;
import net.teumteum.teum_teum.domain.UserLocation;

public record UserAroundLocationsResponse(
List<UserAroundLocationResponse> aroundUserLocations
) {

public static UserAroundLocationsResponse of(List<UserLocation> userData) {
public static UserAroundLocationsResponse of(Set<UserLocation> userData) {
return new UserAroundLocationsResponse(
userData.stream()
.map(UserAroundLocationResponse::of)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,43 @@
import static java.lang.Math.toRadians;
import static java.util.Comparator.comparingDouble;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.teumteum.core.security.service.RedisService;
import net.teumteum.teum_teum.domain.UserLocation;
import net.teumteum.teum_teum.domain.request.UserLocationRequest;
import net.teumteum.teum_teum.domain.response.UserAroundLocationsResponse;
import org.springframework.stereotype.Service;

@Slf4j

@Service
@RequiredArgsConstructor
public class TeumTeumService {

private static final int SEARCH_LIMIT = 6;
private static final long USER_LOCATION_DATA_DURATION = 10L;
private static final int AROUND_USER_LOCATION_DISTANCE = 100;

private final RedisService redisService;

public UserAroundLocationsResponse saveAndGetUserAroundLocations(UserLocationRequest request) {
redisService.setUserLocation(request.toUserLocation(), 60L);
redisService.setUserLocation(request.toUserLocation(), USER_LOCATION_DATA_DURATION);
return getUserAroundLocations(request);
}

private UserAroundLocationsResponse getUserAroundLocations(UserLocationRequest request) {
Set<UserLocation> allUserLocations = redisService.getAllUserLocations();

List<UserLocation> aroundUserLocations = allUserLocations.stream()
Set<UserLocation> aroundUserLocations = allUserLocations.stream()
.filter(userLocation -> !userLocation.id().equals(request.id()))
.filter(userLocation -> calculateDistance(request.latitude(), request.longitude(),
userLocation.latitude(), userLocation.longitude()) <= 100)
userLocation.latitude(), userLocation.longitude()) <= AROUND_USER_LOCATION_DISTANCE)
.sorted(comparingDouble(userLocation
-> calculateDistance(request.latitude(), request.longitude(),
userLocation.latitude(), userLocation.longitude()))
).limit(SEARCH_LIMIT)
.toList();
.collect(Collectors.toSet());

return UserAroundLocationsResponse.of(aroundUserLocations);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/teumteum/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ public void addFriends(Long myId, Long friendId) {
public void withdraw(UserWithdrawRequest request, Long userId) {
var existUser = getUser(userId);

userRepository.delete(existUser);
redisService.deleteData(String.valueOf(userId));

userRepository.delete(existUser);

withdrawReasonRepository.saveAll(request.toEntity());
}

Expand All @@ -95,9 +96,9 @@ public UserRegisterResponse register(UserRegisterRequest request) {
return UserRegisterResponse.of(savedUser.getId(), jwtService.createServiceToken(savedUser));
}

@Transactional
public void logout(Long userId) {
redisService.deleteData(String.valueOf(userId));

SecurityService.clearSecurityContext();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.MediaType.APPLICATION_JSON;
Expand Down Expand Up @@ -220,4 +221,25 @@ void Get_user_reviews_with_200_ok() throws Exception {
.andExpect(jsonPath("$[0].review").value("별로에요"));
}
}

@Nested
@DisplayName("회원 로그아웃 API는")
class Logout_user_api_unit {

@Test
@DisplayName("")
void Logout_user_with_200_ok() throws Exception {
// given
var userId = 1L;

doNothing().when(userService).logout(anyLong());

// when && then
mockMvc.perform(post("/users/logouts")
.with(csrf())
.header(AUTHORIZATION, VALID_ACCESS_TOKEN))
.andDo(print())
.andExpect(status().isOk());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class UserServiceTest {
@Mock
MeetingConnector meetingConnector;


private User user;

@BeforeEach
Expand Down Expand Up @@ -111,7 +112,26 @@ void If_user_already_exist_register_user_card_fail() {
}

@Nested
@DisplayName("유저 탈퇴 API는")
@DisplayName("회원 로그아웃 API는")
class Logout_user_api_unit {

@Test
@DisplayName("정상적인 요청시, 회원 로그아웃을 진행하고 200 OK을 반환한다.")
void If_valid_user_logout_request_return_200_OK() {
// given
Long userId = 1L;
doNothing().when(redisService).deleteData(anyString());

// when
userService.logout(userId);

// then
verify(redisService, times(1)).deleteData(anyString());
}
}

@Nested
@DisplayName("회원 탈퇴 API는")
class Withdraw_user_api_unit {

@Test
Expand Down

0 comments on commit 53d28ce

Please sign in to comment.