-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from Parkjyun/feat/31
[feat] 토론 투표, 토론 댓글 관련 api 생성
- Loading branch information
Showing
16 changed files
with
261 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...a/com/newsnack/www/newsnackserver/common/code/failure/DebateParticipationFailureCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.newsnack.www.newsnackserver.common.code.failure; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum DebateParticipationFailureCode implements FailureCode { | ||
/** | ||
* 404 Not Found | ||
*/ | ||
NOT_PARTICIPATED_DEBATE(HttpStatus.NOT_FOUND,"투표를 먼저 하셔야 합니다"), | ||
NOT_FOUND_DEBATE(HttpStatus.NOT_FOUND, "토론이 없습니다"), | ||
/** | ||
* 409 Conflict | ||
*/ | ||
ALREADY_COMMENTED_DEBATE(HttpStatus.CONFLICT,"이미 작성한 댓글입니다"); | ||
|
||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
17 changes: 17 additions & 0 deletions
17
...a/com/newsnack/www/newsnackserver/common/code/success/DebateParticipationSuccessCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.newsnack.www.newsnackserver.common.code.success; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum DebateParticipationSuccessCode implements SuccessCode{ | ||
/** | ||
* 200 OK | ||
**/ | ||
DEBATE_PARTICIPATION_SUCCESS(HttpStatus.OK, "토론 댓글 작성 성공"); | ||
|
||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...n/java/com/newsnack/www/newsnackserver/common/exception/DebateParticipationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.newsnack.www.newsnackserver.common.exception; | ||
|
||
import com.newsnack.www.newsnackserver.common.code.failure.FailureCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class DebateParticipationException extends RuntimeException{ | ||
private final FailureCode failureCode; | ||
public DebateParticipationException(FailureCode failureCode) { | ||
super("[DebateParticipationException] : " + failureCode.getMessage()); | ||
this.failureCode = failureCode; | ||
} | ||
|
||
public int getHttpStatusCode() { | ||
return failureCode.getHttpStatus().value(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
...ewsnackserver/domain/debateparticipation/repository/DebateParticipationJpaRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
package com.newsnack.www.newsnackserver.domain.debateparticipation.repository; | ||
|
||
import com.newsnack.www.newsnackserver.domain.debate.model.Debate; | ||
import com.newsnack.www.newsnackserver.domain.debateparticipation.model.DebateParticipation; | ||
import com.newsnack.www.newsnackserver.domain.member.model.Member; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface DebateParticipationJpaRepository extends JpaRepository<DebateParticipation, Long> { | ||
Optional<DebateParticipation> findByDebateIdAndMemberId(Long debateId, Long memberId); | ||
Optional<DebateParticipation> findByDebateAndMember(Debate debate, Member member); | ||
|
||
@Query("select dp from DebateParticipation dp join fetch dp.member where dp.debate.id = :debateId order by dp.id desc") | ||
List<DebateParticipation> findAllWithMemberByDebateIdOrderByIdDescJPQL(Long debateId); | ||
|
||
@Query("select dp from DebateParticipation dp join fetch dp.member where dp.debate.id = :debateId order by dp.heartCount desc, dp.id desc") | ||
List<DebateParticipation> findAllWithMemberByDebateIdOrderByHeartCountDescJPQL(Long debateId); | ||
|
||
@Query("select distinct dp from DebateParticipation dp left join fetch dp.debateParticipationHearts join fetch dp.member where dp.debate.id = :debateId order by dp.id desc") | ||
List<DebateParticipation> findAllWithMemberAndDebateParticipationHeartByDebateIdOrderByIdDescJPQL(Long debateId); | ||
@Query("select distinct dp from DebateParticipation dp left join fetch dp.debateParticipationHearts join fetch dp.member where dp.debate.id = :debateId order by dp.heartCount desc, dp.id desc") | ||
List<DebateParticipation> findAllWithMemberAndDebateParticipationHeartByDebateIdOrderByHeartCountDescJPQL(Long debateId); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/newsnack/www/newsnackserver/dto/request/DebateParticipationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.newsnack.www.newsnackserver.dto.request; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record DebateParticipationRequest(@Size(min = 1, max = 200) @NotBlank String content) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/newsnack/www/newsnackserver/dto/request/DebateVoteRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.newsnack.www.newsnackserver.dto.request; | ||
|
||
public record DebateVoteRequest(boolean vote) { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/newsnack/www/newsnackserver/dto/response/DebateCommentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.newsnack.www.newsnackserver.dto.response; | ||
|
||
import com.newsnack.www.newsnackserver.domain.debateparticipation.model.DebateParticipation; | ||
|
||
public record DebateCommentResponse(Long id, String writerName, long memberId, boolean vote, String content, String createdAt, int likeCount, boolean isLikedByMe, boolean isMyComment) { | ||
public static DebateCommentResponse of(DebateParticipation debateParticipation, boolean isLikedByMe, boolean isMyComment) { | ||
return new DebateCommentResponse(debateParticipation.getId(), debateParticipation.getMember().getName(), debateParticipation.getMember().getId(), | ||
debateParticipation.getVote(), debateParticipation.getComment(), | ||
debateParticipation.getCreatedAt().toString(), debateParticipation.getHeartCount(), isLikedByMe, isMyComment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/newsnack/www/newsnackserver/service/DebateParticipationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.newsnack.www.newsnackserver.service; | ||
|
||
import com.newsnack.www.newsnackserver.common.code.failure.DebateParticipationFailureCode; | ||
import com.newsnack.www.newsnackserver.common.exception.DebateParticipationException; | ||
import com.newsnack.www.newsnackserver.controller.parameter.SearchOrder; | ||
import com.newsnack.www.newsnackserver.domain.debate.model.Debate; | ||
import com.newsnack.www.newsnackserver.domain.debate.repository.DebateJpaRepository; | ||
import com.newsnack.www.newsnackserver.domain.debateparticipation.model.DebateParticipation; | ||
import com.newsnack.www.newsnackserver.domain.debateparticipation.repository.DebateParticipationJpaRepository; | ||
import com.newsnack.www.newsnackserver.domain.member.model.Member; | ||
import com.newsnack.www.newsnackserver.domain.member.repository.MemberJpaRepository; | ||
import com.newsnack.www.newsnackserver.dto.response.DebateCommentResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
|
||
@Transactional(readOnly = true) | ||
@Service | ||
@RequiredArgsConstructor | ||
public class DebateParticipationService { | ||
private final DebateParticipationJpaRepository debateParticipationJpaRepository; | ||
private final DebateJpaRepository debateJpaRepository; | ||
private final MemberJpaRepository memberJpaRepository; | ||
|
||
@Transactional | ||
public void participateDebate(Long debateId, Long memberId, String content) { | ||
Debate debate = debateJpaRepository.getReferenceById(debateId); | ||
Member member = memberJpaRepository.getReferenceById(memberId); | ||
|
||
DebateParticipation debateParticipation = debateParticipationJpaRepository.findByDebateAndMember(debate, member) | ||
.orElseThrow(() -> new DebateParticipationException(DebateParticipationFailureCode.NOT_PARTICIPATED_DEBATE)); | ||
if (debateParticipation.getComment() != null) { | ||
throw new DebateParticipationException(DebateParticipationFailureCode.ALREADY_COMMENTED_DEBATE); | ||
} | ||
debateParticipation.updateComment(content); | ||
} | ||
|
||
public List<DebateCommentResponse> getDebateComments(Long debateId, Long memberId, SearchOrder searchOrder) { | ||
Debate debate = debateJpaRepository.findById(debateId).orElseThrow(() -> new DebateParticipationException(DebateParticipationFailureCode.NOT_FOUND_DEBATE)); | ||
if(memberId == null) { // 비회원일 경우 | ||
if (searchOrder.getValue().equals(SearchOrder.RECENT.getValue()))// 최신순 정렬 | ||
return debateParticipationJpaRepository.findAllWithMemberByDebateIdOrderByIdDescJPQL(debateId) | ||
.stream().map(debateParticipation -> DebateCommentResponse.of(debateParticipation, false, false)).toList(); | ||
if (searchOrder.getValue().equals(SearchOrder.POPULAR.getValue())) // 인기순 정렬 | ||
return debateParticipationJpaRepository.findAllWithMemberByDebateIdOrderByHeartCountDescJPQL(debateId) | ||
.stream().map(debateParticipation -> DebateCommentResponse.of(debateParticipation, false, false)).toList(); | ||
} | ||
//회원일 경우 1000 -> boolean 값 4개 변경 해야함 | ||
if (searchOrder.getValue().equals(SearchOrder.RECENT.getValue())) {// 최신순 정렬 | ||
return debateParticipationJpaRepository.findAllWithMemberAndDebateParticipationHeartByDebateIdOrderByIdDescJPQL(debateId) | ||
.stream().map(debateParticipation -> DebateCommentResponse.of(debateParticipation, isLikedByMe(debateParticipation, memberId), isMyDebateParticipation(debateParticipation, memberId))).toList(); | ||
} | ||
//인기순 | ||
return debateParticipationJpaRepository.findAllWithMemberAndDebateParticipationHeartByDebateIdOrderByHeartCountDescJPQL(debateId) | ||
.stream().map(debateParticipation -> DebateCommentResponse.of(debateParticipation, isLikedByMe(debateParticipation, memberId), isMyDebateParticipation(debateParticipation, memberId))).toList(); | ||
} | ||
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); | ||
} | ||
|
||
} |
Oops, something went wrong.