Skip to content

Commit ab8d056

Browse files
committed
refactor: 제안 취소 이후에 제안 다시 가능하도록 수정
1 parent 6c44fee commit ab8d056

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

src/main/java/swyp/dodream/domain/suggestion/controller/SuggestionController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import swyp.dodream.domain.suggestion.service.SuggestionService;
2020
import swyp.dodream.jwt.dto.UserPrincipal;
2121

22+
import java.util.Map;
23+
2224
@RestController
2325
@RequestMapping("/api/my/suggestions")
2426
@RequiredArgsConstructor
@@ -97,15 +99,15 @@ public ResponseEntity<Void> cancelSuggestion(
9799
@ApiResponse(responseCode = "200", description = "조회 성공")
98100
})
99101
@GetMapping("/{postId}/suggestions/status")
100-
public ResponseEntity<java.util.Map<String, Object>> getSuggestionStatus(
102+
public ResponseEntity<Map<String, Object>> getSuggestionStatus(
101103
Authentication authentication,
102104
@PathVariable Long postId,
103105
@RequestParam Long toUserId
104106
) {
105107
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
106108
boolean exists = suggestionService.hasActiveSuggestion(userPrincipal.getUserId(), postId, toUserId);
107109
Long suggestionId = suggestionService.getActiveSuggestionId(postId, toUserId).orElse(null);
108-
java.util.Map<String, Object> body = new java.util.LinkedHashMap<>();
110+
Map<String, Object> body = new java.util.LinkedHashMap<>();
109111
body.put("suggested", exists);
110112
body.put("suggestionId", suggestionId);
111113
return ResponseEntity.ok(body);

src/main/java/swyp/dodream/domain/suggestion/domain/Suggestion.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,18 @@ public void markAsAccepted() {
6666
public void markAsRejected() {
6767
this.status = SuggestionStatus.REJECTED;
6868
}
69+
70+
public void resend(String suggestionMessage) {
71+
// 다시 보낼 수 있는 상태만 허용(CANCELED, REJECTED)
72+
if (this.status != SuggestionStatus.CANCELED && this.status != SuggestionStatus.REJECTED) {
73+
throw new IllegalStateException("현재 상태에서는 제안을 다시 보낼 수 없습니다.");
74+
}
75+
76+
this.status = SuggestionStatus.SENT;
77+
this.withdrawnAt = null;
78+
79+
if (suggestionMessage != null && !suggestionMessage.isBlank()) {
80+
this.suggestionMessage = suggestionMessage;
81+
}
82+
}
6983
}

src/main/java/swyp/dodream/domain/suggestion/repository/SuggestionRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package swyp.dodream.domain.suggestion.repository;
22

3+
import jakarta.validation.constraints.NotNull;
34
import org.springframework.data.domain.Page;
45
import org.springframework.data.domain.Pageable;
56
import org.springframework.data.domain.Slice;
@@ -118,4 +119,6 @@ Optional<Suggestion> findActiveByPostIdAndToUserId(@Param("postId") Long postId,
118119
AND s.status IN :validStatuses
119120
""")
120121
Optional<Long> findValidSuggestionId(@Param("postId") Long postId, @Param("userId") Long userId, @Param("validStatuses") List<SuggestionStatus> validStatuses);
122+
123+
Optional<Suggestion> findByPostIdAndToUserId(Long postId, Long toUserId);
121124
}

src/main/java/swyp/dodream/domain/suggestion/service/SuggestionService.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,26 @@ public SuggestionResponse createSuggestion(Long fromUserId, Long postId, Suggest
6363
User toUser = userRepository.findById(request.toUserId())
6464
.orElseThrow(() -> new CustomException(ExceptionType.NOT_FOUND, "제안 대상 회원을 찾을 수 없습니다."));
6565

66-
// 5. 제안 생성
67-
Suggestion suggestion = new Suggestion(
68-
snowflakeIdService.generateId(),
69-
request.suggestionMessage(),
70-
post,
71-
post.getOwner(), // fromUser
72-
toUser,
73-
LocalDateTime.now()
74-
);
66+
// 5. 기존 제안 있는지 조회 (상태 무관: CANCELED, REJECTED 포함)
67+
java.util.Optional<Suggestion> existingOpt =
68+
suggestionRepository.findByPostIdAndToUserId(postId, request.toUserId());
69+
70+
Suggestion suggestion;
71+
72+
if (existingOpt.isPresent()) {
73+
suggestion = existingOpt.get();
74+
suggestion.resend(request.suggestionMessage());
75+
} else {
76+
suggestion = new Suggestion(
77+
snowflakeIdService.generateId(),
78+
request.suggestionMessage(),
79+
post,
80+
post.getOwner(),
81+
toUser,
82+
LocalDateTime.now()
83+
);
84+
suggestionRepository.save(suggestion);
85+
}
7586

7687
suggestionRepository.save(suggestion);
7788

0 commit comments

Comments
 (0)