Skip to content

Commit a73ff8c

Browse files
authored
Merge pull request #132 from prgrms-web-devcourse-final-project/refactor
[REFACTOR]: users info service 코드 리팩토링
2 parents bcf0bc0 + 6e211e4 commit a73ff8c

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

back/src/main/java/com/back/domain/scenario/repository/ScenarioRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,17 @@ Page<Scenario> findByUserIdAndDecisionLineIsNotNullAndStatusOrderByCreatedDateDe
8282

8383
@Query("select s.id from Scenario s where s.baseLine.id = :baseLineId")
8484
List<Long> findIdsByBaseLine_Id(@Param("baseLineId") Long baseLineId);
85+
86+
@Modifying(clearAutomatically = true)
87+
@Query("""
88+
UPDATE Scenario s
89+
SET s.representative =
90+
CASE WHEN s.id = :scenarioId THEN true ELSE false END
91+
WHERE s.user.id = :userId
92+
AND (s.representative = true OR s.id = :scenarioId)
93+
""")
94+
void updateRepresentativeStatus(
95+
@Param("userId") Long userId,
96+
@Param("scenarioId") Long scenarioId
97+
);
8598
}

back/src/main/java/com/back/domain/user/controller/UserInfoController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public ResponseEntity<UserInfoResponse> getMyInfo(@AuthenticationPrincipal Custo
3838
@PostMapping("/users-info")
3939
public ResponseEntity<UserInfoResponse> createMyInfo(@AuthenticationPrincipal CustomUserDetails principal,
4040
@Valid @RequestBody UserInfoRequest req) {
41-
return ResponseEntity.ok(userInfoService.createMyInfo(principal.getId(), req));
41+
return ResponseEntity.ok(userInfoService.saveOrUpdateMyInfo(principal.getId(), req));
4242
}
4343

4444
// 사용자 정보 수정
4545
@PutMapping("/users-info")
4646
public ResponseEntity<UserInfoResponse> updateMyInfo(@AuthenticationPrincipal CustomUserDetails principal,
4747
@Valid @RequestBody UserInfoRequest req) {
48-
return ResponseEntity.ok(userInfoService.updateMyInfo(principal.getId(), req));
48+
return ResponseEntity.ok(userInfoService.saveOrUpdateMyInfo(principal.getId(), req));
4949
}
5050

5151
// 내가 만든 시나리오 목록 조회 (평행우주 목록)

back/src/main/java/com/back/domain/user/service/GuestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public User createAndSaveGuest(){
2727
User guest = User.builder()
2828
.email(guestEmail)
2929
.username(guestLoginId)
30-
.password(null) // 게스트 비밀번호 없음(추후 전환 시 설정)
30+
.password(null)
3131
.nickname("게스트_" + UUID.randomUUID().toString().substring(0, 4))
3232
.birthdayAt(LocalDateTime.now())
3333
.role(Role.GUEST)

back/src/main/java/com/back/domain/user/service/UserInfoService.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,7 @@ public UserInfoResponse getMyInfo(Long userId) {
4242
}
4343

4444
@Transactional
45-
public UserInfoResponse createMyInfo(Long userId, UserInfoRequest req) {
46-
User user = userRepository.findById(userId)
47-
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
48-
49-
applyPatch(user, req);
50-
51-
return UserInfoResponse.from(user);
52-
}
53-
54-
@Transactional
55-
public UserInfoResponse updateMyInfo(Long userId, UserInfoRequest req) {
45+
public UserInfoResponse saveOrUpdateMyInfo(Long userId, UserInfoRequest req) {
5646
User user = userRepository.findById(userId)
5747
.orElseThrow(() -> new EntityNotFoundException("User not found: " + userId));
5848

@@ -85,11 +75,12 @@ public PageResponse<UserScenarioListResponse> getMyScenarios(Long userId, Pageab
8575
// 시나리오 id 목록 추출
8676
List<Long> scenarioIds = scenarioPage.getContent().stream()
8777
.map(Scenario::getId)
88-
.collect(Collectors.toList());
78+
.toList();
8979

9080
// SceneType 조회, 시나리오 id별로 그룹화
91-
List<SceneType> sceneTypes = sceneTypeRepository.findByScenarioIdIn(scenarioIds);
92-
Map<Long, List<SceneType>> sceneTypeMap = sceneTypes.stream()
81+
Map<Long, List<SceneType>> sceneTypeMap = scenarioIds.isEmpty()
82+
? Map.of()
83+
: sceneTypeRepository.findByScenarioIdIn(scenarioIds).stream()
9384
.collect(Collectors.groupingBy(st -> st.getScenario().getId()));
9485

9586
Page<UserScenarioListResponse> responsePage = scenarioPage.map(scenario ->
@@ -134,12 +125,7 @@ public void setProfileScenario(Long userId, Long scenarioId) {
134125
throw new IllegalArgumentException("Scenario does not belong to user");
135126
}
136127

137-
// 기존 대표 시나리오를 false로 변경
138-
scenarioRepository.findByUserIdAndRepresentativeTrue(userId)
139-
.ifPresent(existingRepresentative -> existingRepresentative.setRepresentative(false));
140-
141-
// 새로운 시나리오를 대표로 설정
142-
scenario.setRepresentative(true);
128+
scenarioRepository.updateRepresentativeStatus(userId, scenarioId);
143129
}
144130

145131
public UserProfileResponse getMyProfile(Long userId) {
@@ -157,11 +143,11 @@ public UserProfileResponse getMyProfile(Long userId) {
157143
}
158144

159145
private void applyPatch(User user, UserInfoRequest req) {
160-
if (req.username() != null) user.setUsername(req.username());
146+
if (req.username() != null) user.setUsername(req.username().trim());
161147
if (req.birthdayAt() != null) user.setBirthdayAt(req.birthdayAt());
162148
if (req.gender() != null) user.setGender(req.gender());
163149
if (req.mbti() != null) user.setMbti(req.mbti());
164-
if (req.beliefs() != null) user.setBeliefs(req.beliefs());
150+
if (req.beliefs() != null) user.setBeliefs(req.beliefs().trim());
165151
if (req.lifeSatis() != null) user.setLifeSatis(req.lifeSatis());
166152
if (req.relationship() != null) user.setRelationship(req.relationship());
167153
if (req.workLifeBal() != null) user.setWorkLifeBal(req.workLifeBal());

0 commit comments

Comments
 (0)