Skip to content

Commit

Permalink
Merge pull request #34 from hook-killer/feat/28-reply
Browse files Browse the repository at this point in the history
[FEATURE] reply 기능 구현 완료
  • Loading branch information
donsonioc2010 authored Oct 15, 2023
2 parents b709ac9 + be6f9fa commit 5869220
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package HookKiller.server.board.controller;

import HookKiller.server.board.dto.ReplyResponseDto;
import HookKiller.server.board.service.ReplyService;
import HookKiller.server.common.type.LanguageType;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/reply")
@RequiredArgsConstructor
public class ReplyController {

private final ReplyService replyService;

@PostMapping
public void createReply(@RequestBody ReplyResponseDto responseDto) {
replyService.createReply(responseDto);
}

@GetMapping("/{articleId}")
public List<ReplyResponseDto> getReplyList(@PathVariable Long articleId, HttpServletRequest request) {
return replyService.getReplyList(articleId, LanguageType.findTypeByRequest(request));
}

@DeleteMapping("/{replyId}")
public ResponseEntity<String> deleteReply(@PathVariable Long replyId) {
replyService.deleteReply(replyId);
return ResponseEntity.ok("삭제처리가 완료되었습니다.");
}

}
38 changes: 38 additions & 0 deletions src/main/java/HookKiller/server/board/dto/ReplyResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package HookKiller.server.board.dto;


import HookKiller.server.board.entity.Reply;
import HookKiller.server.board.entity.ReplyContent;
import HookKiller.server.common.AbstractTimeStamp;
import HookKiller.server.common.type.LanguageType;
import HookKiller.server.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReplyResponseDto extends AbstractTimeStamp {

private Long articleId;

private Long replyId;

private LanguageType orgReplyLanguage;

private User createUser;

private String content;

public static ReplyResponseDto of(Reply reply, ReplyContent replyContent) {
return ReplyResponseDto.builder()
.replyId(reply.getId())
.createUser(reply.getCreatedUser())
.content(replyContent.getContent())
.build();
}

}
51 changes: 43 additions & 8 deletions src/main/java/HookKiller/server/board/entity/Reply.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package HookKiller.server.board.entity;

import HookKiller.server.board.type.ReplyStatus;
import HookKiller.server.common.AbstractTimeStamp;
import HookKiller.server.common.type.LanguageType;
import HookKiller.server.user.entity.User;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -10,7 +15,10 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -32,20 +40,47 @@
@Getter
@Table(name = "tbl_reply")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Reply extends AbstractTimeStamp {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToMany(mappedBy = "reply", fetch = FetchType.EAGER)
private List<ReplyContent> replyContent;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="article_id")
private Article article;

@OneToMany(mappedBy = "reply")
private List<ReplyContent> replyContent;
@NotNull
private LanguageType orgReplyLanguage;

@NotNull
@Enumerated(EnumType.STRING)
private ReplyStatus replyStatus;

@NotNull
@ManyToOne(fetch = FetchType.EAGER)
private User createdUser;

@NotNull
@ManyToOne(fetch = FetchType.EAGER)
private User updatedUser;

@Builder
public Reply(Long id, LanguageType orgReplyLanguage, ReplyStatus replyStatus,
User createdUser, User updatedUser) {
this.id = id;
this.orgReplyLanguage = orgReplyLanguage;
this.replyStatus = replyStatus;
this.createdUser = createdUser;
this.updatedUser = updatedUser;
}

public void updateStatus(ReplyStatus status) {
replyStatus = status;
}

private String orgReplyLanguage;
private boolean isDeleted;
private Long createdUserId;
private Long updatedUserId;
}
10 changes: 9 additions & 1 deletion src/main/java/HookKiller/server/board/entity/ReplyContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -28,7 +29,7 @@
public class ReplyContent {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="reply_id")
Expand All @@ -41,5 +42,12 @@ public class ReplyContent {
@Lob
private String content;

@Builder
public ReplyContent(Long id, Reply reply, LanguageType language, String content) {
this.id = id;
this.reply = reply;
this.language = language;
this.content = content;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
public enum BoardException implements BaseErrorCode {

BOARD_NOT_FOUND_ERROR(NOT_FOUND.value(), "Board_404_1", "해당 게시판을 찾을 수 없습니다."),
ARTICLE_CONTENT_NOT_FOUND_ERROR(NOT_FOUND.value(), "ArticleContent_404_1", "해당 게시글을 찾을 수 없습니다.");
ARTICLE_CONTENT_NOT_FOUND_ERROR(NOT_FOUND.value(), "ArticleContent_404_1", "해당 게시글을 찾을 수 없습니다."),
REPLY_CONTENT_NOT_FOUND_ERROR(NOT_FOUND.value(), "ReplyContent_404_1", "해당 댓글을 찾을 수 없습니다.");

private final Integer statusCode;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package HookKiller.server.board.exception;

import HookKiller.server.common.exception.BaseException;

public class ReplyContentNotFoundException extends BaseException {

public static final BaseException EXCEPTION = new ReplyContentNotFoundException();

private ReplyContentNotFoundException() {
super(BoardException.REPLY_CONTENT_NOT_FOUND_ERROR);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;


public interface ArticleRepository extends JpaRepository<Article, Long> {

List<Article> findAllByBoardAndArticleStatus(Board board, ArticleStatus status);


Optional<Article> findByIdAndArticleStatus(Long id, ArticleStatus status);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package HookKiller.server.board.repository;

import HookKiller.server.board.entity.Reply;
import HookKiller.server.board.entity.ReplyContent;
import HookKiller.server.common.type.LanguageType;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ReplyContentRepository extends JpaRepository<ReplyContent, Long> {

Optional<ReplyContent> findByReplyAndLanguage(Reply reply, LanguageType language);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package HookKiller.server.board.repository;

import HookKiller.server.board.entity.Article;
import HookKiller.server.board.entity.Reply;
import HookKiller.server.board.type.ReplyStatus;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ReplyRepository extends JpaRepository<Reply, Long> {
List<Reply> findAllByArticleAndReplyStatus(Article article, ReplyStatus replyStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import HookKiller.server.board.entity.Article;
import HookKiller.server.board.entity.ArticleContent;
import HookKiller.server.board.repository.ArticleContentRepository;
import HookKiller.server.common.service.TranslateService;
import HookKiller.server.common.type.LanguageType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,6 +19,7 @@
@RequiredArgsConstructor
public class ArticleContentService {
private final ArticleContentRepository articleContentRepository;
private final TranslateService translateService;

public void createContent(PostArticleRequestDto requestDto, Article article) {

Expand All @@ -30,8 +32,8 @@ public void createContent(PostArticleRequestDto requestDto, Article article) {
languageType -> ArticleContent.builder()
.article(article)
.language(languageType)
// .title(getTranslatePapagoTextArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getTitle()))
// .content(getTranslatePapagoHTMLArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getContent()))
// .title(translateService.naverPapagoTextTranslate(requestDto.getOrgArticleLanguage(), languageType.getLanguageCode(), requestDto.getTitle()))
// .content(translateService.naverPapagoHtmlTranslate(requestDto.getOrgArticleLanguage(), languageType.getLanguageCode(), requestDto.getContent()))
.build()
).toList()
);
Expand Down Expand Up @@ -63,8 +65,9 @@ public void updateContent(PostArticleRequestDto requestDto, Article article) {
existingContents.stream()
.filter(content -> content.getLanguage().equals(languageType))
.forEach(content -> {
// content.articleUpdate(getTranslatePapagoTextArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getTitle()),
// getTranslatePapagoHTMLArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getContent()));
content.articleUpdate(
getTranslatePapagoTextArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getTitle()),
getTranslatePapagoHTMLArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getContent()));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static HookKiller.server.board.type.ArticleStatus.PUBLIC;
Expand Down Expand Up @@ -65,7 +64,7 @@ public Article createArticle(PostArticleRequestDto postArticleRequestDto) {
User requestUser = userUtils.getUser();
return articleRepository.save(Article.builder()
.board(board)
.articleStatus(PUBLIC)
.articleStatus(ArticleStatus.PUBLIC)
.orgArticleLanguage(postArticleRequestDto.getOrgArticleLanguage())
.createdUser(requestUser)
.updatedUser(requestUser)
Expand Down Expand Up @@ -99,5 +98,4 @@ public void deleteArticle(Long articleId) {
.orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION)
.updateStatus(ArticleStatus.DELETE);
}

}
Loading

0 comments on commit 5869220

Please sign in to comment.