Skip to content

Commit

Permalink
[#46] 사용자 동네 인증 기능 영속성 계층 구현 (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Srltas authored Oct 24, 2024
1 parent 44c6ea8 commit 3ef8a43
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.srltas.runtogether.adapter.out.persistence.mybatis;

import org.apache.ibatis.annotations.Mapper;

import com.srltas.runtogether.adapter.out.persistence.mybatis.dao.VerifiedUserNeighborhoodDAO;

@Mapper
public interface MybatisUserMapper {
void updateVerifiedUserNeighborhood(VerifiedUserNeighborhoodDAO dao);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.srltas.runtogether.adapter.out.persistence.mybatis;

import static com.srltas.runtogether.adapter.out.persistence.mybatis.converter.UserConverter.*;

import java.util.Optional;

import org.springframework.stereotype.Repository;

import com.srltas.runtogether.domain.model.user.User;
import com.srltas.runtogether.domain.model.user.UserNeighborhood;
import com.srltas.runtogether.domain.model.user.UserRepository;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class MybatisUserRepository implements UserRepository {

private final MybatisUserMapper mybatisUserMapper;

@Override
public Optional<User> findById(long id) {
// TODO 사용자 조회 구현
return Optional.empty();
}

@Override
public void save(User user) {
// TODO 사용자 저장 구현
}

@Override
public void updateVerifiedUserNeighborhood(long userId, UserNeighborhood userNeighborhood) {
mybatisUserMapper.updateVerifiedUserNeighborhood(toVerifiedUserNeighborhoodDAO(userId, userNeighborhood));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.srltas.runtogether.adapter.out.persistence.mybatis.converter;

import com.srltas.runtogether.adapter.out.persistence.mybatis.dao.VerifiedUserNeighborhoodDAO;
import com.srltas.runtogether.domain.model.user.UserNeighborhood;

import lombok.experimental.UtilityClass;

@UtilityClass
public class UserConverter {

public VerifiedUserNeighborhoodDAO toVerifiedUserNeighborhoodDAO(long userId, UserNeighborhood userNeighborhood) {
return VerifiedUserNeighborhoodDAO.builder()
.userId(userId)
.neighborhoodId(userNeighborhood.getNeighborhood().getId())
.verified(userNeighborhood.isVerified())
.verifiedAt(userNeighborhood.getVerifiedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.srltas.runtogether.adapter.out.persistence.mybatis.dao;

import java.time.LocalDateTime;

import lombok.Builder;

@Builder
public record VerifiedUserNeighborhoodDAO(
long userId,
int neighborhoodId,
boolean verified,
LocalDateTime verifiedAt
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.srltas.runtogether.application.mappper.LocationMapper.*;

import java.time.LocalDateTime;
import java.util.UUID;

import org.springframework.stereotype.Service;
Expand All @@ -17,6 +16,7 @@
import com.srltas.runtogether.domain.model.neighborhood.Neighborhood;
import com.srltas.runtogether.domain.model.neighborhood.NeighborhoodRepository;
import com.srltas.runtogether.domain.model.user.User;
import com.srltas.runtogether.domain.model.user.UserNeighborhood;
import com.srltas.runtogether.domain.model.user.UserRepository;

import lombok.RequiredArgsConstructor;
Expand All @@ -39,13 +39,14 @@ public NeighborhoodVerificationResult verifyAndRegisterNeighborhood(long userId,
if (neighborhood.isWithinBoundary(currentLocation)) {
User user = userRepository.findById(userId)
.orElseThrow(UserNotFoundException::new);
user.verifiedNeighborhood(neighborhood.getId());
userRepository.save(user);

UserNeighborhood userNeighborhood = user.verifiedNeighborhood(neighborhood.getId());
userRepository.updateVerifiedUserNeighborhood(user.getId(), userNeighborhood);

return NeighborhoodVerificationResult.builder()
.verifyId(UUID.randomUUID().toString())
.verified(true)
.verificationTime(LocalDateTime.now().toString())
.verificationTime(userNeighborhood.getVerifiedAt().toString())
.build();
} else {
throw new OutOfNeighborhoodBoundaryException();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.srltas.runtogether.application.port.out;

import java.time.LocalDateTime;

import lombok.Builder;

@Builder
public record VerifyUserNeighborhoodCommand(
long userId,
int neighborhoodId,
boolean verified,
LocalDateTime verifiedAt
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public void addNeighborhood(Neighborhood neighborhood) {
userNeighborhoods.put(neighborhood.getId(), new UserNeighborhood(neighborhood));
}

public void verifiedNeighborhood(int neighborhoodId) {
public UserNeighborhood verifiedNeighborhood(int neighborhoodId) {
UserNeighborhood userNeighborhood = userNeighborhoods.get(neighborhoodId);

if (isNull(userNeighborhood)) {
throw new CommonException("해당 동네가 등록되지 않았습니다.");
}

userNeighborhood.verifyNeighborhood();
return userNeighborhood;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.util.Optional;

import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository {
Optional<User> findById(long id);
void save(User user);
void updateVerifiedUserNeighborhood(long userId, UserNeighborhood neighborhood);
}
12 changes: 12 additions & 0 deletions src/main/resources/mapper/UserMapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.srltas.runtogether.adapter.out.persistence.mybatis.MybatisUserMapper">

<update id="updateVerifiedUserNeighborhood" parameterType="VerifiedUserNeighborhoodDAO">
UPDATE user_neighborhoods
SET verified = #{verified}, verified_at = #{verifiedAt}
WHERE user_id = #{userId} AND neighborhood_id = #{neighborhoodId}
</update>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.srltas.runtogether.adapter.out.persistence.mybatis;

import static org.mockito.BDDMockito.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.srltas.runtogether.adapter.out.persistence.mybatis.dao.VerifiedUserNeighborhoodDAO;
import com.srltas.runtogether.domain.model.neighborhood.Location;
import com.srltas.runtogether.domain.model.neighborhood.Neighborhood;
import com.srltas.runtogether.domain.model.user.UserNeighborhood;

@ExtendWith(MockitoExtension.class)
class MybatisUserRepositoryTest {

@Mock
private MybatisUserMapper mybatisUserMapper;

@InjectMocks
private MybatisUserRepository mybatisUserRepository;

@Test
@DisplayName("동네 인증 정보 업데이트 SQL 실행 위임을 검증")
void shouldCallMapperToUserRepository() {
long userId = 1L;
Neighborhood neighborhood = new Neighborhood(1, "Test Neighborhood",
new Location(37.505858, 127.058319), 5.0);
UserNeighborhood userNeighborhood = new UserNeighborhood(neighborhood);

mybatisUserRepository.updateVerifiedUserNeighborhood(userId, userNeighborhood);

verify(mybatisUserMapper).updateVerifiedUserNeighborhood(any(VerifiedUserNeighborhoodDAO.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.*;

import java.time.LocalDateTime;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -27,6 +28,7 @@
import com.srltas.runtogether.domain.model.neighborhood.Neighborhood;
import com.srltas.runtogether.domain.model.neighborhood.NeighborhoodRepository;
import com.srltas.runtogether.domain.model.user.User;
import com.srltas.runtogether.domain.model.user.UserNeighborhood;
import com.srltas.runtogether.domain.model.user.UserRepository;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -64,9 +66,13 @@ public void setUp() {
@Test
@DisplayName("사용자가 동네 경계 안에 있을 때 동네 인증 성공")
public void testVerifyAndRegisterNeighborhood_WithinBoundary() {
User user = new User(1L, "testUser");
user.addNeighborhood(neighborhood);
Location location = new Location(1L, 1L);
User user = mock(User.class);
UserNeighborhood userNeighborhood = mock(UserNeighborhood.class);
Location location = mock(Location.class);

given(user.getId()).willReturn(1L);
given(user.verifiedNeighborhood(neighborhood.getId())).willReturn(userNeighborhood);
given(userNeighborhood.getVerifiedAt()).willReturn(LocalDateTime.now());
given(userRepository.findById(1L)).willReturn(Optional.of(user));

try (MockedStatic<LocationMapper> locationMapperMock = mockStatic(LocationMapper.class);
Expand All @@ -82,7 +88,7 @@ public void testVerifyAndRegisterNeighborhood_WithinBoundary() {

neighborhoodVerificationService.verifyAndRegisterNeighborhood(1L, neighborhoodVerificationCommand);
}
then(userRepository).should().save(user);
then(userRepository).should().updateVerifiedUserNeighborhood(1L, userNeighborhood);
}

@Test
Expand Down

0 comments on commit 3ef8a43

Please sign in to comment.