Skip to content

Commit

Permalink
Merge pull request #130 from sh1220/dev
Browse files Browse the repository at this point in the history
feat: debate delete
  • Loading branch information
sh1220 authored Aug 18, 2024
2 parents 591d0a2 + d192128 commit 1c8e918
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public enum BaseExceptionResponseStatus implements ResponseStatus {
COMMENT_NOT_FOUND(6005,HttpStatus.BAD_REQUEST.value(), "해당 댓글이 존재하지 않습니다."),
KEYWORD_NOT_FOUND(6006,HttpStatus.BAD_REQUEST.value(), "해당 키워드가 존재하지 않습니다."),
INVALID_GET_DEBATE_VALUE(6007,HttpStatus.BAD_REQUEST.value(), "토론 상세 정보 요청에서 잘못된 값이 존재합니다."),
INVALID_USER_ID(6008,HttpStatus.BAD_REQUEST.value(), "다른 유저의 토론을 지울 수 없습니다."),
DELETED_DEBATE(6009,HttpStatus.BAD_REQUEST.value(), "이미 삭제된 토론입니다."),



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,11 @@ public BaseResponse<List<DebateByKeywordDTO>> getRecentViewedDebate(@RequestHead

return new BaseResponse<>(debateResponse);
}


@DeleteMapping("/{debateId}")
public BaseResponse<?> deleteDebate(@PreAuthorize long userId, @PathVariable Long debateId){
debateService.deleteDebate(debateId,userId);
return new BaseResponse<>(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static class MyDebate {
private String title;
private String keyword;
private String duration;
private Long debateId;
private Long hits;
private Long comments;
}
Expand All @@ -46,6 +47,7 @@ public static class InvolvedDebate {
private String title;
private String keyword;
private String duration;
private Long debateId;
private Long hits;
private Long comments;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store.itpick.backend.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.data.repository.query.Param;
Expand Down Expand Up @@ -31,4 +32,11 @@ public interface DebateRepository extends JpaRepository<Debate, Long> {
"WHERE uvc.user = :user")
List<Debate> findDebatesByUserVoteChoice(@Param("user") User user);

@Modifying
@Query("UPDATE Debate d SET d.status = 'deleted' WHERE d.debateId = :debate_id")
void softDeleteById(@Param("debate_id") Long debate_id);

Optional<Debate> getDebateByDebateId(Long debateId);


}
41 changes: 34 additions & 7 deletions src/main/java/store/itpick/backend/service/DebateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public GetDebateResponse getDebate(Long debateId, String token) {
Debate debate = debateRepository.findById(debateId)
.orElseThrow(() -> new DebateException(DEBATE_NOT_FOUND));

if(debate.getStatus().equals("deleted")) throw new DebateException(DEBATE_NOT_FOUND);


// 최근 본 토론 기록 생성 및 저장
saveRecentViewedDebate(userId, debate);
Expand Down Expand Up @@ -220,7 +222,7 @@ public GetDebateResponse getDebate(Long debateId, String token) {
.userVoteOptionText(userVoteOptionText)
.build();
}

@Transactional
public List<DebateByKeywordDTO> GetDebatesByKeyword(Long keywordID, String sort){
List<Debate> debates=null;
Expand All @@ -233,12 +235,14 @@ public List<DebateByKeywordDTO> GetDebatesByKeyword(Long keywordID, String sort)
List<DebateByKeywordDTO> debateList = new ArrayList<>();

for (Debate debate : debates) {
String title= debate.getTitle();
String content =debate.getContent();
String mediaUrl =debate.getImageUrl();
Long hit = debate.getHits();
Long comment = (long) debate.getComment().size();
debateList.add(new DebateByKeywordDTO(title,content,mediaUrl,hit,comment));
if(debate.getStatus().equals("active")){
String title= debate.getTitle();
String content =debate.getContent();
String mediaUrl =debate.getImageUrl();
Long hit = debate.getHits();
Long comment = (long) debate.getComment().size();
debateList.add(new DebateByKeywordDTO(title,content,mediaUrl,hit,comment));
}
}

return debateList;
Expand Down Expand Up @@ -274,9 +278,32 @@ public List<DebateByKeywordDTO> getRecentViewedDebate(String token){

// Debate를 DTO로 변환
return debates.stream()
.filter(debate -> "active".equals(debate.getStatus()))
.map(debate -> new DebateByKeywordDTO(debate.getTitle(), debate.getContent(), debate.getImageUrl(),debate.getHits(), (long) debate.getComment().size()))
.collect(Collectors.toList());

}

@Transactional
public void deleteDebate(Long debateId, long userId) {
Optional<Debate> debate = debateRepository.getDebateByDebateId(debateId);

if (debate.isEmpty()) {
throw new DebateException(DEBATE_NOT_FOUND);
}

if (!debate.get().getUser().getUserId().equals(userId)) {
throw new DebateException(INVALID_USER_ID);
}

if(debate.get().getStatus().equals("deleted")){
throw new DebateException(DELETED_DEBATE);
}


debateRepository.softDeleteById(debateId);


}

}
4 changes: 4 additions & 0 deletions src/main/java/store/itpick/backend/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public List<GetMyPageResponse.MyDebate> getMyDebate(long userId) {

// 최근 순으로 정렬
List<Debate> sortedDebates = myDebateList.stream()
.filter(debate -> "active".equals(debate.getStatus()))
.sorted((d1, d2) -> d2.getCreateAt().compareTo(d1.getCreateAt()))
.toList();

Expand All @@ -205,6 +206,7 @@ public List<GetMyPageResponse.MyDebate> getMyDebate(long userId) {
.keyword(myDebate.getKeyword().getKeyword())
.duration(getTimeAgo(myDebate.getCreateAt()))
.hits(myDebate.getHits())
.debateId(myDebate.getDebateId())
.comments(debateRepository.countCommentsByDebate(myDebate))
.build()
);
Expand All @@ -226,6 +228,7 @@ public List<GetMyPageResponse.InvolvedDebate> getInvolvedDebate(long userId) {

// 최근 순으로 정렬
List<Debate> sortedDebates = allDebates.stream()
.filter(debate -> "active".equals(debate.getStatus()))
.sorted((d1, d2) -> d2.getCreateAt().compareTo(d1.getCreateAt()))
.toList();

Expand All @@ -236,6 +239,7 @@ public List<GetMyPageResponse.InvolvedDebate> getInvolvedDebate(long userId) {
.keyword(involvedDebate.getKeyword().getKeyword())
.duration(getTimeAgo(involvedDebate.getCreateAt()))
.hits(involvedDebate.getHits())
.debateId(involvedDebate.getDebateId())
.comments(debateRepository.countCommentsByDebate(involvedDebate))
.build()
);
Expand Down

0 comments on commit 1c8e918

Please sign in to comment.