Skip to content

Commit e086e8c

Browse files
authored
refactor: Location 도메인 리팩터링 (#340)
* refactor: InterestedCountryRepository rename * refactor: InterestedCountry, InterestedRegion에 복합 unique 제약 조건 설정 - 테이블에 제약 조건 설정하는 sql 파일 작성 * test: interested_region, interested_country에 대한 테스트 코드 작성 * chore: CountryRepositoryTest, RegionRepositoryTest 파일명 앞에 Interested 추가 * chore: 파일 끝 개행 추가 * chore: UK 제약조건 이름 변경 * chore: 마이그레이션 파일 버전 수정 V15 -> V16
1 parent 0144cb2 commit e086e8c

File tree

10 files changed

+211
-16
lines changed

10 files changed

+211
-16
lines changed

src/main/java/com/example/solidconnection/auth/service/EmailSignUpService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.example.solidconnection.auth.dto.SignUpRequest;
44
import com.example.solidconnection.common.exception.CustomException;
55
import com.example.solidconnection.location.country.repository.CountryRepository;
6-
import com.example.solidconnection.location.country.repository.InterestedCountyRepository;
6+
import com.example.solidconnection.location.country.repository.InterestedCountryRepository;
77
import com.example.solidconnection.location.region.repository.InterestedRegionRepository;
88
import com.example.solidconnection.location.region.repository.RegionRepository;
99
import com.example.solidconnection.siteuser.domain.AuthType;
@@ -20,9 +20,9 @@ public class EmailSignUpService extends SignUpService {
2020

2121
public EmailSignUpService(SignInService signInService, SiteUserRepository siteUserRepository,
2222
RegionRepository regionRepository, InterestedRegionRepository interestedRegionRepository,
23-
CountryRepository countryRepository, InterestedCountyRepository interestedCountyRepository,
23+
CountryRepository countryRepository, InterestedCountryRepository interestedCountryRepository,
2424
EmailSignUpTokenProvider emailSignUpTokenProvider) {
25-
super(signInService, siteUserRepository, regionRepository, interestedRegionRepository, countryRepository, interestedCountyRepository);
25+
super(signInService, siteUserRepository, regionRepository, interestedRegionRepository, countryRepository, interestedCountryRepository);
2626
this.emailSignUpTokenProvider = emailSignUpTokenProvider;
2727
}
2828

src/main/java/com/example/solidconnection/auth/service/SignUpService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.example.solidconnection.common.exception.CustomException;
66
import com.example.solidconnection.location.country.domain.InterestedCountry;
77
import com.example.solidconnection.location.country.repository.CountryRepository;
8-
import com.example.solidconnection.location.country.repository.InterestedCountyRepository;
8+
import com.example.solidconnection.location.country.repository.InterestedCountryRepository;
99
import com.example.solidconnection.location.region.domain.InterestedRegion;
1010
import com.example.solidconnection.location.region.repository.InterestedRegionRepository;
1111
import com.example.solidconnection.location.region.repository.RegionRepository;
@@ -31,17 +31,17 @@ public abstract class SignUpService {
3131
protected final RegionRepository regionRepository;
3232
protected final InterestedRegionRepository interestedRegionRepository;
3333
protected final CountryRepository countryRepository;
34-
protected final InterestedCountyRepository interestedCountyRepository;
34+
protected final InterestedCountryRepository interestedCountryRepository;
3535

3636
protected SignUpService(SignInService signInService, SiteUserRepository siteUserRepository,
3737
RegionRepository regionRepository, InterestedRegionRepository interestedRegionRepository,
38-
CountryRepository countryRepository, InterestedCountyRepository interestedCountyRepository) {
38+
CountryRepository countryRepository, InterestedCountryRepository interestedCountryRepository) {
3939
this.signInService = signInService;
4040
this.siteUserRepository = siteUserRepository;
4141
this.regionRepository = regionRepository;
4242
this.interestedRegionRepository = interestedRegionRepository;
4343
this.countryRepository = countryRepository;
44-
this.interestedCountyRepository = interestedCountyRepository;
44+
this.interestedCountryRepository = interestedCountryRepository;
4545
}
4646

4747
@Transactional
@@ -81,7 +81,7 @@ private void saveInterestedCountry(SignUpRequest signUpRequest, SiteUser savedSi
8181
List<InterestedCountry> interestedCountries = countryRepository.findByKoreanNames(interestedCountryNames).stream()
8282
.map(country -> new InterestedCountry(savedSiteUser, country))
8383
.toList();
84-
interestedCountyRepository.saveAll(interestedCountries);
84+
interestedCountryRepository.saveAll(interestedCountries);
8585
}
8686

8787
protected abstract void validateSignUpToken(SignUpRequest signUpRequest);

src/main/java/com/example/solidconnection/auth/service/oauth/OAuthSignUpService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.example.solidconnection.auth.service.SignUpService;
66
import com.example.solidconnection.common.exception.CustomException;
77
import com.example.solidconnection.location.country.repository.CountryRepository;
8-
import com.example.solidconnection.location.country.repository.InterestedCountyRepository;
8+
import com.example.solidconnection.location.country.repository.InterestedCountryRepository;
99
import com.example.solidconnection.location.region.repository.InterestedRegionRepository;
1010
import com.example.solidconnection.location.region.repository.RegionRepository;
1111
import com.example.solidconnection.siteuser.domain.AuthType;
@@ -22,9 +22,9 @@ public class OAuthSignUpService extends SignUpService {
2222

2323
OAuthSignUpService(SignInService signInService, SiteUserRepository siteUserRepository,
2424
RegionRepository regionRepository, InterestedRegionRepository interestedRegionRepository,
25-
CountryRepository countryRepository, InterestedCountyRepository interestedCountyRepository,
25+
CountryRepository countryRepository, InterestedCountryRepository interestedCountryRepository,
2626
OAuthSignUpTokenProvider oAuthSignUpTokenProvider) {
27-
super(signInService, siteUserRepository, regionRepository, interestedRegionRepository, countryRepository, interestedCountyRepository);
27+
super(signInService, siteUserRepository, regionRepository, interestedRegionRepository, countryRepository, interestedCountryRepository);
2828
this.oAuthSignUpTokenProvider = oAuthSignUpTokenProvider;
2929
}
3030

src/main/java/com/example/solidconnection/location/country/domain/InterestedCountry.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
import jakarta.persistence.GenerationType;
77
import jakarta.persistence.Id;
88
import jakarta.persistence.ManyToOne;
9+
import jakarta.persistence.Table;
10+
import jakarta.persistence.UniqueConstraint;
911
import lombok.AccessLevel;
1012
import lombok.Getter;
1113
import lombok.NoArgsConstructor;
1214

1315
@Getter
1416
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1517
@Entity
18+
@Table(uniqueConstraints = {
19+
@UniqueConstraint(
20+
name = "uk_interested_country_site_user_id_country_code",
21+
columnNames = {"site_user_id", "country_code"}
22+
)
23+
})
1624
public class InterestedCountry {
1725

1826
@Id

src/main/java/com/example/solidconnection/location/country/repository/InterestedCountyRepository.java renamed to src/main/java/com/example/solidconnection/location/country/repository/InterestedCountryRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
import java.util.List;
99

1010
@Repository
11-
public interface InterestedCountyRepository extends JpaRepository<InterestedCountry, Long> {
11+
public interface InterestedCountryRepository extends JpaRepository<InterestedCountry, Long> {
1212
List<InterestedCountry> findAllBySiteUser(SiteUser siteUser);
1313
}

src/main/java/com/example/solidconnection/location/region/domain/InterestedRegion.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,21 @@
66
import jakarta.persistence.GenerationType;
77
import jakarta.persistence.Id;
88
import jakarta.persistence.ManyToOne;
9+
import jakarta.persistence.Table;
10+
import jakarta.persistence.UniqueConstraint;
911
import lombok.AccessLevel;
1012
import lombok.Getter;
1113
import lombok.NoArgsConstructor;
1214

1315
@Getter
1416
@NoArgsConstructor(access = AccessLevel.PROTECTED)
1517
@Entity
18+
@Table(uniqueConstraints = {
19+
@UniqueConstraint(
20+
name = "uk_interested_region_site_user_id_region_code",
21+
columnNames = {"site_user_id", "region_code"}
22+
)
23+
})
1624
public class InterestedRegion {
1725

1826
@Id
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ALTER TABLE interested_country
2+
ADD CONSTRAINT uk_interested_country_site_user_id_country_code
3+
UNIQUE (site_user_id, country_code);
4+
5+
ALTER TABLE interested_region
6+
ADD CONSTRAINT uk_interested_region_site_user_id_region_code
7+
UNIQUE (site_user_id, region_code);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.example.solidconnection.location.country.repository;
2+
3+
import com.example.solidconnection.location.country.domain.Country;
4+
import com.example.solidconnection.location.country.domain.InterestedCountry;
5+
import com.example.solidconnection.location.country.fixture.CountryFixture;
6+
import com.example.solidconnection.siteuser.domain.SiteUser;
7+
import com.example.solidconnection.siteuser.fixture.SiteUserFixture;
8+
import com.example.solidconnection.support.TestContainerSpringBootTest;
9+
import org.junit.jupiter.api.Nested;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.dao.DataIntegrityViolationException;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.assertj.core.api.Assertions.assertThatCode;
16+
17+
@TestContainerSpringBootTest
18+
public class InterestedCountryRepositoryTest {
19+
20+
@Autowired
21+
private InterestedCountryRepository interestedCountryRepository;
22+
23+
@Autowired
24+
private SiteUserFixture siteUserFixture;
25+
26+
@Autowired
27+
private CountryFixture countryFixture;
28+
29+
@Nested
30+
class 사용자와_나라는_복합_유니크_제약_조건을_가진다 {
31+
32+
@Test
33+
void 같은_사용자가_같은_나라에_관심_표시를_하면_예외_응답을_반환한다() {
34+
// given
35+
SiteUser user = siteUserFixture.사용자();
36+
Country country = countryFixture.미국();
37+
38+
InterestedCountry firstInterest = new InterestedCountry(user, country);
39+
interestedCountryRepository.save(firstInterest);
40+
41+
InterestedCountry secondInterest = new InterestedCountry(user, country);
42+
43+
// when & then
44+
assertThatCode(() -> interestedCountryRepository.save(secondInterest))
45+
.isInstanceOf(DataIntegrityViolationException.class);
46+
}
47+
48+
@Test
49+
void 다른_사용자가_같은_나라에_관심_표시를_하면_정상_저장된다() {
50+
// given
51+
SiteUser user1 = siteUserFixture.사용자(1, "user1");
52+
SiteUser user2 = siteUserFixture.사용자(2, "user2");
53+
Country country = countryFixture.미국();
54+
55+
InterestedCountry firstInterest = new InterestedCountry(user1, country);
56+
interestedCountryRepository.save(firstInterest);
57+
58+
InterestedCountry secondInterest = new InterestedCountry(user2, country);
59+
60+
// when & then
61+
assertThatCode(() -> {
62+
InterestedCountry saved = interestedCountryRepository.save(secondInterest);
63+
assertThat(saved.getId()).isNotNull();
64+
}).doesNotThrowAnyException();
65+
}
66+
67+
@Test
68+
void 같은_사용자가_다른_나라에_관심_표시를_하면_정상_저장된다() {
69+
// given
70+
SiteUser user = siteUserFixture.사용자();
71+
Country country1 = countryFixture.미국();
72+
Country country2 = countryFixture.일본();
73+
74+
InterestedCountry firstInterest = new InterestedCountry(user, country1);
75+
interestedCountryRepository.save(firstInterest);
76+
77+
InterestedCountry secondInterest = new InterestedCountry(user, country2);
78+
79+
// when & then
80+
assertThatCode(() -> {
81+
InterestedCountry saved = interestedCountryRepository.save(secondInterest);
82+
assertThat(saved.getId()).isNotNull();
83+
}).doesNotThrowAnyException();
84+
}
85+
}
86+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.example.solidconnection.location.region.repository;
2+
3+
import com.example.solidconnection.location.region.domain.InterestedRegion;
4+
import com.example.solidconnection.location.region.domain.Region;
5+
import com.example.solidconnection.location.region.fixture.RegionFixture;
6+
import com.example.solidconnection.siteuser.domain.SiteUser;
7+
import com.example.solidconnection.siteuser.fixture.SiteUserFixture;
8+
import com.example.solidconnection.support.TestContainerSpringBootTest;
9+
import org.junit.jupiter.api.Nested;
10+
import org.junit.jupiter.api.Test;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.dao.DataIntegrityViolationException;
13+
14+
import static org.assertj.core.api.Assertions.assertThat;
15+
import static org.assertj.core.api.Assertions.assertThatCode;
16+
17+
@TestContainerSpringBootTest
18+
public class InterestedRegionRepositoryTest {
19+
20+
@Autowired
21+
private InterestedRegionRepository interestedRegionRepository;
22+
23+
@Autowired
24+
private SiteUserFixture siteUserFixture;
25+
26+
@Autowired
27+
private RegionFixture regionFixture;
28+
29+
@Nested
30+
class 사용자와_지역은_복합_유니크_제약_조건을_가진다 {
31+
32+
@Test
33+
void 같은_사용자가_같은_지역에_관심_표시를_하면_예외_응답을_반환한다() {
34+
// given
35+
SiteUser user = siteUserFixture.사용자();
36+
Region region = regionFixture.영미권();
37+
38+
InterestedRegion firstInterest = new InterestedRegion(user, region);
39+
interestedRegionRepository.save(firstInterest);
40+
41+
InterestedRegion secondInterest = new InterestedRegion(user, region);
42+
43+
// when & then
44+
assertThatCode(() -> interestedRegionRepository.save(secondInterest))
45+
.isInstanceOf(DataIntegrityViolationException.class);
46+
}
47+
48+
@Test
49+
void 다른_사용자가_같은_지역에_관심_표시를_하면_정상_저장된다() {
50+
// given
51+
SiteUser user1 = siteUserFixture.사용자(1, "user1");
52+
SiteUser user2 = siteUserFixture.사용자(2, "user2");
53+
Region region = regionFixture.영미권();
54+
55+
InterestedRegion firstInterest = new InterestedRegion(user1, region);
56+
interestedRegionRepository.save(firstInterest);
57+
58+
InterestedRegion secondInterest = new InterestedRegion(user2, region);
59+
60+
// when & then
61+
assertThatCode(() -> {
62+
InterestedRegion saved = interestedRegionRepository.save(secondInterest);
63+
assertThat(saved.getId()).isNotNull();
64+
}).doesNotThrowAnyException();
65+
}
66+
67+
@Test
68+
void 같은_사용자가_다른_지역에_관심_표시를_하면_정상_저장된다() {
69+
// given
70+
SiteUser user = siteUserFixture.사용자();
71+
Region region1 = regionFixture.영미권();
72+
Region region2 = regionFixture.유럽();
73+
74+
InterestedRegion firstInterest = new InterestedRegion(user, region1);
75+
interestedRegionRepository.save(firstInterest);
76+
77+
InterestedRegion secondInterest = new InterestedRegion(user, region2);
78+
79+
// when & then
80+
assertThatCode(() -> {
81+
InterestedRegion saved = interestedRegionRepository.save(secondInterest);
82+
assertThat(saved.getId()).isNotNull();
83+
}).doesNotThrowAnyException();
84+
}
85+
}
86+
}

src/test/java/com/example/solidconnection/university/service/UniversityRecommendServiceTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.example.solidconnection.location.country.domain.InterestedCountry;
44
import com.example.solidconnection.location.country.fixture.CountryFixture;
5-
import com.example.solidconnection.location.country.repository.InterestedCountyRepository;
5+
import com.example.solidconnection.location.country.repository.InterestedCountryRepository;
66
import com.example.solidconnection.location.region.domain.InterestedRegion;
77
import com.example.solidconnection.location.region.fixture.RegionFixture;
88
import com.example.solidconnection.location.region.repository.InterestedRegionRepository;
@@ -34,7 +34,7 @@ class UniversityRecommendServiceTest {
3434
private InterestedRegionRepository interestedRegionRepository;
3535

3636
@Autowired
37-
private InterestedCountyRepository interestedCountyRepository;
37+
private InterestedCountryRepository interestedCountryRepository;
3838

3939
@Autowired
4040
private GeneralUniversityRecommendService generalUniversityRecommendService;
@@ -97,7 +97,7 @@ void setUp() {
9797
@Test
9898
void 관심_국가_설정한_사용자의_맞춤_추천_대학을_조회한다() {
9999
// given
100-
interestedCountyRepository.save(new InterestedCountry(user, countryFixture.덴마크()));
100+
interestedCountryRepository.save(new InterestedCountry(user, countryFixture.덴마크()));
101101

102102
// when
103103
UniversityRecommendsResponse response = universityRecommendService.getPersonalRecommends(user);
@@ -115,7 +115,7 @@ void setUp() {
115115
void 관심_지역과_국가_모두_설정한_사용자의_맞춤_추천_대학을_조회한다() {
116116
// given
117117
interestedRegionRepository.save(new InterestedRegion(user, regionFixture.영미권()));
118-
interestedCountyRepository.save(new InterestedCountry(user, countryFixture.덴마크()));
118+
interestedCountryRepository.save(new InterestedCountry(user, countryFixture.덴마크()));
119119

120120
// when
121121
UniversityRecommendsResponse response = universityRecommendService.getPersonalRecommends(user);

0 commit comments

Comments
 (0)