-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] - 여행기 나라 관련 리팩토링 #586
Changes from 11 commits
fbc06f5
2ae1432
dcb07e4
aefa010
5704600
44f81ef
d36abe6
7959937
ffd5fa8
4e09f4e
8bcc217
a166d4b
0246790
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import kr.touroot.global.exception.BadRequestException; | ||
import kr.touroot.travelogue.domain.search.CountryCode; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
|
@@ -24,6 +25,8 @@ | |
@Entity | ||
public class TravelogueCountry { | ||
|
||
private static final int MIN_COUNT = 1; | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
@@ -40,8 +43,27 @@ public class TravelogueCountry { | |
private Integer count; | ||
|
||
public TravelogueCountry(Travelogue travelogue, CountryCode countryCode, Integer count) { | ||
validate(travelogue, countryCode, count); | ||
this.travelogue = travelogue; | ||
this.countryCode = countryCode; | ||
this.count = count; | ||
} | ||
|
||
|
||
private void validate(Travelogue travelogue, CountryCode countryCode, Integer count) { | ||
validateNotNull(travelogue, countryCode, count); | ||
validateCount(count); | ||
} | ||
|
||
private void validateNotNull(Travelogue travelogue, CountryCode countryCode, Integer count) { | ||
if (travelogue == null || countryCode == null || count == null) { | ||
throw new BadRequestException("여행기와 국가 코드, 국가 코드의 count 는 null 일 수 없습니다."); | ||
} | ||
} | ||
|
||
private void validateCount(Integer count) { | ||
if (count < MIN_COUNT) { | ||
throw new BadRequestException(String.format("국가 코드의 개수는 %d 보다 커야합니다.", MIN_COUNT)); | ||
} | ||
} | ||
Comment on lines
+64
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분은 개발자가 직접 계산해서 넣어주는 값이기 때문에, 사용자의 잘못된 요청이 아닌 서버의 에러로 봐야 한다고 생각하는데요. 해당 부분은 이후에 같이 이야기해보면 좋을 것 같아요. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
package kr.touroot.travelogue.domain.search; | ||
|
||
import java.util.Arrays; | ||
import kr.touroot.global.exception.BadRequestException; | ||
|
||
public enum SearchType { | ||
TITLE, AUTHOR, COUNTRY; | ||
|
||
public static SearchType from(String searchType) { | ||
return Arrays.stream(SearchType.values()) | ||
.filter(type -> searchType.equalsIgnoreCase(type.name())) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 검색 키워드 종류입니다.")); | ||
try { | ||
return SearchType.valueOf(searchType.toUpperCase()); | ||
} catch (IllegalArgumentException exception) { | ||
throw new BadRequestException("존재하지 않는 검색 키워드 종류입니다."); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,30 +19,34 @@ public class TravelogueCountryService { | |
|
||
private final TravelogueCountryRepository travelogueCountryRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public List<TravelogueCountry> readCountryByTravelogue(Travelogue travelogue) { | ||
return travelogueCountryRepository.findAllByTravelogue(travelogue); | ||
} | ||
|
||
@Transactional | ||
public void createTravelogueCountries(Travelogue travelogue, TravelogueRequest request) { | ||
public List<TravelogueCountry> createTravelogueCountries(Travelogue travelogue, TravelogueRequest request) { | ||
Map<CountryCode, Long> countryCounts = countCountries(request); | ||
|
||
countryCounts.forEach((countryCode, count) -> travelogueCountryRepository.save( | ||
new TravelogueCountry(travelogue, countryCode, count.intValue()))); | ||
return countryCounts.entrySet().stream() | ||
.map(entry -> travelogueCountryRepository.save( | ||
new TravelogueCountry(travelogue, entry.getKey(), entry.getValue().intValue())) | ||
) | ||
.toList(); | ||
} | ||
|
||
private Map<CountryCode, Long> countCountries(TravelogueRequest request) { | ||
return request.days().stream() | ||
.flatMap(day -> day.places().stream()) | ||
.map(place -> CountryCode.valueOf(place.countryCode())) | ||
.filter(countryCode -> countryCode != CountryCode.NONE) | ||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); | ||
} | ||
Comment on lines
33
to
39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분은 일급 컬렉션으로 묶을까 하다, 두었는데 이에 대해 어떻게 생각하실까욥? |
||
|
||
@Transactional(readOnly = true) | ||
public List<TravelogueCountry> getTravelogueCountryByTravelogue(Travelogue travelogue) { | ||
return travelogueCountryRepository.findAllByTravelogue(travelogue); | ||
} | ||
|
||
@Transactional | ||
public void updateTravelogueCountries(Travelogue travelogue, TravelogueRequest request) { | ||
public List<TravelogueCountry> updateTravelogueCountries(Travelogue travelogue, TravelogueRequest request) { | ||
deleteAllByTravelogue(travelogue); | ||
createTravelogueCountries(travelogue, request); | ||
return createTravelogueCountries(travelogue, request); | ||
} | ||
|
||
@Transactional | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍