Skip to content

Commit

Permalink
Merge pull request #44 from Parkjyun/feat/43
Browse files Browse the repository at this point in the history
[feat] 토론 참여 수정, 삭제 api 생성
  • Loading branch information
Parkjyun authored May 12, 2024
2 parents 7a516e2 + 7298f54 commit 0826839
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ dependencies {

// Database
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'

runtimeOnly("com.mysql:mysql-connector-j")
// Log
implementation 'org.springframework.boot:spring-boot-starter-log4j2'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public enum DebateParticipationFailureCode implements FailureCode {
*/
NOT_PARTICIPATED_DEBATE(HttpStatus.NOT_FOUND,"투표를 먼저 하셔야 합니다"),
NOT_FOUND_DEBATE(HttpStatus.NOT_FOUND, "토론이 없습니다"),
NOT_FOUND_DEBATEPARTICIPATION(HttpStatus.NOT_FOUND, "토론에 투표하지 않으셨습니다"),
/**
* 409 Conflict
*/
ALREADY_COMMENTED_DEBATE(HttpStatus.CONFLICT,"이미 작성한 댓글입니다");

ALREADY_COMMENTED_DEBATE(HttpStatus.CONFLICT,"이미 작성한 댓글입니다"),
NOT_MY_DEBATE_PARTICIPATION(HttpStatus.CONFLICT, "해당 멤버가 작성한 댓글이 아닙니다");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public enum DebateParticipationSuccessCode implements SuccessCode{
/**
* 200 OK
**/
DEBATE_PARTICIPATION_SUCCESS(HttpStatus.OK, "토론 댓글 작성 성공");
DEBATE_PARTICIPATION_SUCCESS(HttpStatus.OK, "토론 댓글 작성 성공"),
DEBATE_PARTICIPATION_PATCH_SUCCESS(HttpStatus.OK, "토론 댓글 수정 성공"),
DEBATE_PARTICIPATION_DELETE_SUCCESS(HttpStatus.OK, "토론 침여 삭제 성공");

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,55 @@
import java.util.List;

@RestController
@RequestMapping("/v1/debates")
@RequestMapping("/v1")
@RequiredArgsConstructor
public class DebateController {

private final DebateService debateService;
private final DebateParticipationService debateParticipationService;

@GetMapping
@GetMapping("/debates")
public NewSnackResponse<List<DebateMainPageResponse>> getDebates() {
return NewSnackResponse.success(DebateSuccessCode.GET_DEBATES_SUCCESS, debateService.getDebates());
}

@GetMapping("/main")
@GetMapping("/debates/main")
public NewSnackResponse<DebateMainPageResponse> getMainDebate() {
return NewSnackResponse.success(DebateSuccessCode.GET_DEBATES_SUCCESS, debateService.getMainDebate());
}

@GetMapping("/{debateId}")
@GetMapping("/debates/{debateId}")
public NewSnackResponse<DebateIndividualResponse> getDebate(@PathVariable Long debateId, @MemberId(isForSecuredApi = false) Long memberId) {
return NewSnackResponse.success(DebateSuccessCode.GET_DEBATES_SUCCESS, debateService.getDebate(debateId, memberId));
}

@PostMapping("/{debateId}/votes")
@PostMapping("/debates/{debateId}/votes")
public NewSnackResponse<?> voteDebate(@PathVariable Long debateId, @MemberId Long memberId, @RequestBody DebateVoteRequest debateVoteRequest) {
debateService.voteDebate(debateId, memberId, debateVoteRequest.vote());
return NewSnackResponse.success(DebateSuccessCode.DEBATE_VOTE_SUCCESS);
}

@PostMapping("/{debateId}/comments")
@PostMapping("/debates/{debateId}/comments")
public NewSnackResponse<?> createDebateComment(@PathVariable Long debateId, @MemberId Long memberId, @RequestBody @Valid DebateParticipationRequest request) {
debateParticipationService.participateDebate(debateId, memberId, request.content());
return NewSnackResponse.success(DebateParticipationSuccessCode.DEBATE_PARTICIPATION_SUCCESS);
}

@GetMapping("/{debateId}/comments")
@GetMapping("/debates/{debateId}/comments")
public NewSnackResponse<List<DebateCommentResponse>> getDebateComments(@PathVariable Long debateId, @MemberId(isForSecuredApi = false) Long memberId,
@RequestParam SearchOrder order) {
return NewSnackResponse.success(DebateSuccessCode.GET_DEBATES_SUCCESS, debateParticipationService.getDebateComments(debateId, memberId, order));
}

@PatchMapping("/debate-participations/{debateParticipationId}")
public NewSnackResponse<?> updateDebateComment(@PathVariable Long debateParticipationId, @MemberId Long memberId, @RequestBody @Valid DebateParticipationRequest request) {
debateParticipationService.updateDebateComment(debateParticipationId, memberId, request.content());
return NewSnackResponse.success(DebateParticipationSuccessCode.DEBATE_PARTICIPATION_PATCH_SUCCESS);
}

@DeleteMapping("/debate-participations/{debateParticipationId}")
public NewSnackResponse<?> deleteDebateComment(@PathVariable Long debateParticipationId, @MemberId Long memberId) {
debateParticipationService.deleteDebateComment(debateParticipationId, memberId);
return NewSnackResponse.success(DebateParticipationSuccessCode.DEBATE_PARTICIPATION_DELETE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,33 @@ public List<DebateCommentResponse> getDebateComments(Long debateId, Long memberI
return debateParticipationJpaRepository.findAllWithMemberAndDebateParticipationHeartByDebateIdOrderByHeartCountDescJPQL(debateId)
.stream().map(debateParticipation -> DebateCommentResponse.of(debateParticipation, isLikedByMe(debateParticipation, memberId), isMyDebateParticipation(debateParticipation, memberId))).toList();
}

@Transactional
public void updateDebateComment(Long debateParticipationId, Long memberId, String content) {
DebateParticipation debateParticipation = debateParticipationJpaRepository.findById(debateParticipationId)
.orElseThrow(() -> new DebateParticipationException(DebateParticipationFailureCode.NOT_FOUND_DEBATEPARTICIPATION));
if (!isMyDebateParticipation(debateParticipation, memberId)) {
throw new DebateParticipationException(DebateParticipationFailureCode.NOT_MY_DEBATE_PARTICIPATION);
}
debateParticipation.updateComment(content);
}

@Transactional
public void deleteDebateComment(Long debateParticipationId, Long memberId) {
DebateParticipation debateParticipation = debateParticipationJpaRepository.findById(debateParticipationId)
.orElseThrow(() -> new DebateParticipationException(DebateParticipationFailureCode.NOT_FOUND_DEBATEPARTICIPATION));
if (!isMyDebateParticipation(debateParticipation, memberId)) {
throw new DebateParticipationException(DebateParticipationFailureCode.NOT_MY_DEBATE_PARTICIPATION);
}
debateParticipationJpaRepository.delete(debateParticipation);
}


private boolean isLikedByMe(DebateParticipation debateParticipation, Long memberId) {
return debateParticipation.getDebateParticipationHearts().stream().anyMatch(commentHeart -> commentHeart.getMember().getId().equals(memberId));
}

private boolean isMyDebateParticipation(DebateParticipation debateParticipation, Long memberId) {
return debateParticipation.getMember().getId().equals(memberId);
}

}

0 comments on commit 0826839

Please sign in to comment.