From bf9460457c8222ae569962c41d51dd7d6172b1de Mon Sep 17 00:00:00 2001 From: lljh1992 Date: Fri, 6 Oct 2023 19:30:29 +0900 Subject: [PATCH 01/15] =?UTF-8?q?feat=20:=20=EB=A0=88=ED=8F=AC=EC=9E=91?= =?UTF-8?q?=EC=97=85=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit . --- .../board/controller/BoardController.java | 4 ++ .../server/board/entity/Article.java | 53 +++++++++++++++++++ .../server/board/entity/ArticleContent.java | 37 +++++++++++++ .../server/board/entity/ArticleLike.java | 32 +++++++++++ .../HookKiller/server/board/entity/Board.java | 32 +++++++++++ .../HookKiller/server/board/entity/Reply.java | 44 +++++++++++++++ .../server/board/entity/ReplyContent.java | 32 +++++++++++ .../server/board/exception/BaseErrorCode.java | 9 ++++ .../server/board/exception/BaseException.java | 17 ++++++ .../board/exception/ExampleException.java | 10 ++++ .../board/exception/GlobalException.java | 27 ++++++++++ .../board/repository/BoardRepository.java | 12 +++++ .../board/repository/JpaBoardRepository.java | 18 +++++++ .../HookKiller/server/reply/entity/Reply.java | 45 ++++++++++++++++ .../server/reply/entity/ReplyContent.java | 32 +++++++++++ .../server/reply/exception/BaseErrorCode.java | 9 ++++ .../server/reply/exception/BaseException.java | 17 ++++++ .../reply/exception/ExampleException.java | 10 ++++ .../reply/exception/GlobalException.java | 27 ++++++++++ src/main/resources/application-local.yml | 8 +-- 20 files changed, 471 insertions(+), 4 deletions(-) create mode 100644 src/main/java/HookKiller/server/board/controller/BoardController.java create mode 100644 src/main/java/HookKiller/server/board/entity/Article.java create mode 100644 src/main/java/HookKiller/server/board/entity/ArticleContent.java create mode 100644 src/main/java/HookKiller/server/board/entity/ArticleLike.java create mode 100644 src/main/java/HookKiller/server/board/entity/Board.java create mode 100644 src/main/java/HookKiller/server/board/entity/Reply.java create mode 100644 src/main/java/HookKiller/server/board/entity/ReplyContent.java create mode 100644 src/main/java/HookKiller/server/board/exception/BaseErrorCode.java create mode 100644 src/main/java/HookKiller/server/board/exception/BaseException.java create mode 100644 src/main/java/HookKiller/server/board/exception/ExampleException.java create mode 100644 src/main/java/HookKiller/server/board/exception/GlobalException.java create mode 100644 src/main/java/HookKiller/server/board/repository/BoardRepository.java create mode 100644 src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java create mode 100644 src/main/java/HookKiller/server/reply/entity/Reply.java create mode 100644 src/main/java/HookKiller/server/reply/entity/ReplyContent.java create mode 100644 src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java create mode 100644 src/main/java/HookKiller/server/reply/exception/BaseException.java create mode 100644 src/main/java/HookKiller/server/reply/exception/ExampleException.java create mode 100644 src/main/java/HookKiller/server/reply/exception/GlobalException.java diff --git a/src/main/java/HookKiller/server/board/controller/BoardController.java b/src/main/java/HookKiller/server/board/controller/BoardController.java new file mode 100644 index 0000000..6140d69 --- /dev/null +++ b/src/main/java/HookKiller/server/board/controller/BoardController.java @@ -0,0 +1,4 @@ +package HookKiller.server.board.controller; + +public class BoardController { +} diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java new file mode 100644 index 0000000..4070365 --- /dev/null +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -0,0 +1,53 @@ +package HookKiller.server.board.entity; + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.joda.time.DateTime; + +import java.util.List; + +/** + * orgArticleLanguage : 원본으로 작성된 언어 타입. KOR:한국어, ENG:영어, CHI:중국어, JPN:일본어 + * articleType : 게시물 종류. NOTI:공지사항, NORMAL:일반적인 게시물 + * status : 게시물 상태. WRITING:작성중, PUBLIC:공개상태, HIDING:숨김처리, DELETE:삭제처리 + * likeCount : 좋아요 갯수. + * isDeleted : 게시글 삭제 여부 + * createdAt : 게시글 생성일 + * createdUser : 게시글 작성 사용자 ID입력 + * updatedAt : 게시글 정보 업데이트 일자 + * updatedUser : 마지막에 수정한 사용자 ID입력 + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Article { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="board_id") + private Board board; + + @OneToMany + private List ArticleLike; + + @OneToMany + private List articleContent; + + @OneToMany + private List reply; + + private String orgArticleLanguage; + private String articleType; + private String status; + private int likeCount; + private boolean isDeleted; + private DateTime createdAt; + private long createdUser; + private DateTime updatedAt; + private long updatedUser; +} diff --git a/src/main/java/HookKiller/server/board/entity/ArticleContent.java b/src/main/java/HookKiller/server/board/entity/ArticleContent.java new file mode 100644 index 0000000..e26bef6 --- /dev/null +++ b/src/main/java/HookKiller/server/board/entity/ArticleContent.java @@ -0,0 +1,37 @@ +package HookKiller.server.board.entity; + + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * id : PK + * article : 게시글 정보 + * language : 적용된 언어 타입 + * title : 게시글 제목 + * content : 게시글 내용 + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class ArticleContent { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="article_id") + private Article article; + + private String language; + private String title; + + @Column + @Lob + private String content; + +} diff --git a/src/main/java/HookKiller/server/board/entity/ArticleLike.java b/src/main/java/HookKiller/server/board/entity/ArticleLike.java new file mode 100644 index 0000000..4220796 --- /dev/null +++ b/src/main/java/HookKiller/server/board/entity/ArticleLike.java @@ -0,0 +1,32 @@ +package HookKiller.server.board.entity; + + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.joda.time.DateTime; + +/** + * id : PK + * article : 게시물 정보 + * userId : 좋아요를 누른 사용자의 userId + * createdAt : 게시물 좋아요를 클릭한 일자 + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class ArticleLike { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="article_id") + private Article article; + + private long userId; + private DateTime createdAt; +} diff --git a/src/main/java/HookKiller/server/board/entity/Board.java b/src/main/java/HookKiller/server/board/entity/Board.java new file mode 100644 index 0000000..6263150 --- /dev/null +++ b/src/main/java/HookKiller/server/board/entity/Board.java @@ -0,0 +1,32 @@ +package HookKiller.server.board.entity; + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * id : PK + * name :게시판 명 + * boardType : 게시판 종류 + * description : 게시판 사용 용도 + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Board { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @OneToMany + private List
article; + + private String name; + private String boardType; + private String description; + +} \ No newline at end of file diff --git a/src/main/java/HookKiller/server/board/entity/Reply.java b/src/main/java/HookKiller/server/board/entity/Reply.java new file mode 100644 index 0000000..a6cfdaa --- /dev/null +++ b/src/main/java/HookKiller/server/board/entity/Reply.java @@ -0,0 +1,44 @@ +package HookKiller.server.board.entity; + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.joda.time.DateTime; + +import java.util.List; + +/** + * id : PK + * article : 게시글 정보 + * replyContent : 댓글 내용 + * orgReplyLanguage : 원본으로 작성된 언어 타입 + * isDeleted : 댓글 삭제 여부 + * createdAt : 댓글 생성일 + * createdUser : 댓글 작성 사용자 ID + * updatedAt : 댓글 정보 업데이트 + * updatedUser : 마지막에 수정한 사용자 ID + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Reply { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="article_id") + private Article article; + + @OneToMany + private List replyContent; + + private String orgReplyLanguage; + private boolean isDeleted; + private DateTime createdAt; + private long createdUser; + private DateTime updatedAt; + private long updatedUser; +} diff --git a/src/main/java/HookKiller/server/board/entity/ReplyContent.java b/src/main/java/HookKiller/server/board/entity/ReplyContent.java new file mode 100644 index 0000000..9e8559a --- /dev/null +++ b/src/main/java/HookKiller/server/board/entity/ReplyContent.java @@ -0,0 +1,32 @@ +package HookKiller.server.board.entity; + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * language : content가 적용된 언어 타입. + * content : 댓글 내용. + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class ReplyContent { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="reply_id") + private Reply reply; + + private String language; + + @Column + @Lob + private String content; + + +} diff --git a/src/main/java/HookKiller/server/board/exception/BaseErrorCode.java b/src/main/java/HookKiller/server/board/exception/BaseErrorCode.java new file mode 100644 index 0000000..7dba316 --- /dev/null +++ b/src/main/java/HookKiller/server/board/exception/BaseErrorCode.java @@ -0,0 +1,9 @@ +package HookKiller.server.board.exception; + +import HookKiller.server.common.dto.ErrorDetail; + +public interface BaseErrorCode { + + ErrorDetail getErrorDetail(); +} + diff --git a/src/main/java/HookKiller/server/board/exception/BaseException.java b/src/main/java/HookKiller/server/board/exception/BaseException.java new file mode 100644 index 0000000..99ba425 --- /dev/null +++ b/src/main/java/HookKiller/server/board/exception/BaseException.java @@ -0,0 +1,17 @@ +package HookKiller.server.board.exception; + + +import HookKiller.server.common.dto.ErrorDetail; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class BaseException extends RuntimeException { + + private BaseErrorCode errorCode; + + public ErrorDetail getErrorDetail() { + return this.errorCode.getErrorDetail(); + } +} diff --git a/src/main/java/HookKiller/server/board/exception/ExampleException.java b/src/main/java/HookKiller/server/board/exception/ExampleException.java new file mode 100644 index 0000000..f9e9c5e --- /dev/null +++ b/src/main/java/HookKiller/server/board/exception/ExampleException.java @@ -0,0 +1,10 @@ +package HookKiller.server.board.exception; + +public class ExampleException extends BaseException { + + public static final BaseException EXCEPTION = new ExampleException(); + + private ExampleException() { + super(GlobalException.EXAMPLE_ERROR); + } +} diff --git a/src/main/java/HookKiller/server/board/exception/GlobalException.java b/src/main/java/HookKiller/server/board/exception/GlobalException.java new file mode 100644 index 0000000..a326550 --- /dev/null +++ b/src/main/java/HookKiller/server/board/exception/GlobalException.java @@ -0,0 +1,27 @@ +package HookKiller.server.board.exception; + +import HookKiller.server.common.dto.ErrorDetail; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +@Getter +@AllArgsConstructor +public enum GlobalException implements BaseErrorCode { + EXAMPLE_ERROR(BAD_REQUEST.value(), "400-0", "에러 예시 입니다."), + METHOD_ARGUMENT_ERROR( + BAD_REQUEST.value(), "400-1", "메서드 인자가 유효하지 않거나 @Valid를 통과하지 못하여 발생하는 예외입니다."), + INTERNAL_SERVER_ERRORS(INTERNAL_SERVER_ERROR.value(), "500-1", "서버 내부 오류입니다."); + + + private final Integer statusCode; + private final String errorCode; + private final String reason; + + @Override + public ErrorDetail getErrorDetail() { + return ErrorDetail.of(statusCode, errorCode, reason); + } +} diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java new file mode 100644 index 0000000..05cdab3 --- /dev/null +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -0,0 +1,12 @@ +package HookKiller.server.board.repository; + +import HookKiller.server.board.entity.Board; + +import java.util.List; + +public interface BoardRepository { + + Board save(Board board); + List findAll(); + +} diff --git a/src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java b/src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java new file mode 100644 index 0000000..93c170f --- /dev/null +++ b/src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java @@ -0,0 +1,18 @@ +package HookKiller.server.board.repository; + +import HookKiller.server.board.entity.Board; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Locale; + +public interface JpaBoardRepository extends JpaRepository, BoardRepository { + + @Override + default Board save(Board board) { + return null; + } + + @Override + List findAll(); +} diff --git a/src/main/java/HookKiller/server/reply/entity/Reply.java b/src/main/java/HookKiller/server/reply/entity/Reply.java new file mode 100644 index 0000000..2a08740 --- /dev/null +++ b/src/main/java/HookKiller/server/reply/entity/Reply.java @@ -0,0 +1,45 @@ +package HookKiller.server.reply.entity; + +import HookKiller.server.board.entity.Article; +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.joda.time.DateTime; + +import java.util.List; + +/** + * id : PK + * article : 게시글 정보 + * replyContent : 댓글 내용 + * orgReplyLanguage : 원본으로 작성된 언어 타입 + * isDeleted : 댓글 삭제 여부 + * createdAt : 댓글 생성일 + * createdUser : 댓글 작성 사용자 ID + * updatedAt : 댓글 정보 업데이트 + * updatedUser : 마지막에 수정한 사용자 ID + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Reply { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="article_id") + private Article article; + + @OneToMany + private List replyContent; + + private String orgReplyLanguage; + private boolean isDeleted; + private DateTime createdAt; + private long createdUser; + private DateTime updatedAt; + private long updatedUser; +} diff --git a/src/main/java/HookKiller/server/reply/entity/ReplyContent.java b/src/main/java/HookKiller/server/reply/entity/ReplyContent.java new file mode 100644 index 0000000..92e0a1e --- /dev/null +++ b/src/main/java/HookKiller/server/reply/entity/ReplyContent.java @@ -0,0 +1,32 @@ +package HookKiller.server.reply.entity; + +import jakarta.persistence.*; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * language : content가 적용된 언어 타입. + * content : 댓글 내용. + */ + +@Entity +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class ReplyContent { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="reply_id") + private Reply reply; + + private String language; + + @Column + @Lob + private String content; + + +} diff --git a/src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java b/src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java new file mode 100644 index 0000000..f4fdd80 --- /dev/null +++ b/src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java @@ -0,0 +1,9 @@ +package HookKiller.server.reply.exception; + +import HookKiller.server.common.dto.ErrorDetail; + +public interface BaseErrorCode { + + ErrorDetail getErrorDetail(); +} + diff --git a/src/main/java/HookKiller/server/reply/exception/BaseException.java b/src/main/java/HookKiller/server/reply/exception/BaseException.java new file mode 100644 index 0000000..db39f41 --- /dev/null +++ b/src/main/java/HookKiller/server/reply/exception/BaseException.java @@ -0,0 +1,17 @@ +package HookKiller.server.reply.exception; + + +import HookKiller.server.common.dto.ErrorDetail; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class BaseException extends RuntimeException { + + private BaseErrorCode errorCode; + + public ErrorDetail getErrorDetail() { + return this.errorCode.getErrorDetail(); + } +} diff --git a/src/main/java/HookKiller/server/reply/exception/ExampleException.java b/src/main/java/HookKiller/server/reply/exception/ExampleException.java new file mode 100644 index 0000000..411ccb6 --- /dev/null +++ b/src/main/java/HookKiller/server/reply/exception/ExampleException.java @@ -0,0 +1,10 @@ +package HookKiller.server.reply.exception; + +public class ExampleException extends BaseException { + + public static final BaseException EXCEPTION = new ExampleException(); + + private ExampleException() { + super(GlobalException.EXAMPLE_ERROR); + } +} diff --git a/src/main/java/HookKiller/server/reply/exception/GlobalException.java b/src/main/java/HookKiller/server/reply/exception/GlobalException.java new file mode 100644 index 0000000..d5a95e4 --- /dev/null +++ b/src/main/java/HookKiller/server/reply/exception/GlobalException.java @@ -0,0 +1,27 @@ +package HookKiller.server.reply.exception; + +import HookKiller.server.common.dto.ErrorDetail; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import static org.springframework.http.HttpStatus.BAD_REQUEST; +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; + +@Getter +@AllArgsConstructor +public enum GlobalException implements BaseErrorCode { + EXAMPLE_ERROR(BAD_REQUEST.value(), "400-0", "에러 예시 입니다."), + METHOD_ARGUMENT_ERROR( + BAD_REQUEST.value(), "400-1", "메서드 인자가 유효하지 않거나 @Valid를 통과하지 못하여 발생하는 예외입니다."), + INTERNAL_SERVER_ERRORS(INTERNAL_SERVER_ERROR.value(), "500-1", "서버 내부 오류입니다."); + + + private final Integer statusCode; + private final String errorCode; + private final String reason; + + @Override + public ErrorDetail getErrorDetail() { + return ErrorDetail.of(statusCode, errorCode, reason); + } +} diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 1079cdf..c4fc133 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -10,10 +10,10 @@ # Test용 (Mr.재운DB) hook: db: - url: db-i28dv-kr.vpc-pub-cdb.ntruss.com - database: studydb - username: study - password: bitcamp!@#123 + url: db-it7f7-kr.vpc-pub-cdb.ntruss.com + database: jh + username: hooklocal + password: hooklocal1234! port: 3306 spring: From 705bfce90eb499cc7b27581ab9dd71b0ad65261f Mon Sep 17 00:00:00 2001 From: lljh1992 Date: Sat, 7 Oct 2023 14:21:49 +0900 Subject: [PATCH 02/15] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=8B=9C=ED=8C=90=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit . --- .../board/controller/BoardController.java | 22 +++++++++++++++++++ .../HookKiller/server/board/entity/Board.java | 6 +++++ .../board/repository/BoardRepository.java | 13 ++++++++--- .../board/repository/JpaBoardRepository.java | 18 --------------- .../server/board/service/BoardService.java | 21 ++++++++++++++++++ .../server/board/type/BoardType.java | 9 ++++++++ 6 files changed, 68 insertions(+), 21 deletions(-) delete mode 100644 src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java create mode 100644 src/main/java/HookKiller/server/board/service/BoardService.java create mode 100644 src/main/java/HookKiller/server/board/type/BoardType.java diff --git a/src/main/java/HookKiller/server/board/controller/BoardController.java b/src/main/java/HookKiller/server/board/controller/BoardController.java index 6140d69..f0f5420 100644 --- a/src/main/java/HookKiller/server/board/controller/BoardController.java +++ b/src/main/java/HookKiller/server/board/controller/BoardController.java @@ -1,4 +1,26 @@ package HookKiller.server.board.controller; +import HookKiller.server.board.entity.Board; +import HookKiller.server.board.service.BoardService; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Optional; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/board") public class BoardController { + + private final BoardService boardService; + + @GetMapping + public String boardList() { + List boards = boardService.findAll(); + + } } diff --git a/src/main/java/HookKiller/server/board/entity/Board.java b/src/main/java/HookKiller/server/board/entity/Board.java index 6263150..f1518c5 100644 --- a/src/main/java/HookKiller/server/board/entity/Board.java +++ b/src/main/java/HookKiller/server/board/entity/Board.java @@ -1,6 +1,7 @@ package HookKiller.server.board.entity; import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -16,6 +17,7 @@ @Entity @Getter +@Table(name = "Board") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Board { @@ -25,8 +27,12 @@ public class Board { @OneToMany private List
article; + @NotNull private String name; + private String boardType; + + @NotNull private String description; } \ No newline at end of file diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java index 05cdab3..b01a60a 100644 --- a/src/main/java/HookKiller/server/board/repository/BoardRepository.java +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -1,12 +1,19 @@ package HookKiller.server.board.repository; import HookKiller.server.board.entity.Board; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; import java.util.List; -public interface BoardRepository { - Board save(Board board); - List findAll(); +/** + * BoardRepository: BoardRepository 인터페이스는 Board 엔터티를 데이터베이스에 + * 저장, 조회, 수정 및 삭제하기 위한 메서드를 제공합니다. + * Spring Data JPA를 사용하여 기본 CRUD 작업을 자동으로 처리합니다. + */ + +@Repository +public interface BoardRepository extends JpaRepository { } diff --git a/src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java b/src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java deleted file mode 100644 index 93c170f..0000000 --- a/src/main/java/HookKiller/server/board/repository/JpaBoardRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -package HookKiller.server.board.repository; - -import HookKiller.server.board.entity.Board; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.List; -import java.util.Locale; - -public interface JpaBoardRepository extends JpaRepository, BoardRepository { - - @Override - default Board save(Board board) { - return null; - } - - @Override - List findAll(); -} diff --git a/src/main/java/HookKiller/server/board/service/BoardService.java b/src/main/java/HookKiller/server/board/service/BoardService.java new file mode 100644 index 0000000..6483abb --- /dev/null +++ b/src/main/java/HookKiller/server/board/service/BoardService.java @@ -0,0 +1,21 @@ +package HookKiller.server.board.service; + +import HookKiller.server.board.entity.Board; +import HookKiller.server.board.repository.BoardRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +@Service +@RequiredArgsConstructor +public class BoardService { + + private final BoardRepository boardRepository; + + public List findAll() { + return boardRepository.findAll(); + } +} diff --git a/src/main/java/HookKiller/server/board/type/BoardType.java b/src/main/java/HookKiller/server/board/type/BoardType.java new file mode 100644 index 0000000..3248538 --- /dev/null +++ b/src/main/java/HookKiller/server/board/type/BoardType.java @@ -0,0 +1,9 @@ +package HookKiller.server.board.type; + +import lombok.Getter; + +public enum BoardType { + KOREA, + JAPAN, + CHINA; +} From 80c758754f77d43c34663de3166b5f14389998b1 Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Sat, 7 Oct 2023 16:49:23 +0900 Subject: [PATCH 03/15] =?UTF-8?q?Feat=20:=20article=20=EC=A0=84=EC=B2=B4?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/ArticleController.java | 30 +++++++++++++ .../board/controller/BoardController.java | 7 +-- .../server/board/dto/ArticleDto.java | 31 +++++++++++++ .../HookKiller/server/board/dto/BoardDto.java | 29 ++++++++++++ .../server/board/entity/Article.java | 8 ++-- .../HookKiller/server/board/entity/Board.java | 7 ++- .../repository/ArticleContentRepository.java | 11 +++++ .../board/repository/ArticleRepository.java | 13 ++++++ .../board/repository/BoardRepository.java | 2 +- .../server/board/service/ArticleService.java | 31 +++++++++++++ .../server/board/service/BoardService.java | 9 +++- .../server/board/type/BoardType.java | 11 +++-- .../HookKiller/server/reply/entity/Reply.java | 45 ------------------- .../server/reply/entity/ReplyContent.java | 32 ------------- .../server/reply/exception/BaseErrorCode.java | 9 ---- .../server/reply/exception/BaseException.java | 17 ------- .../reply/exception/ExampleException.java | 10 ----- .../reply/exception/GlobalException.java | 27 ----------- 18 files changed, 174 insertions(+), 155 deletions(-) create mode 100644 src/main/java/HookKiller/server/board/controller/ArticleController.java create mode 100644 src/main/java/HookKiller/server/board/dto/ArticleDto.java create mode 100644 src/main/java/HookKiller/server/board/dto/BoardDto.java create mode 100644 src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java create mode 100644 src/main/java/HookKiller/server/board/repository/ArticleRepository.java create mode 100644 src/main/java/HookKiller/server/board/service/ArticleService.java delete mode 100644 src/main/java/HookKiller/server/reply/entity/Reply.java delete mode 100644 src/main/java/HookKiller/server/reply/entity/ReplyContent.java delete mode 100644 src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java delete mode 100644 src/main/java/HookKiller/server/reply/exception/BaseException.java delete mode 100644 src/main/java/HookKiller/server/reply/exception/ExampleException.java delete mode 100644 src/main/java/HookKiller/server/reply/exception/GlobalException.java diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java new file mode 100644 index 0000000..11f0e06 --- /dev/null +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -0,0 +1,30 @@ +package HookKiller.server.board.controller; + +import HookKiller.server.board.dto.ArticleDto; +import HookKiller.server.board.entity.Article; +import HookKiller.server.board.service.ArticleService; +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/article") +public class ArticleController { + + private final ArticleService articleService; + + @GetMapping("/{boardId}") + public List getArticleList(@PathVariable Long boardId) { + // TODO: 추후 RequestHeader에서 User가 선택한 언어를 뽑아오는 작업으로 수정할 예정 + String language = "KOR"; + List dto = articleService.getArticleList(boardId, language); + return dto; + } + +} diff --git a/src/main/java/HookKiller/server/board/controller/BoardController.java b/src/main/java/HookKiller/server/board/controller/BoardController.java index f0f5420..b039853 100644 --- a/src/main/java/HookKiller/server/board/controller/BoardController.java +++ b/src/main/java/HookKiller/server/board/controller/BoardController.java @@ -1,5 +1,6 @@ package HookKiller.server.board.controller; +import HookKiller.server.board.dto.BoardDto; import HookKiller.server.board.entity.Board; import HookKiller.server.board.service.BoardService; import lombok.Getter; @@ -19,8 +20,8 @@ public class BoardController { private final BoardService boardService; @GetMapping - public String boardList() { - List boards = boardService.findAll(); - + public List getBoardList() { + List dto = boardService.getBoardList(); + return dto; } } diff --git a/src/main/java/HookKiller/server/board/dto/ArticleDto.java b/src/main/java/HookKiller/server/board/dto/ArticleDto.java new file mode 100644 index 0000000..aee1f99 --- /dev/null +++ b/src/main/java/HookKiller/server/board/dto/ArticleDto.java @@ -0,0 +1,31 @@ +package HookKiller.server.board.dto; + +import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.ArticleContent; +import lombok.Builder; +import lombok.Getter; +import org.joda.time.DateTime; + +@Getter +@Builder +public class ArticleDto { + private Long id; + private String title; + private Long createdUser; + private DateTime createdAt; + private int likeCount; + +public static ArticleDto of(Article article, ArticleContent articleContent) { + return ArticleDto.builder() + .id(article.getId()) + .title(articleContent.getTitle()) + .createdUser(article.getCreatedUser()) + .createdAt(article.getCreatedAt()) + .likeCount(article.getLikeCount()) + .build(); + } + + + + +} diff --git a/src/main/java/HookKiller/server/board/dto/BoardDto.java b/src/main/java/HookKiller/server/board/dto/BoardDto.java new file mode 100644 index 0000000..b58ec9d --- /dev/null +++ b/src/main/java/HookKiller/server/board/dto/BoardDto.java @@ -0,0 +1,29 @@ +package HookKiller.server.board.dto; + +import HookKiller.server.board.entity.Board; +import HookKiller.server.board.repository.BoardRepository; +import jakarta.persistence.Enumerated; +import lombok.Builder; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.springframework.stereotype.Service; + +@Getter +@Builder +public class BoardDto { + + private Long id; + private String name; + private String boardType; + private String description; + + public BoardDto from(Board board) { + return BoardDto.builder() + .id(board.getId()) + .name(board.getName()) + .description(board.getDescription()) + .build(); + } + +} diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index 4070365..1541475 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -1,9 +1,7 @@ package HookKiller.server.board.entity; import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import org.joda.time.DateTime; import java.util.List; @@ -22,7 +20,7 @@ @Entity @Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) +@RequiredArgsConstructor public class Article { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -50,4 +48,6 @@ public class Article { private long createdUser; private DateTime updatedAt; private long updatedUser; + + } diff --git a/src/main/java/HookKiller/server/board/entity/Board.java b/src/main/java/HookKiller/server/board/entity/Board.java index f1518c5..297af04 100644 --- a/src/main/java/HookKiller/server/board/entity/Board.java +++ b/src/main/java/HookKiller/server/board/entity/Board.java @@ -1,10 +1,12 @@ package HookKiller.server.board.entity; +import HookKiller.server.board.type.BoardType; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; import java.util.List; @@ -18,7 +20,7 @@ @Entity @Getter @Table(name = "Board") -@NoArgsConstructor(access = AccessLevel.PROTECTED) +@RequiredArgsConstructor public class Board { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -30,7 +32,8 @@ public class Board { @NotNull private String name; - private String boardType; + @Enumerated(EnumType.STRING) + private BoardType boardType; @NotNull private String description; diff --git a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java new file mode 100644 index 0000000..588d3b1 --- /dev/null +++ b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java @@ -0,0 +1,11 @@ +package HookKiller.server.board.repository; + +import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.ArticleContent; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface ArticleContentRepository extends JpaRepository { + Optional findByArticleAndLanguage(Article article, String language); +} diff --git a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java new file mode 100644 index 0000000..f0080c3 --- /dev/null +++ b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java @@ -0,0 +1,13 @@ +package HookKiller.server.board.repository; + +import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.Board; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface ArticleRepository extends JpaRepository { + List
findAllByBoard(Board board); +} diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java index b01a60a..1d08795 100644 --- a/src/main/java/HookKiller/server/board/repository/BoardRepository.java +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -15,5 +15,5 @@ @Repository public interface BoardRepository extends JpaRepository { - + ㅣㅑㄴ } diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java new file mode 100644 index 0000000..717cba7 --- /dev/null +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -0,0 +1,31 @@ +package HookKiller.server.board.service; + +import HookKiller.server.board.dto.ArticleDto; +import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.ArticleContent; +import HookKiller.server.board.repository.ArticleContentRepository; +import HookKiller.server.board.repository.ArticleRepository; +import HookKiller.server.board.repository.BoardRepository; +import HookKiller.server.common.exception.ExampleException; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Service +@RequiredArgsConstructor +public class ArticleService { + private final BoardRepository boardRepository; + private final ArticleRepository articleRepository; + private final ArticleContentRepository articleContentRepository; + + @Transactional(readOnly = true) + public List getArticleList(Long boardId, String language) { + return articleRepository.findAllByBoard(boardRepository.findById(boardId).orElseThrow(() -> ExampleException.EXCEPTION)) + .stream() + .map(article -> ArticleDto.of(article, articleContentRepository.findByArticleAndLanguage(article, language).orElseThrow(() -> ExampleException.EXCEPTION))) + .toList(); + } +} diff --git a/src/main/java/HookKiller/server/board/service/BoardService.java b/src/main/java/HookKiller/server/board/service/BoardService.java index 6483abb..6edd697 100644 --- a/src/main/java/HookKiller/server/board/service/BoardService.java +++ b/src/main/java/HookKiller/server/board/service/BoardService.java @@ -1,11 +1,13 @@ package HookKiller.server.board.service; +import HookKiller.server.board.dto.BoardDto; import HookKiller.server.board.entity.Board; import HookKiller.server.board.repository.BoardRepository; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -15,7 +17,10 @@ public class BoardService { private final BoardRepository boardRepository; - public List findAll() { - return boardRepository.findAll(); + public List getBoardList() { + List boardList = boardRepository.findAll(); + List boardDtoList = new ArrayList<>(); +// boardDtoList = boardList.stream().map().toList(); + return boardDtoList; } } diff --git a/src/main/java/HookKiller/server/board/type/BoardType.java b/src/main/java/HookKiller/server/board/type/BoardType.java index 3248538..b5ef078 100644 --- a/src/main/java/HookKiller/server/board/type/BoardType.java +++ b/src/main/java/HookKiller/server/board/type/BoardType.java @@ -1,9 +1,14 @@ package HookKiller.server.board.type; import lombok.Getter; +import lombok.RequiredArgsConstructor; +@Getter +@RequiredArgsConstructor public enum BoardType { - KOREA, - JAPAN, - CHINA; + KOREA("한국 게시판"), + JAPAN("일본 게시판"), + CHINA("중국 게시판"); + + private final String typeName; } diff --git a/src/main/java/HookKiller/server/reply/entity/Reply.java b/src/main/java/HookKiller/server/reply/entity/Reply.java deleted file mode 100644 index 2a08740..0000000 --- a/src/main/java/HookKiller/server/reply/entity/Reply.java +++ /dev/null @@ -1,45 +0,0 @@ -package HookKiller.server.reply.entity; - -import HookKiller.server.board.entity.Article; -import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.joda.time.DateTime; - -import java.util.List; - -/** - * id : PK - * article : 게시글 정보 - * replyContent : 댓글 내용 - * orgReplyLanguage : 원본으로 작성된 언어 타입 - * isDeleted : 댓글 삭제 여부 - * createdAt : 댓글 생성일 - * createdUser : 댓글 작성 사용자 ID - * updatedAt : 댓글 정보 업데이트 - * updatedUser : 마지막에 수정한 사용자 ID - */ - -@Entity -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Reply { - - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name="article_id") - private Article article; - - @OneToMany - private List replyContent; - - private String orgReplyLanguage; - private boolean isDeleted; - private DateTime createdAt; - private long createdUser; - private DateTime updatedAt; - private long updatedUser; -} diff --git a/src/main/java/HookKiller/server/reply/entity/ReplyContent.java b/src/main/java/HookKiller/server/reply/entity/ReplyContent.java deleted file mode 100644 index 92e0a1e..0000000 --- a/src/main/java/HookKiller/server/reply/entity/ReplyContent.java +++ /dev/null @@ -1,32 +0,0 @@ -package HookKiller.server.reply.entity; - -import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -/** - * language : content가 적용된 언어 타입. - * content : 댓글 내용. - */ - -@Entity -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class ReplyContent { - - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name="reply_id") - private Reply reply; - - private String language; - - @Column - @Lob - private String content; - - -} diff --git a/src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java b/src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java deleted file mode 100644 index f4fdd80..0000000 --- a/src/main/java/HookKiller/server/reply/exception/BaseErrorCode.java +++ /dev/null @@ -1,9 +0,0 @@ -package HookKiller.server.reply.exception; - -import HookKiller.server.common.dto.ErrorDetail; - -public interface BaseErrorCode { - - ErrorDetail getErrorDetail(); -} - diff --git a/src/main/java/HookKiller/server/reply/exception/BaseException.java b/src/main/java/HookKiller/server/reply/exception/BaseException.java deleted file mode 100644 index db39f41..0000000 --- a/src/main/java/HookKiller/server/reply/exception/BaseException.java +++ /dev/null @@ -1,17 +0,0 @@ -package HookKiller.server.reply.exception; - - -import HookKiller.server.common.dto.ErrorDetail; -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public class BaseException extends RuntimeException { - - private BaseErrorCode errorCode; - - public ErrorDetail getErrorDetail() { - return this.errorCode.getErrorDetail(); - } -} diff --git a/src/main/java/HookKiller/server/reply/exception/ExampleException.java b/src/main/java/HookKiller/server/reply/exception/ExampleException.java deleted file mode 100644 index 411ccb6..0000000 --- a/src/main/java/HookKiller/server/reply/exception/ExampleException.java +++ /dev/null @@ -1,10 +0,0 @@ -package HookKiller.server.reply.exception; - -public class ExampleException extends BaseException { - - public static final BaseException EXCEPTION = new ExampleException(); - - private ExampleException() { - super(GlobalException.EXAMPLE_ERROR); - } -} diff --git a/src/main/java/HookKiller/server/reply/exception/GlobalException.java b/src/main/java/HookKiller/server/reply/exception/GlobalException.java deleted file mode 100644 index d5a95e4..0000000 --- a/src/main/java/HookKiller/server/reply/exception/GlobalException.java +++ /dev/null @@ -1,27 +0,0 @@ -package HookKiller.server.reply.exception; - -import HookKiller.server.common.dto.ErrorDetail; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; - -@Getter -@AllArgsConstructor -public enum GlobalException implements BaseErrorCode { - EXAMPLE_ERROR(BAD_REQUEST.value(), "400-0", "에러 예시 입니다."), - METHOD_ARGUMENT_ERROR( - BAD_REQUEST.value(), "400-1", "메서드 인자가 유효하지 않거나 @Valid를 통과하지 못하여 발생하는 예외입니다."), - INTERNAL_SERVER_ERRORS(INTERNAL_SERVER_ERROR.value(), "500-1", "서버 내부 오류입니다."); - - - private final Integer statusCode; - private final String errorCode; - private final String reason; - - @Override - public ErrorDetail getErrorDetail() { - return ErrorDetail.of(statusCode, errorCode, reason); - } -} From c2b9536fa59e2334498bacc1e6c34e89427d696e Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Sat, 7 Oct 2023 18:00:07 +0900 Subject: [PATCH 04/15] =?UTF-8?q?Feat=20:=20article=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/ArticleController.java | 10 +++------- .../HookKiller/server/board/dto/ArticleDto.java | 14 ++++++-------- .../HookKiller/server/board/dto/BoardDto.java | 2 +- .../repository/ArticleContentRepository.java | 5 ++++- .../board/repository/ArticleRepository.java | 4 +++- .../server/board/service/ArticleService.java | 15 +++++++-------- 6 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index 11f0e06..e037f4e 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -3,7 +3,6 @@ import HookKiller.server.board.dto.ArticleDto; import HookKiller.server.board.entity.Article; import HookKiller.server.board.service.ArticleService; -import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -13,18 +12,15 @@ import java.util.List; @RestController +@RequestMapping("/articles") @RequiredArgsConstructor -@RequestMapping("/article") public class ArticleController { private final ArticleService articleService; - @GetMapping("/{boardId}") + @GetMapping("{/boardId}") public List getArticleList(@PathVariable Long boardId) { - // TODO: 추후 RequestHeader에서 User가 선택한 언어를 뽑아오는 작업으로 수정할 예정 String language = "KOR"; - List dto = articleService.getArticleList(boardId, language); - return dto; + return articleService.getArticleList(boardId, language); } - } diff --git a/src/main/java/HookKiller/server/board/dto/ArticleDto.java b/src/main/java/HookKiller/server/board/dto/ArticleDto.java index aee1f99..c237a04 100644 --- a/src/main/java/HookKiller/server/board/dto/ArticleDto.java +++ b/src/main/java/HookKiller/server/board/dto/ArticleDto.java @@ -9,23 +9,21 @@ @Getter @Builder public class ArticleDto { - private Long id; + + private Long articleId; private String title; - private Long createdUser; private DateTime createdAt; + private Long createdUser; private int likeCount; -public static ArticleDto of(Article article, ArticleContent articleContent) { + public static ArticleDto of(Article article, ArticleContent articleContent) { return ArticleDto.builder() - .id(article.getId()) + .articleId(article.getId()) .title(articleContent.getTitle()) - .createdUser(article.getCreatedUser()) .createdAt(article.getCreatedAt()) + .createdUser(article.getCreatedUser()) .likeCount(article.getLikeCount()) .build(); } - - - } diff --git a/src/main/java/HookKiller/server/board/dto/BoardDto.java b/src/main/java/HookKiller/server/board/dto/BoardDto.java index b58ec9d..e7a3411 100644 --- a/src/main/java/HookKiller/server/board/dto/BoardDto.java +++ b/src/main/java/HookKiller/server/board/dto/BoardDto.java @@ -18,7 +18,7 @@ public class BoardDto { private String boardType; private String description; - public BoardDto from(Board board) { + public static BoardDto from(Board board) { return BoardDto.builder() .id(board.getId()) .name(board.getName()) diff --git a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java index 588d3b1..411a5b0 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java @@ -3,9 +3,12 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.ArticleContent; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; import java.util.Optional; +@Repository public interface ArticleContentRepository extends JpaRepository { - Optional findByArticleAndLanguage(Article article, String language); + public Optional findByArticleAndLanguage(Article article, String language); + } diff --git a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java index f0080c3..798d641 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java @@ -1,6 +1,7 @@ package HookKiller.server.board.repository; import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.ArticleContent; import HookKiller.server.board.entity.Board; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,5 +10,6 @@ @Repository public interface ArticleRepository extends JpaRepository { - List
findAllByBoard(Board board); + + public List
findAllByBoard(Board board); } diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index 717cba7..834b44d 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -1,31 +1,30 @@ package HookKiller.server.board.service; import HookKiller.server.board.dto.ArticleDto; -import HookKiller.server.board.entity.Article; -import HookKiller.server.board.entity.ArticleContent; import HookKiller.server.board.repository.ArticleContentRepository; import HookKiller.server.board.repository.ArticleRepository; import HookKiller.server.board.repository.BoardRepository; import HookKiller.server.common.exception.ExampleException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; @Service @RequiredArgsConstructor public class ArticleService { + private final BoardRepository boardRepository; private final ArticleRepository articleRepository; private final ArticleContentRepository articleContentRepository; - @Transactional(readOnly = true) public List getArticleList(Long boardId, String language) { - return articleRepository.findAllByBoard(boardRepository.findById(boardId).orElseThrow(() -> ExampleException.EXCEPTION)) - .stream() - .map(article -> ArticleDto.of(article, articleContentRepository.findByArticleAndLanguage(article, language).orElseThrow(() -> ExampleException.EXCEPTION))) + // boardId로 board에 해당하는 Article들을 모두 뽑아온다 + // Article들 하나하나마다에 해당하는 ArticleContent랑 같이 DTO로 변환한다 + // 변환된 DTO들을 모아놓은 list를 리턴한다 + return articleRepository + .findAllByBoard(boardRepository.findById(boardId).orElseThrow(() -> ExampleException.EXCEPTION)) + .stream().map(article -> ArticleDto.of(article, articleContentRepository.findByArticleAndLanguage(article, language).orElseThrow(() -> ExampleException.EXCEPTION))) .toList(); } } From e12d18395f3112b49704680fe280ecb752ea8ab0 Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Tue, 10 Oct 2023 10:32:38 +0900 Subject: [PATCH 05/15] =?UTF-8?q?board=20entity,=20repo=20=EC=9E=91?= =?UTF-8?q?=EC=97=85=20=EC=99=84=EB=A3=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/HookKiller/server/board/entity/Article.java | 12 +++++------- .../server/board/entity/ArticleContent.java | 2 +- .../HookKiller/server/board/entity/ArticleLike.java | 6 +++--- .../java/HookKiller/server/board/entity/Reply.java | 9 ++++----- .../HookKiller/server/board/entity/ReplyContent.java | 1 + .../server/board/repository/ArticleRepository.java | 1 - .../server/board/repository/BoardRepository.java | 3 --- .../board/repository/ReplyContentRepository.java | 9 +++++++++ .../server/board/repository/ReplyRepository.java | 9 +++++++++ .../java/HookKiller/server/user/type/UserRole.java | 2 +- 10 files changed, 33 insertions(+), 21 deletions(-) create mode 100644 src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java create mode 100644 src/main/java/HookKiller/server/board/repository/ReplyRepository.java diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index 1541475..5b73c07 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -1,5 +1,6 @@ package HookKiller.server.board.entity; +import HookKiller.server.common.AbstractTimeStamp; import jakarta.persistence.*; import lombok.*; import org.joda.time.DateTime; @@ -21,10 +22,10 @@ @Entity @Getter @RequiredArgsConstructor -public class Article { +public class Article extends AbstractTimeStamp { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; + private Long id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="board_id") @@ -44,10 +45,7 @@ public class Article { private String status; private int likeCount; private boolean isDeleted; - private DateTime createdAt; - private long createdUser; - private DateTime updatedAt; - private long updatedUser; - + private Long createdUser; + private Long updatedUser; } diff --git a/src/main/java/HookKiller/server/board/entity/ArticleContent.java b/src/main/java/HookKiller/server/board/entity/ArticleContent.java index e26bef6..d75a039 100644 --- a/src/main/java/HookKiller/server/board/entity/ArticleContent.java +++ b/src/main/java/HookKiller/server/board/entity/ArticleContent.java @@ -21,7 +21,7 @@ public class ArticleContent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; + private Long id; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="article_id") diff --git a/src/main/java/HookKiller/server/board/entity/ArticleLike.java b/src/main/java/HookKiller/server/board/entity/ArticleLike.java index 4220796..c936282 100644 --- a/src/main/java/HookKiller/server/board/entity/ArticleLike.java +++ b/src/main/java/HookKiller/server/board/entity/ArticleLike.java @@ -1,6 +1,7 @@ package HookKiller.server.board.entity; +import HookKiller.server.common.AbstractTimeStamp; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; @@ -17,7 +18,7 @@ @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class ArticleLike { +public class ArticleLike extends AbstractTimeStamp { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -27,6 +28,5 @@ public class ArticleLike { @JoinColumn(name="article_id") private Article article; - private long userId; - private DateTime createdAt; + private Long userId; } diff --git a/src/main/java/HookKiller/server/board/entity/Reply.java b/src/main/java/HookKiller/server/board/entity/Reply.java index a6cfdaa..f86e1c4 100644 --- a/src/main/java/HookKiller/server/board/entity/Reply.java +++ b/src/main/java/HookKiller/server/board/entity/Reply.java @@ -1,5 +1,6 @@ package HookKiller.server.board.entity; +import HookKiller.server.common.AbstractTimeStamp; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; @@ -23,7 +24,7 @@ @Entity @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Reply { +public class Reply extends AbstractTimeStamp { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @@ -37,8 +38,6 @@ public class Reply { private String orgReplyLanguage; private boolean isDeleted; - private DateTime createdAt; - private long createdUser; - private DateTime updatedAt; - private long updatedUser; + private Long createdUser; + private Long updatedUser; } diff --git a/src/main/java/HookKiller/server/board/entity/ReplyContent.java b/src/main/java/HookKiller/server/board/entity/ReplyContent.java index 9e8559a..424e872 100644 --- a/src/main/java/HookKiller/server/board/entity/ReplyContent.java +++ b/src/main/java/HookKiller/server/board/entity/ReplyContent.java @@ -1,5 +1,6 @@ package HookKiller.server.board.entity; +import HookKiller.server.common.AbstractTimeStamp; import jakarta.persistence.*; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java index 798d641..93cfdb7 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java @@ -1,7 +1,6 @@ package HookKiller.server.board.repository; import HookKiller.server.board.entity.Article; -import HookKiller.server.board.entity.ArticleContent; import HookKiller.server.board.entity.Board; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java index 1d08795..146c0d9 100644 --- a/src/main/java/HookKiller/server/board/repository/BoardRepository.java +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -4,8 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; - /** * BoardRepository: BoardRepository 인터페이스는 Board 엔터티를 데이터베이스에 @@ -15,5 +13,4 @@ @Repository public interface BoardRepository extends JpaRepository { - ㅣㅑㄴ } diff --git a/src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java b/src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java new file mode 100644 index 0000000..25d2a4b --- /dev/null +++ b/src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java @@ -0,0 +1,9 @@ +package HookKiller.server.board.repository; + +import HookKiller.server.board.entity.ReplyContent; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ReplyContentRepository extends JpaRepository { +} diff --git a/src/main/java/HookKiller/server/board/repository/ReplyRepository.java b/src/main/java/HookKiller/server/board/repository/ReplyRepository.java new file mode 100644 index 0000000..c79b063 --- /dev/null +++ b/src/main/java/HookKiller/server/board/repository/ReplyRepository.java @@ -0,0 +1,9 @@ +package HookKiller.server.board.repository; + +import HookKiller.server.board.entity.Reply; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ReplyRepository extends JpaRepository { +} diff --git a/src/main/java/HookKiller/server/user/type/UserRole.java b/src/main/java/HookKiller/server/user/type/UserRole.java index 2dd24af..f958975 100644 --- a/src/main/java/HookKiller/server/user/type/UserRole.java +++ b/src/main/java/HookKiller/server/user/type/UserRole.java @@ -1,4 +1,4 @@ -정package HookKiller.server.user.type; +package HookKiller.server.user.type; import lombok.AllArgsConstructor; import lombok.Getter; From a22cf17e8e02136eeef9aaf4a5f4caaeaebb85ac Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Tue, 10 Oct 2023 11:04:53 +0900 Subject: [PATCH 06/15] build complate --- src/main/java/HookKiller/server/board/dto/ArticleDto.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/HookKiller/server/board/dto/ArticleDto.java b/src/main/java/HookKiller/server/board/dto/ArticleDto.java index c237a04..84da293 100644 --- a/src/main/java/HookKiller/server/board/dto/ArticleDto.java +++ b/src/main/java/HookKiller/server/board/dto/ArticleDto.java @@ -6,13 +6,15 @@ import lombok.Getter; import org.joda.time.DateTime; +import java.sql.Timestamp; + @Getter @Builder public class ArticleDto { private Long articleId; private String title; - private DateTime createdAt; + private Timestamp createdAt; private Long createdUser; private int likeCount; @@ -20,7 +22,7 @@ public static ArticleDto of(Article article, ArticleContent articleContent) { return ArticleDto.builder() .articleId(article.getId()) .title(articleContent.getTitle()) - .createdAt(article.getCreatedAt()) + .createdAt(article.getCreateAt()) .createdUser(article.getCreatedUser()) .likeCount(article.getLikeCount()) .build(); From d1ec33d049a81da08b263019604820e1d8f13b25 Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Tue, 10 Oct 2023 21:35:27 +0900 Subject: [PATCH 07/15] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/ArticleController.java | 19 +++++++----- .../board/controller/BoardController.java | 27 ----------------- .../server/board/dto/ArticleDto.java | 18 ++++++++++-- .../HookKiller/server/board/dto/BoardDto.java | 29 ------------------- .../server/board/entity/Article.java | 28 ++++++++++++------ .../server/board/entity/ArticleContent.java | 18 ++++++++++-- .../server/board/entity/ArticleLike.java | 11 +++++-- .../HookKiller/server/board/entity/Board.java | 16 ++++++---- .../HookKiller/server/board/entity/Reply.java | 18 ++++++++---- .../server/board/entity/ReplyContent.java | 16 ++++++++-- .../ArticleContentNotFoundException.java | 13 +++++++++ .../server/board/exception/BaseErrorCode.java | 9 ------ .../server/board/exception/BaseException.java | 17 ----------- .../board/exception/BoardException.java | 26 +++++++++++++++++ .../exception/BoardNotFoundException.java | 13 +++++++++ .../board/exception/ExampleException.java | 10 ------- .../board/exception/GlobalException.java | 27 ----------------- .../repository/ArticleContentRepository.java | 5 ++-- .../board/repository/ArticleRepository.java | 3 +- .../board/repository/BoardRepository.java | 3 +- .../repository/ReplyContentRepository.java | 2 -- .../board/repository/ReplyRepository.java | 2 -- .../server/board/service/ArticleService.java | 26 ++++++++++++----- .../server/board/service/BoardService.java | 26 ----------------- 24 files changed, 182 insertions(+), 200 deletions(-) delete mode 100644 src/main/java/HookKiller/server/board/controller/BoardController.java delete mode 100644 src/main/java/HookKiller/server/board/dto/BoardDto.java create mode 100644 src/main/java/HookKiller/server/board/exception/ArticleContentNotFoundException.java delete mode 100644 src/main/java/HookKiller/server/board/exception/BaseErrorCode.java delete mode 100644 src/main/java/HookKiller/server/board/exception/BaseException.java create mode 100644 src/main/java/HookKiller/server/board/exception/BoardException.java create mode 100644 src/main/java/HookKiller/server/board/exception/BoardNotFoundException.java delete mode 100644 src/main/java/HookKiller/server/board/exception/ExampleException.java delete mode 100644 src/main/java/HookKiller/server/board/exception/GlobalException.java delete mode 100644 src/main/java/HookKiller/server/board/service/BoardService.java diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index e037f4e..72992b5 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -1,13 +1,13 @@ package HookKiller.server.board.controller; import HookKiller.server.board.dto.ArticleDto; -import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.Board; import HookKiller.server.board.service.ArticleService; +import HookKiller.server.board.type.BoardType; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -18,9 +18,12 @@ public class ArticleController { private final ArticleService articleService; - @GetMapping("{/boardId}") - public List getArticleList(@PathVariable Long boardId) { - String language = "KOR"; + + @GetMapping("/{boardId}") + public List getArticleList(@PathVariable Long boardId, HttpServletRequest request) { + BoardType language = BoardType.valueOf(request.getHeader("language")); return articleService.getArticleList(boardId, language); } + + } diff --git a/src/main/java/HookKiller/server/board/controller/BoardController.java b/src/main/java/HookKiller/server/board/controller/BoardController.java deleted file mode 100644 index b039853..0000000 --- a/src/main/java/HookKiller/server/board/controller/BoardController.java +++ /dev/null @@ -1,27 +0,0 @@ -package HookKiller.server.board.controller; - -import HookKiller.server.board.dto.BoardDto; -import HookKiller.server.board.entity.Board; -import HookKiller.server.board.service.BoardService; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.*; - -import java.util.List; -import java.util.Optional; - -@RestController -@RequiredArgsConstructor -@RequestMapping("/board") -public class BoardController { - - private final BoardService boardService; - - @GetMapping - public List getBoardList() { - List dto = boardService.getBoardList(); - return dto; - } -} diff --git a/src/main/java/HookKiller/server/board/dto/ArticleDto.java b/src/main/java/HookKiller/server/board/dto/ArticleDto.java index 84da293..12b4a42 100644 --- a/src/main/java/HookKiller/server/board/dto/ArticleDto.java +++ b/src/main/java/HookKiller/server/board/dto/ArticleDto.java @@ -2,9 +2,12 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.ArticleContent; +import HookKiller.server.board.type.BoardType; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.Lob; import lombok.Builder; import lombok.Getter; -import org.joda.time.DateTime; import java.sql.Timestamp; @@ -14,16 +17,25 @@ public class ArticleDto { private Long articleId; private String title; + + @Lob + private String content; private Timestamp createdAt; - private Long createdUser; + private Long createdUserId; private int likeCount; + private Long boardId; + + @Enumerated(EnumType.STRING) + private BoardType boardType; + public static ArticleDto of(Article article, ArticleContent articleContent) { return ArticleDto.builder() .articleId(article.getId()) .title(articleContent.getTitle()) + .content(articleContent.getContent()) .createdAt(article.getCreateAt()) - .createdUser(article.getCreatedUser()) + .createdUserId(article.getCreatedUserId()) .likeCount(article.getLikeCount()) .build(); } diff --git a/src/main/java/HookKiller/server/board/dto/BoardDto.java b/src/main/java/HookKiller/server/board/dto/BoardDto.java deleted file mode 100644 index e7a3411..0000000 --- a/src/main/java/HookKiller/server/board/dto/BoardDto.java +++ /dev/null @@ -1,29 +0,0 @@ -package HookKiller.server.board.dto; - -import HookKiller.server.board.entity.Board; -import HookKiller.server.board.repository.BoardRepository; -import jakarta.persistence.Enumerated; -import lombok.Builder; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.Setter; -import org.springframework.stereotype.Service; - -@Getter -@Builder -public class BoardDto { - - private Long id; - private String name; - private String boardType; - private String description; - - public static BoardDto from(Board board) { - return BoardDto.builder() - .id(board.getId()) - .name(board.getName()) - .description(board.getDescription()) - .build(); - } - -} diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index 5b73c07..a6fe4e9 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -1,9 +1,18 @@ package HookKiller.server.board.entity; import HookKiller.server.common.AbstractTimeStamp; -import jakarta.persistence.*; -import lombok.*; -import org.joda.time.DateTime; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; import java.util.List; @@ -21,7 +30,8 @@ @Entity @Getter -@RequiredArgsConstructor +@Table(name = "tbl_article") +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Article extends AbstractTimeStamp { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -31,13 +41,13 @@ public class Article extends AbstractTimeStamp { @JoinColumn(name="board_id") private Board board; - @OneToMany + @OneToMany(mappedBy = "article") private List ArticleLike; - @OneToMany + @OneToMany(mappedBy = "article") private List articleContent; - @OneToMany + @OneToMany(mappedBy = "article") private List reply; private String orgArticleLanguage; @@ -45,7 +55,7 @@ public class Article extends AbstractTimeStamp { private String status; private int likeCount; private boolean isDeleted; - private Long createdUser; - private Long updatedUser; + private Long createdUserId; + private Long updatedUserId; } diff --git a/src/main/java/HookKiller/server/board/entity/ArticleContent.java b/src/main/java/HookKiller/server/board/entity/ArticleContent.java index d75a039..a5ea46d 100644 --- a/src/main/java/HookKiller/server/board/entity/ArticleContent.java +++ b/src/main/java/HookKiller/server/board/entity/ArticleContent.java @@ -1,7 +1,19 @@ package HookKiller.server.board.entity; -import jakarta.persistence.*; +import HookKiller.server.board.type.BoardType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.Lob; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -16,6 +28,7 @@ @Entity @Getter +@Table(name = "tbl_article_content") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class ArticleContent { @@ -27,7 +40,8 @@ public class ArticleContent { @JoinColumn(name="article_id") private Article article; - private String language; + @Enumerated(EnumType.STRING) + private BoardType language; private String title; @Column diff --git a/src/main/java/HookKiller/server/board/entity/ArticleLike.java b/src/main/java/HookKiller/server/board/entity/ArticleLike.java index c936282..76843f0 100644 --- a/src/main/java/HookKiller/server/board/entity/ArticleLike.java +++ b/src/main/java/HookKiller/server/board/entity/ArticleLike.java @@ -2,11 +2,17 @@ import HookKiller.server.common.AbstractTimeStamp; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import org.joda.time.DateTime; /** * id : PK @@ -17,6 +23,7 @@ @Entity @Getter +@Table(name = "tbl_article_like") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class ArticleLike extends AbstractTimeStamp { diff --git a/src/main/java/HookKiller/server/board/entity/Board.java b/src/main/java/HookKiller/server/board/entity/Board.java index 297af04..2d3175a 100644 --- a/src/main/java/HookKiller/server/board/entity/Board.java +++ b/src/main/java/HookKiller/server/board/entity/Board.java @@ -1,12 +1,18 @@ package HookKiller.server.board.entity; import HookKiller.server.board.type.BoardType; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import lombok.RequiredArgsConstructor; import java.util.List; @@ -19,14 +25,14 @@ @Entity @Getter -@Table(name = "Board") -@RequiredArgsConstructor +@Table(name = "tb1_board") +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Board { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @OneToMany + @OneToMany(mappedBy = "board") private List
article; @NotNull diff --git a/src/main/java/HookKiller/server/board/entity/Reply.java b/src/main/java/HookKiller/server/board/entity/Reply.java index f86e1c4..a1b657f 100644 --- a/src/main/java/HookKiller/server/board/entity/Reply.java +++ b/src/main/java/HookKiller/server/board/entity/Reply.java @@ -1,11 +1,18 @@ package HookKiller.server.board.entity; import HookKiller.server.common.AbstractTimeStamp; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import org.joda.time.DateTime; import java.util.List; @@ -23,6 +30,7 @@ @Entity @Getter +@Table(name = "tbl_reply") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Reply extends AbstractTimeStamp { @@ -33,11 +41,11 @@ public class Reply extends AbstractTimeStamp { @JoinColumn(name="article_id") private Article article; - @OneToMany + @OneToMany(mappedBy = "reply") private List replyContent; private String orgReplyLanguage; private boolean isDeleted; - private Long createdUser; - private Long updatedUser; + private Long createdUserId; + private Long updatedUserId; } diff --git a/src/main/java/HookKiller/server/board/entity/ReplyContent.java b/src/main/java/HookKiller/server/board/entity/ReplyContent.java index 424e872..0d021a2 100644 --- a/src/main/java/HookKiller/server/board/entity/ReplyContent.java +++ b/src/main/java/HookKiller/server/board/entity/ReplyContent.java @@ -1,7 +1,16 @@ package HookKiller.server.board.entity; -import HookKiller.server.common.AbstractTimeStamp; -import jakarta.persistence.*; +import HookKiller.server.board.type.BoardType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.Lob; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -13,6 +22,7 @@ @Entity @Getter +@Table(name = "tbl_reply_content") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class ReplyContent { @@ -23,7 +33,7 @@ public class ReplyContent { @JoinColumn(name="reply_id") private Reply reply; - private String language; + private BoardType language; @Column @Lob diff --git a/src/main/java/HookKiller/server/board/exception/ArticleContentNotFoundException.java b/src/main/java/HookKiller/server/board/exception/ArticleContentNotFoundException.java new file mode 100644 index 0000000..af2dd5d --- /dev/null +++ b/src/main/java/HookKiller/server/board/exception/ArticleContentNotFoundException.java @@ -0,0 +1,13 @@ +package HookKiller.server.board.exception; + +import HookKiller.server.common.exception.BaseException; + +public class ArticleContentNotFoundException extends BaseException { + + public static final BaseException EXCEPTION = new ArticleContentNotFoundException(); + + private ArticleContentNotFoundException() { + super(BoardException.ARTICLE_CONTENT_NOT_FOUND_ERROR); + } + +} diff --git a/src/main/java/HookKiller/server/board/exception/BaseErrorCode.java b/src/main/java/HookKiller/server/board/exception/BaseErrorCode.java deleted file mode 100644 index 7dba316..0000000 --- a/src/main/java/HookKiller/server/board/exception/BaseErrorCode.java +++ /dev/null @@ -1,9 +0,0 @@ -package HookKiller.server.board.exception; - -import HookKiller.server.common.dto.ErrorDetail; - -public interface BaseErrorCode { - - ErrorDetail getErrorDetail(); -} - diff --git a/src/main/java/HookKiller/server/board/exception/BaseException.java b/src/main/java/HookKiller/server/board/exception/BaseException.java deleted file mode 100644 index 99ba425..0000000 --- a/src/main/java/HookKiller/server/board/exception/BaseException.java +++ /dev/null @@ -1,17 +0,0 @@ -package HookKiller.server.board.exception; - - -import HookKiller.server.common.dto.ErrorDetail; -import lombok.AllArgsConstructor; -import lombok.Getter; - -@Getter -@AllArgsConstructor -public class BaseException extends RuntimeException { - - private BaseErrorCode errorCode; - - public ErrorDetail getErrorDetail() { - return this.errorCode.getErrorDetail(); - } -} diff --git a/src/main/java/HookKiller/server/board/exception/BoardException.java b/src/main/java/HookKiller/server/board/exception/BoardException.java new file mode 100644 index 0000000..92c0e27 --- /dev/null +++ b/src/main/java/HookKiller/server/board/exception/BoardException.java @@ -0,0 +1,26 @@ +package HookKiller.server.board.exception; + + +import HookKiller.server.common.dto.ErrorDetail; +import HookKiller.server.common.exception.BaseErrorCode; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import static org.springframework.http.HttpStatus.*; + +@Getter +@AllArgsConstructor +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", "해당 게시글을 찾을 수 없습니다."); + + private final Integer statusCode; + private final String errorCode; + private final String reason; + + @Override + public ErrorDetail getErrorDetail() { + return ErrorDetail.of(statusCode, errorCode, reason); + } +} diff --git a/src/main/java/HookKiller/server/board/exception/BoardNotFoundException.java b/src/main/java/HookKiller/server/board/exception/BoardNotFoundException.java new file mode 100644 index 0000000..f5f0d86 --- /dev/null +++ b/src/main/java/HookKiller/server/board/exception/BoardNotFoundException.java @@ -0,0 +1,13 @@ +package HookKiller.server.board.exception; + +import HookKiller.server.common.exception.BaseException; + +public class BoardNotFoundException extends BaseException { + + public static final BaseException EXCEPTION = new BoardNotFoundException(); + + private BoardNotFoundException() { + super(BoardException.BOARD_NOT_FOUND_ERROR); + } + +} diff --git a/src/main/java/HookKiller/server/board/exception/ExampleException.java b/src/main/java/HookKiller/server/board/exception/ExampleException.java deleted file mode 100644 index f9e9c5e..0000000 --- a/src/main/java/HookKiller/server/board/exception/ExampleException.java +++ /dev/null @@ -1,10 +0,0 @@ -package HookKiller.server.board.exception; - -public class ExampleException extends BaseException { - - public static final BaseException EXCEPTION = new ExampleException(); - - private ExampleException() { - super(GlobalException.EXAMPLE_ERROR); - } -} diff --git a/src/main/java/HookKiller/server/board/exception/GlobalException.java b/src/main/java/HookKiller/server/board/exception/GlobalException.java deleted file mode 100644 index a326550..0000000 --- a/src/main/java/HookKiller/server/board/exception/GlobalException.java +++ /dev/null @@ -1,27 +0,0 @@ -package HookKiller.server.board.exception; - -import HookKiller.server.common.dto.ErrorDetail; -import lombok.AllArgsConstructor; -import lombok.Getter; - -import static org.springframework.http.HttpStatus.BAD_REQUEST; -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; - -@Getter -@AllArgsConstructor -public enum GlobalException implements BaseErrorCode { - EXAMPLE_ERROR(BAD_REQUEST.value(), "400-0", "에러 예시 입니다."), - METHOD_ARGUMENT_ERROR( - BAD_REQUEST.value(), "400-1", "메서드 인자가 유효하지 않거나 @Valid를 통과하지 못하여 발생하는 예외입니다."), - INTERNAL_SERVER_ERRORS(INTERNAL_SERVER_ERROR.value(), "500-1", "서버 내부 오류입니다."); - - - private final Integer statusCode; - private final String errorCode; - private final String reason; - - @Override - public ErrorDetail getErrorDetail() { - return ErrorDetail.of(statusCode, errorCode, reason); - } -} diff --git a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java index 411a5b0..da3f888 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java @@ -2,13 +2,12 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.ArticleContent; +import HookKiller.server.board.type.BoardType; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; import java.util.Optional; -@Repository public interface ArticleContentRepository extends JpaRepository { - public Optional findByArticleAndLanguage(Article article, String language); + public Optional findByArticleAndLanguage(Article article, BoardType language); } diff --git a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java index 93cfdb7..c63858c 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java @@ -3,11 +3,10 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.Board; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; import java.util.List; -@Repository + public interface ArticleRepository extends JpaRepository { public List
findAllByBoard(Board board); diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java index 146c0d9..d603eca 100644 --- a/src/main/java/HookKiller/server/board/repository/BoardRepository.java +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -2,7 +2,6 @@ import HookKiller.server.board.entity.Board; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; /** @@ -11,6 +10,6 @@ * Spring Data JPA를 사용하여 기본 CRUD 작업을 자동으로 처리합니다. */ -@Repository public interface BoardRepository extends JpaRepository { + } diff --git a/src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java b/src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java index 25d2a4b..2ed9d65 100644 --- a/src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ReplyContentRepository.java @@ -2,8 +2,6 @@ import HookKiller.server.board.entity.ReplyContent; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -@Repository public interface ReplyContentRepository extends JpaRepository { } diff --git a/src/main/java/HookKiller/server/board/repository/ReplyRepository.java b/src/main/java/HookKiller/server/board/repository/ReplyRepository.java index c79b063..0a42bfb 100644 --- a/src/main/java/HookKiller/server/board/repository/ReplyRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ReplyRepository.java @@ -2,8 +2,6 @@ import HookKiller.server.board.entity.Reply; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; -@Repository public interface ReplyRepository extends JpaRepository { } diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index 834b44d..e512e81 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -1,15 +1,21 @@ package HookKiller.server.board.service; import HookKiller.server.board.dto.ArticleDto; +import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.Board; +import HookKiller.server.board.exception.ArticleContentNotFoundException; +import HookKiller.server.board.exception.BoardNotFoundException; import HookKiller.server.board.repository.ArticleContentRepository; import HookKiller.server.board.repository.ArticleRepository; import HookKiller.server.board.repository.BoardRepository; -import HookKiller.server.common.exception.ExampleException; +import HookKiller.server.board.type.BoardType; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; + @Service @RequiredArgsConstructor public class ArticleService { @@ -18,13 +24,19 @@ public class ArticleService { private final ArticleRepository articleRepository; private final ArticleContentRepository articleContentRepository; - public List getArticleList(Long boardId, String language) { + public List getArticleList(Long boardId, BoardType language) { // boardId로 board에 해당하는 Article들을 모두 뽑아온다 - // Article들 하나하나마다에 해당하는 ArticleContent랑 같이 DTO로 변환한다 - // 변환된 DTO들을 모아놓은 list를 리턴한다 - return articleRepository - .findAllByBoard(boardRepository.findById(boardId).orElseThrow(() -> ExampleException.EXCEPTION)) - .stream().map(article -> ArticleDto.of(article, articleContentRepository.findByArticleAndLanguage(article, language).orElseThrow(() -> ExampleException.EXCEPTION))) + Board board = boardRepository.findById(boardId).orElseThrow(()-> BoardNotFoundException.EXCEPTION); + List
articleList = articleRepository.findAllByBoard(board); + + // Article들 하나하나 마다에 해당하는 ArticleContent랑 같이 DTO로 변환한다 + List articleDtoList = new ArrayList<>(); + + return articleList.stream() + .map(article -> + ArticleDto.of(article, articleContentRepository.findByArticleAndLanguage(article, language) + .orElseThrow(()-> ArticleContentNotFoundException.EXCEPTION))) .toList(); } + } diff --git a/src/main/java/HookKiller/server/board/service/BoardService.java b/src/main/java/HookKiller/server/board/service/BoardService.java deleted file mode 100644 index 6edd697..0000000 --- a/src/main/java/HookKiller/server/board/service/BoardService.java +++ /dev/null @@ -1,26 +0,0 @@ -package HookKiller.server.board.service; - -import HookKiller.server.board.dto.BoardDto; -import HookKiller.server.board.entity.Board; -import HookKiller.server.board.repository.BoardRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -@Service -@RequiredArgsConstructor -public class BoardService { - - private final BoardRepository boardRepository; - - public List getBoardList() { - List boardList = boardRepository.findAll(); - List boardDtoList = new ArrayList<>(); -// boardDtoList = boardList.stream().map().toList(); - return boardDtoList; - } -} From 7dfd2ceeac22e47b742f6d52b47e2c160d4fd8f8 Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Wed, 11 Oct 2023 14:07:53 +0900 Subject: [PATCH 08/15] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D=20=EC=A7=84=ED=96=89=EC=A4=91,=20=EC=9E=84?= =?UTF-8?q?=EC=8B=9C=20=EC=BB=A4=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/ArticleController.java | 18 +++-- ...ArticleDto.java => ArticleRequestDto.java} | 31 +++++---- .../board/dto/PostArticleRequestDto.java | 30 ++++++++ .../server/board/entity/Article.java | 69 +++++++++++-------- .../HookKiller/server/board/entity/Board.java | 10 +-- .../server/board/service/ArticleService.java | 28 ++++++-- .../server/board/type/ArticleStatus.java | 13 ++++ 7 files changed, 143 insertions(+), 56 deletions(-) rename src/main/java/HookKiller/server/board/dto/{ArticleDto.java => ArticleRequestDto.java} (61%) create mode 100644 src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java create mode 100644 src/main/java/HookKiller/server/board/type/ArticleStatus.java diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index 72992b5..8de61ac 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -1,11 +1,11 @@ package HookKiller.server.board.controller; -import HookKiller.server.board.dto.ArticleDto; -import HookKiller.server.board.entity.Board; +import HookKiller.server.board.dto.ArticleRequestDto; +import HookKiller.server.board.dto.PostArticleRequestDto; +import HookKiller.server.board.repository.ArticleContentRepository; import HookKiller.server.board.service.ArticleService; import HookKiller.server.board.type.BoardType; import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -18,12 +18,22 @@ public class ArticleController { private final ArticleService articleService; + /** + * 게시글 조회 + */ @GetMapping("/{boardId}") - public List getArticleList(@PathVariable Long boardId, HttpServletRequest request) { + public List getArticleList(@PathVariable Long boardId, HttpServletRequest request) { BoardType language = BoardType.valueOf(request.getHeader("language")); return articleService.getArticleList(boardId, language); } + /** + * 게시글 등록 + */ + @PostMapping + public ArticleRequestDto createArticle(@RequestBody PostArticleRequestDto articleDto) { + return articleService.createArticle(articleDto); + } } diff --git a/src/main/java/HookKiller/server/board/dto/ArticleDto.java b/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java similarity index 61% rename from src/main/java/HookKiller/server/board/dto/ArticleDto.java rename to src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java index 12b4a42..5dec9fa 100644 --- a/src/main/java/HookKiller/server/board/dto/ArticleDto.java +++ b/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java @@ -2,7 +2,8 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.ArticleContent; -import HookKiller.server.board.type.BoardType; +import HookKiller.server.board.type.ArticleStatus; +import HookKiller.server.common.type.LanguageType; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; import jakarta.persistence.Lob; @@ -13,29 +14,33 @@ @Getter @Builder -public class ArticleDto { +public class ArticleRequestDto { + private Long boardId; private Long articleId; - private String title; - - @Lob - private String content; + @Enumerated(EnumType.STRING) + private LanguageType orgArticleLanguage; + @Enumerated(EnumType.STRING) + private ArticleStatus status; + private int likeCount; private Timestamp createdAt; private Long createdUserId; - private int likeCount; - - private Long boardId; + private Timestamp updatedAt; + private Long updatedUserId; + private Long contentId; @Enumerated(EnumType.STRING) - private BoardType boardType; + private LanguageType contentLanguage; + private String title; + @Lob + private String content; - public static ArticleDto of(Article article, ArticleContent articleContent) { - return ArticleDto.builder() + public static ArticleRequestDto of(Article article, ArticleContent articleContent) { + return ArticleRequestDto.builder() .articleId(article.getId()) .title(articleContent.getTitle()) .content(articleContent.getContent()) .createdAt(article.getCreateAt()) - .createdUserId(article.getCreatedUserId()) .likeCount(article.getLikeCount()) .build(); } diff --git a/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java b/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java new file mode 100644 index 0000000..959f6d0 --- /dev/null +++ b/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java @@ -0,0 +1,30 @@ +package HookKiller.server.board.dto; + +import HookKiller.server.common.type.LanguageType; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.Lob; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.*; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class PostArticleRequestDto { + @NotNull(message = "게시판 ID는 필수 입니다.") + private Long boardId; + + @NotNull(message = "원본 언어는 필수 선택 하셔야 합니다.") + @Enumerated(EnumType.STRING) + private LanguageType orgArticleLanguage; + + @NotEmpty(message = "제목이 입력되지 않았습니다.") + private String title; + + @NotEmpty(message = "내용이 입력되지 않았습니다.") + @Lob + private String content; +} diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index a6fe4e9..7482230 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -1,19 +1,17 @@ package HookKiller.server.board.entity; +import HookKiller.server.board.type.ArticleStatus; import HookKiller.server.common.AbstractTimeStamp; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; +import HookKiller.server.common.type.LanguageType; +import HookKiller.server.user.entity.User; +import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.ArrayList; import java.util.List; /** @@ -34,28 +32,45 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Article extends AbstractTimeStamp { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name="board_id") - private Board board; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "board_id") + private Board board; - @OneToMany(mappedBy = "article") - private List ArticleLike; + @OneToMany(mappedBy = "article") + private List ArticleLike = new ArrayList<>(); - @OneToMany(mappedBy = "article") - private List articleContent; + @OneToMany(mappedBy = "article") + private List articleContent = new ArrayList<>(); - @OneToMany(mappedBy = "article") - private List reply; + @OneToMany(mappedBy = "article") + private List reply = new ArrayList<>(); - private String orgArticleLanguage; - private String articleType; - private String status; - private int likeCount; - private boolean isDeleted; - private Long createdUserId; - private Long updatedUserId; + @NotNull + private LanguageType orgArticleLanguage; + @NotNull + private ArticleStatus articleStatus; + + private int likeCount; + + @NotNull + @ManyToOne(fetch = FetchType.EAGER) + private User createdUser; + + @NotNull + @ManyToOne(fetch = FetchType.EAGER) + private User updatedUser; + + @Builder + public Article(Board board, LanguageType orgArticleLanguage, ArticleStatus articleStatus, User createdUser, User updatedUser) { + this.board = board; + this.articleStatus = articleStatus; + this.orgArticleLanguage = orgArticleLanguage; + this.createdUser = createdUser; + this.updatedUser = updatedUser; + } } diff --git a/src/main/java/HookKiller/server/board/entity/Board.java b/src/main/java/HookKiller/server/board/entity/Board.java index 2d3175a..d49fee2 100644 --- a/src/main/java/HookKiller/server/board/entity/Board.java +++ b/src/main/java/HookKiller/server/board/entity/Board.java @@ -1,14 +1,7 @@ package HookKiller.server.board.entity; import HookKiller.server.board.type.BoardType; -import jakarta.persistence.Entity; -import jakarta.persistence.EnumType; -import jakarta.persistence.Enumerated; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; +import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Getter; @@ -30,6 +23,7 @@ public class Board { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name="board_id") private Long id; @OneToMany(mappedBy = "board") diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index e512e81..c93dfd7 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -1,6 +1,7 @@ package HookKiller.server.board.service; -import HookKiller.server.board.dto.ArticleDto; +import HookKiller.server.board.dto.ArticleRequestDto; +import HookKiller.server.board.dto.PostArticleRequestDto; import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.Board; import HookKiller.server.board.exception.ArticleContentNotFoundException; @@ -8,9 +9,11 @@ import HookKiller.server.board.repository.ArticleContentRepository; import HookKiller.server.board.repository.ArticleRepository; import HookKiller.server.board.repository.BoardRepository; +import HookKiller.server.board.type.ArticleStatus; import HookKiller.server.board.type.BoardType; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @@ -24,19 +27,36 @@ public class ArticleService { private final ArticleRepository articleRepository; private final ArticleContentRepository articleContentRepository; - public List getArticleList(Long boardId, BoardType language) { + public List getArticleList(Long boardId, BoardType language) { // boardId로 board에 해당하는 Article들을 모두 뽑아온다 Board board = boardRepository.findById(boardId).orElseThrow(()-> BoardNotFoundException.EXCEPTION); List
articleList = articleRepository.findAllByBoard(board); // Article들 하나하나 마다에 해당하는 ArticleContent랑 같이 DTO로 변환한다 - List articleDtoList = new ArrayList<>(); + List articleDtoList = new ArrayList<>(); return articleList.stream() .map(article -> - ArticleDto.of(article, articleContentRepository.findByArticleAndLanguage(article, language) + ArticleRequestDto.of(article, articleContentRepository + .findByArticleAndLanguage(article, language) .orElseThrow(()-> ArticleContentNotFoundException.EXCEPTION))) .toList(); } + @Transactional + public ArticleRequestDto createArticle(PostArticleRequestDto postArticleRequestDto) { + Board board = boardRepository.findById(postArticleRequestDto.getBoardId()) + .orElseThrow(() -> BoardNotFoundException.EXCEPTION); + + Article.builder() + .board(board) + .articleStatus(ArticleStatus.PUBLIC) + .createdUser(null) + .updatedUser(null) + .build(); + return null; + } + + + } diff --git a/src/main/java/HookKiller/server/board/type/ArticleStatus.java b/src/main/java/HookKiller/server/board/type/ArticleStatus.java new file mode 100644 index 0000000..853cd00 --- /dev/null +++ b/src/main/java/HookKiller/server/board/type/ArticleStatus.java @@ -0,0 +1,13 @@ +package HookKiller.server.board.type; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum ArticleStatus { + PUBLIC("공개상태"), + DELETE("삭제처리"); + + private final String typeName; +} From de9352f9e3a97d84e8c8eb6b2826ac2795c7e5ca Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Wed, 11 Oct 2023 15:02:45 +0900 Subject: [PATCH 09/15] =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EB=93=B1?= =?UTF-8?q?=EB=A1=9D,=20=EC=A1=B0=ED=9A=8C,=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/ArticleController.java | 21 ++++++- .../server/board/entity/Article.java | 10 +++- .../server/board/entity/ArticleContent.java | 19 +++++-- .../board/repository/ArticleRepository.java | 3 + .../board/service/ArticleContentService.java | 57 +++++++++++++++++++ .../server/board/service/ArticleService.java | 32 ++++++++--- .../server/common/type/LanguageType.java | 6 +- 7 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 src/main/java/HookKiller/server/board/service/ArticleContentService.java diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index 8de61ac..527d969 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -3,20 +3,25 @@ import HookKiller.server.board.dto.ArticleRequestDto; import HookKiller.server.board.dto.PostArticleRequestDto; import HookKiller.server.board.repository.ArticleContentRepository; +import HookKiller.server.board.service.ArticleContentService; import HookKiller.server.board.service.ArticleService; import HookKiller.server.board.type.BoardType; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; +@Slf4j @RestController @RequestMapping("/articles") @RequiredArgsConstructor public class ArticleController { private final ArticleService articleService; + private final ArticleContentService articleContentService; /** * 게시글 조회 @@ -32,8 +37,20 @@ public List getArticleList(@PathVariable Long boardId, HttpSe * 게시글 등록 */ @PostMapping - public ArticleRequestDto createArticle(@RequestBody PostArticleRequestDto articleDto) { - return articleService.createArticle(articleDto); + public ResponseEntity createArticle(@RequestBody PostArticleRequestDto requestDto) { + articleContentService.createContent( + requestDto, articleService.createArticle(requestDto) + ); + return ResponseEntity.ok("Article Create Success"); + } + + /** + * 게시물 삭제 + */ + @DeleteMapping("/{articleId}") + public ResponseEntity deleteArticle(@PathVariable Long articleId) { + articleService.deleteArticle(articleId); + return ResponseEntity.ok("삭제처리가 완료되었습니다."); } } diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index 7482230..bf973e4 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -40,13 +40,13 @@ public class Article extends AbstractTimeStamp { @JoinColumn(name = "board_id") private Board board; - @OneToMany(mappedBy = "article") + @OneToMany(mappedBy = "article", fetch = FetchType.LAZY) private List ArticleLike = new ArrayList<>(); - @OneToMany(mappedBy = "article") + @OneToMany(mappedBy = "article", fetch = FetchType.LAZY) private List articleContent = new ArrayList<>(); - @OneToMany(mappedBy = "article") + @OneToMany(mappedBy = "article", fetch = FetchType.LAZY) private List reply = new ArrayList<>(); @NotNull @@ -65,6 +65,10 @@ public class Article extends AbstractTimeStamp { @ManyToOne(fetch = FetchType.EAGER) private User updatedUser; + public void updateStatus(String status) { + articleStatus = ArticleStatus.valueOf(status); + } + @Builder public Article(Board board, LanguageType orgArticleLanguage, ArticleStatus articleStatus, User createdUser, User updatedUser) { this.board = board; diff --git a/src/main/java/HookKiller/server/board/entity/ArticleContent.java b/src/main/java/HookKiller/server/board/entity/ArticleContent.java index a5ea46d..2e0dd7e 100644 --- a/src/main/java/HookKiller/server/board/entity/ArticleContent.java +++ b/src/main/java/HookKiller/server/board/entity/ArticleContent.java @@ -1,8 +1,7 @@ package HookKiller.server.board.entity; -import HookKiller.server.board.type.BoardType; -import jakarta.persistence.Column; +import HookKiller.server.common.type.LanguageType; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -14,7 +13,9 @@ import jakarta.persistence.Lob; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; +import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -36,16 +37,26 @@ public class ArticleContent { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name="article_id") private Article article; @Enumerated(EnumType.STRING) - private BoardType language; + private LanguageType language; + + @NotNull private String title; - @Column + @NotNull @Lob private String content; + @Builder + public ArticleContent(Article article, LanguageType language, String title, String content) { + this.article = article; + this.language = language; + this.title = title; + this.content = content; + } } diff --git a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java index c63858c..4b842e2 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java @@ -10,4 +10,7 @@ public interface ArticleRepository extends JpaRepository { public List
findAllByBoard(Board board); + + List
findAllByBoardAndStatus(Board board, String status); + } diff --git a/src/main/java/HookKiller/server/board/service/ArticleContentService.java b/src/main/java/HookKiller/server/board/service/ArticleContentService.java new file mode 100644 index 0000000..00c8d37 --- /dev/null +++ b/src/main/java/HookKiller/server/board/service/ArticleContentService.java @@ -0,0 +1,57 @@ +package HookKiller.server.board.service; + +import HookKiller.server.board.dto.PostArticleRequestDto; +import HookKiller.server.board.entity.Article; +import HookKiller.server.board.entity.ArticleContent; +import HookKiller.server.board.repository.ArticleContentRepository; +import HookKiller.server.common.type.LanguageType; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@Slf4j +@Service +@RequiredArgsConstructor +public class ArticleContentService { + private final ArticleContentRepository articleContentRepository; + + public void createContent(PostArticleRequestDto requestDto, Article article) { + + List targetLanguageList = Arrays.stream(LanguageType.values()) + .filter(languageType -> !languageType.equals(requestDto.getOrgArticleLanguage())) + .toList(); + + List articleContentList = new ArrayList<>( + targetLanguageList.stream().map( + 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())) + .build() + ).toList() + ); + articleContentList.add( + ArticleContent.builder() + .article(article) + .language(requestDto.getOrgArticleLanguage()) + .title(requestDto.getTitle()) + .content(requestDto.getContent()) + .build() + ); + articleContentRepository.saveAll(articleContentList); + } + + protected String getTranslatePapagoTextArticleContent(String source, String target, String content) { + return null; + } + + protected String getTranslatePapagoHTMLArticleContent(String source, String target, String content) { + return null; + } + +} diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index c93dfd7..6c8ef13 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -11,18 +11,23 @@ import HookKiller.server.board.repository.BoardRepository; import HookKiller.server.board.type.ArticleStatus; import HookKiller.server.board.type.BoardType; +import HookKiller.server.common.util.UserUtils; +import HookKiller.server.user.entity.User; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.catalina.security.SecurityUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; - +@Slf4j @Service @RequiredArgsConstructor public class ArticleService { + private final UserUtils userUtils; private final BoardRepository boardRepository; private final ArticleRepository articleRepository; private final ArticleContentRepository articleContentRepository; @@ -30,7 +35,7 @@ public class ArticleService { public List getArticleList(Long boardId, BoardType language) { // boardId로 board에 해당하는 Article들을 모두 뽑아온다 Board board = boardRepository.findById(boardId).orElseThrow(()-> BoardNotFoundException.EXCEPTION); - List
articleList = articleRepository.findAllByBoard(board); + List
articleList = articleRepository.findAllByBoardAndStatus(board, "PUBLIC"); // Article들 하나하나 마다에 해당하는 ArticleContent랑 같이 DTO로 변환한다 List articleDtoList = new ArrayList<>(); @@ -43,18 +48,27 @@ public List getArticleList(Long boardId, BoardType language) .toList(); } - @Transactional - public ArticleRequestDto createArticle(PostArticleRequestDto postArticleRequestDto) { + public Article createArticle(PostArticleRequestDto postArticleRequestDto) { Board board = boardRepository.findById(postArticleRequestDto.getBoardId()) .orElseThrow(() -> BoardNotFoundException.EXCEPTION); - Article.builder() + User requestUser = userUtils.getUser(); + return articleRepository.save(Article.builder() .board(board) .articleStatus(ArticleStatus.PUBLIC) - .createdUser(null) - .updatedUser(null) - .build(); - return null; + .orgArticleLanguage(postArticleRequestDto.getOrgArticleLanguage()) + .createdUser(requestUser) + .updatedUser(requestUser) + .build() + ); + } + + @Transactional + public void deleteArticle(Long articleId) { + articleRepository + .findById(articleId) + .orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION) + .updateStatus("DELETE"); } diff --git a/src/main/java/HookKiller/server/common/type/LanguageType.java b/src/main/java/HookKiller/server/common/type/LanguageType.java index 048cac8..4b613d4 100644 --- a/src/main/java/HookKiller/server/common/type/LanguageType.java +++ b/src/main/java/HookKiller/server/common/type/LanguageType.java @@ -1,8 +1,12 @@ package HookKiller.server.common.type; +import lombok.AllArgsConstructor; import lombok.Getter; @Getter +@AllArgsConstructor public enum LanguageType { - KO, EN, CN, JP; + KO("ko"), EN("en"), CN("zh-CN"), JP("ja"); + + private String languageCode; } From 56d8b69d1064f6b2a6efcb9df6bd950f4e4a6483 Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Wed, 11 Oct 2023 19:15:56 +0900 Subject: [PATCH 10/15] papago api --- build.gradle | 3 ++ .../board/controller/ArticleController.java | 12 ++++++- .../server/board/entity/Article.java | 1 + .../board/service/ArticleContentService.java | 1 + .../server/board/service/ArticleService.java | 11 ++++++ .../controller/TranslateController.java | 19 ++++++++++ .../common/dto/TranslateResponseDto.java | 29 +++++++++++++++ .../common/service/TranslateService.java | 35 +++++++++++++++++++ 8 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/main/java/HookKiller/server/common/controller/TranslateController.java create mode 100644 src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java create mode 100644 src/main/java/HookKiller/server/common/service/TranslateService.java diff --git a/build.gradle b/build.gradle index e350f7f..e3ec0cd 100644 --- a/build.gradle +++ b/build.gradle @@ -72,6 +72,9 @@ dependencies { // IAMPORT implementation 'com.github.iamport:iamport-rest-client-java:0.2.23' + // WebClient + implementation "org.springframework.boot:spring-boot-starter-webflux" + // Lombok And Annotation Processor compileOnly 'org.projectlombok:lombok' diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index 527d969..af6dd65 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -2,7 +2,6 @@ import HookKiller.server.board.dto.ArticleRequestDto; import HookKiller.server.board.dto.PostArticleRequestDto; -import HookKiller.server.board.repository.ArticleContentRepository; import HookKiller.server.board.service.ArticleContentService; import HookKiller.server.board.service.ArticleService; import HookKiller.server.board.type.BoardType; @@ -44,6 +43,17 @@ public ResponseEntity createArticle(@RequestBody PostArticleRequestDto r return ResponseEntity.ok("Article Create Success"); } + /** + * 게시물 수정 + */ +// @PutMapping("/{articleId}") +// public ResponseEntity updateArticle(@RequestBody PostArticleRequestDto requestDto) { +// articleContentService.updateArticle( +// requestDto, articleService.updateArticle(requestDto) +// ); +// return ResponseEntity.ok("Article Update Success"); +// } + /** * 게시물 삭제 */ diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index bf973e4..107a547 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -53,6 +53,7 @@ public class Article extends AbstractTimeStamp { private LanguageType orgArticleLanguage; @NotNull + @Enumerated(EnumType.STRING) private ArticleStatus articleStatus; private int likeCount; diff --git a/src/main/java/HookKiller/server/board/service/ArticleContentService.java b/src/main/java/HookKiller/server/board/service/ArticleContentService.java index 00c8d37..278aea1 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleContentService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleContentService.java @@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index 6c8ef13..57ebe07 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -63,6 +63,16 @@ public Article createArticle(PostArticleRequestDto postArticleRequestDto) { ); } +// public Article updateArticle(PostArticleRequestDto postArticleRequestDto) { +// Board board = boardRepository.findById(postArticleRequestDto.getBoardId()) +// .orElseThrow(() -> BoardNotFoundException.EXCEPTION); +// +// User requestUser = userUtils.getUser(); +// return articleRepository +// +// +// } + @Transactional public void deleteArticle(Long articleId) { articleRepository @@ -73,4 +83,5 @@ public void deleteArticle(Long articleId) { + } diff --git a/src/main/java/HookKiller/server/common/controller/TranslateController.java b/src/main/java/HookKiller/server/common/controller/TranslateController.java new file mode 100644 index 0000000..5fcc869 --- /dev/null +++ b/src/main/java/HookKiller/server/common/controller/TranslateController.java @@ -0,0 +1,19 @@ +package HookKiller.server.common.controller; + +import HookKiller.server.common.dto.TranslateResponseDto; +import HookKiller.server.common.service.TranslateService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +@RequiredArgsConstructor +public class TranslateController { + private final TranslateService translateService; + + @PostMapping("/api/translate") + public TranslateResponseDto.Result naverPapagoTranslate(String source, String target, String text) { + return translateService.naverPapagoTranslate(source, target, text); + } +} diff --git a/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java b/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java new file mode 100644 index 0000000..31ab6b2 --- /dev/null +++ b/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java @@ -0,0 +1,29 @@ +package HookKiller.server.common.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class TranslateResponseDto { + + private Message message; + + @AllArgsConstructor + @Getter + @NoArgsConstructor + public static class Message { + private Result result; + } + + @AllArgsConstructor + @Getter + @NoArgsConstructor + public static class Result { + private String srcLangType; + private String tarLangType; + private String translatedText; + } +} diff --git a/src/main/java/HookKiller/server/common/service/TranslateService.java b/src/main/java/HookKiller/server/common/service/TranslateService.java new file mode 100644 index 0000000..8ed234e --- /dev/null +++ b/src/main/java/HookKiller/server/common/service/TranslateService.java @@ -0,0 +1,35 @@ +package HookKiller.server.common.service; + +import HookKiller.server.common.dto.TranslateResponseDto; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; + +@Service +public class TranslateService { + + // @Value() + private String clientId; + // @Value() + private String clientSecret; + + public TranslateResponseDto.Result naverPapagoTranslate(String source, String target, String content) { + + WebClient webClient = WebClient.builder() + .baseUrl("https://") + .build(); + TranslateResponseDto response = webClient.post().uri( + uriBuilder -> uriBuilder.queryParam("source", source) + .queryParam("target", target) + .queryParam("text", content) + .build() + ) + .header("X-Naver-Client-Id", clientId) + .header("X-Naver-Client-Secret", clientSecret) + .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") + .retrieve() + .bodyToMono(TranslateResponseDto.class).block(); + return response.getMessage().getResult(); + } + +} + From f4f30c38189d362514888d570aaedb47dfbbfa40 Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Thu, 12 Oct 2023 15:06:35 +0900 Subject: [PATCH 11/15] =?UTF-8?q?=EA=B2=8C=EC=8B=9C=ED=8C=90=20CRUD=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/ArticleController.java | 17 +++++---- .../server/board/entity/Article.java | 3 +- .../server/board/entity/ArticleContent.java | 5 +++ .../HookKiller/server/board/entity/Board.java | 2 +- .../repository/ArticleContentRepository.java | 3 ++ .../board/service/ArticleContentService.java | 28 ++++++++++++++ .../server/board/service/ArticleService.java | 37 ++++++++++++++----- src/main/resources/application-local.yml | 2 +- 8 files changed, 77 insertions(+), 20 deletions(-) diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index af6dd65..b3c6dbf 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -25,7 +25,6 @@ public class ArticleController { /** * 게시글 조회 */ - @GetMapping("/{boardId}") public List getArticleList(@PathVariable Long boardId, HttpServletRequest request) { BoardType language = BoardType.valueOf(request.getHeader("language")); @@ -46,13 +45,15 @@ public ResponseEntity createArticle(@RequestBody PostArticleRequestDto r /** * 게시물 수정 */ -// @PutMapping("/{articleId}") -// public ResponseEntity updateArticle(@RequestBody PostArticleRequestDto requestDto) { -// articleContentService.updateArticle( -// requestDto, articleService.updateArticle(requestDto) -// ); -// return ResponseEntity.ok("Article Update Success"); -// } + @PostMapping("/{articleId}") + public ResponseEntity updateArticle(@RequestBody PostArticleRequestDto requestDto, + @PathVariable Long articleId) { + articleContentService.updateContent( + requestDto, articleService.updateArticle(articleId, requestDto) + ); + return ResponseEntity.ok("Article Create Success"); + } + /** * 게시물 삭제 diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index 107a547..1ea688b 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -71,8 +71,9 @@ public void updateStatus(String status) { } @Builder - public Article(Board board, LanguageType orgArticleLanguage, ArticleStatus articleStatus, User createdUser, User updatedUser) { + public Article(Board board, Long id, LanguageType orgArticleLanguage, ArticleStatus articleStatus, User createdUser, User updatedUser) { this.board = board; + this.id = id; this.articleStatus = articleStatus; this.orgArticleLanguage = orgArticleLanguage; this.createdUser = createdUser; diff --git a/src/main/java/HookKiller/server/board/entity/ArticleContent.java b/src/main/java/HookKiller/server/board/entity/ArticleContent.java index 2e0dd7e..16b5eb5 100644 --- a/src/main/java/HookKiller/server/board/entity/ArticleContent.java +++ b/src/main/java/HookKiller/server/board/entity/ArticleContent.java @@ -59,4 +59,9 @@ public ArticleContent(Article article, LanguageType language, String title, Stri this.title = title; this.content = content; } + + public void articleUpdate(String title, String content) { + this.title = title; + this.content = content; + } } diff --git a/src/main/java/HookKiller/server/board/entity/Board.java b/src/main/java/HookKiller/server/board/entity/Board.java index d49fee2..2292398 100644 --- a/src/main/java/HookKiller/server/board/entity/Board.java +++ b/src/main/java/HookKiller/server/board/entity/Board.java @@ -18,7 +18,7 @@ @Entity @Getter -@Table(name = "tb1_board") +@Table(name = "tbl_board") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Board { diff --git a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java index da3f888..74620f4 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java @@ -5,9 +5,12 @@ import HookKiller.server.board.type.BoardType; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface ArticleContentRepository extends JpaRepository { public Optional findByArticleAndLanguage(Article article, BoardType language); + List findAllByArticle(Article article); + } diff --git a/src/main/java/HookKiller/server/board/service/ArticleContentService.java b/src/main/java/HookKiller/server/board/service/ArticleContentService.java index 278aea1..9e64f5a 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleContentService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleContentService.java @@ -47,6 +47,34 @@ public void createContent(PostArticleRequestDto requestDto, Article article) { articleContentRepository.saveAll(articleContentList); } + public void updateContent(PostArticleRequestDto requestDto, Article article) { + // 게시물의 모든 내용을 찾습니다. + List existingContents = articleContentRepository.findAllByArticle(article); + + // 원본 언어의 내용을 업데이트합니다. + existingContents.stream() + .filter(content -> content.getLanguage().equals(requestDto.getOrgArticleLanguage())) + .forEach(content -> { + content.articleUpdate(requestDto.getTitle(), requestDto.getContent()); + }); + + // 다른 언어로 번역된 내용들을 업데이트합니다. + for (LanguageType languageType : LanguageType.values()) { + if (!languageType.equals(requestDto.getOrgArticleLanguage())) { + 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())); + }); + } + } + + articleContentRepository.saveAll(existingContents); + } + + + protected String getTranslatePapagoTextArticleContent(String source, String target, String content) { return null; } diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index 57ebe07..9eb1048 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -28,6 +28,7 @@ public class ArticleService { private final UserUtils userUtils; + private final ArticleContentService articleContentService; private final BoardRepository boardRepository; private final ArticleRepository articleRepository; private final ArticleContentRepository articleContentRepository; @@ -63,15 +64,33 @@ public Article createArticle(PostArticleRequestDto postArticleRequestDto) { ); } -// public Article updateArticle(PostArticleRequestDto postArticleRequestDto) { -// Board board = boardRepository.findById(postArticleRequestDto.getBoardId()) -// .orElseThrow(() -> BoardNotFoundException.EXCEPTION); -// -// User requestUser = userUtils.getUser(); -// return articleRepository -// -// -// } + @Transactional + public Article updateArticle(Long articleId, PostArticleRequestDto postArticleRequestDto) { + + Board board = boardRepository.findById(postArticleRequestDto.getBoardId()) + .orElseThrow(() -> BoardNotFoundException.EXCEPTION); + + Article article = articleRepository.findById(articleId) + .orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION); + + User requestUser = userUtils.getUser(); + + article = Article.builder() + .id(article.getId()) + .board(board) + .orgArticleLanguage(postArticleRequestDto.getOrgArticleLanguage()) + .articleStatus(article.getArticleStatus()) + .createdUser(article.getCreatedUser()) + .updatedUser(requestUser) + .build(); + + // 게시물 내용 업데이트 + articleContentService.updateContent(postArticleRequestDto, article); + + return articleRepository.save(article); + } + + @Transactional public void deleteArticle(Long articleId) { diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 999de09..0681673 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -2,7 +2,7 @@ hook: db: url: db-it7f7-kr.vpc-pub-cdb.ntruss.com - database: jw + database: js username: hooklocal password: hooklocal1234! port: 3306 From b87863f80e33bcf0297da172399f0cccc7df1079 Mon Sep 17 00:00:00 2001 From: lgsok00 Date: Thu, 12 Oct 2023 20:32:11 +0900 Subject: [PATCH 12/15] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- build.gradle | 4 --- .../board/controller/ArticleController.java | 32 +++++++++++------ .../server/board/dto/ArticleRequestDto.java | 8 ++--- .../board/dto/PostArticleRequestDto.java | 8 +++-- .../server/board/entity/Article.java | 22 ++++++++---- .../HookKiller/server/board/entity/Board.java | 10 +++++- .../repository/ArticleContentRepository.java | 5 +-- .../board/repository/ArticleRepository.java | 5 ++- .../board/repository/BoardRepository.java | 6 +++- .../board/service/ArticleContentService.java | 9 +++-- .../server/board/service/ArticleService.java | 21 ++++------- .../controller/TranslateController.java | 19 ---------- .../common/dto/TranslateResponseDto.java | 29 --------------- .../common/service/TranslateService.java | 35 ------------------- .../server/common/type/LanguageType.java | 20 ++++++++--- src/main/resources/application-local.yml | 2 +- 17 files changed, 94 insertions(+), 144 deletions(-) delete mode 100644 src/main/java/HookKiller/server/common/controller/TranslateController.java delete mode 100644 src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java delete mode 100644 src/main/java/HookKiller/server/common/service/TranslateService.java diff --git a/.gitignore b/.gitignore index 2bff8a4..154b7c4 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,5 @@ out/ ### VS Code ### .vscode/ -.DS_Store \ No newline at end of file +.DS_Store + diff --git a/build.gradle b/build.gradle index e3ec0cd..a4f3487 100644 --- a/build.gradle +++ b/build.gradle @@ -72,10 +72,6 @@ dependencies { // IAMPORT implementation 'com.github.iamport:iamport-rest-client-java:0.2.23' - // WebClient - implementation "org.springframework.boot:spring-boot-starter-webflux" - - // Lombok And Annotation Processor compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index b3c6dbf..0f20e95 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -5,17 +5,24 @@ import HookKiller.server.board.service.ArticleContentService; import HookKiller.server.board.service.ArticleService; import HookKiller.server.board.type.BoardType; +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.*; +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("/articles") +@RequestMapping("/article") @RequiredArgsConstructor public class ArticleController { @@ -27,10 +34,18 @@ public class ArticleController { */ @GetMapping("/{boardId}") public List getArticleList(@PathVariable Long boardId, HttpServletRequest request) { - BoardType language = BoardType.valueOf(request.getHeader("language")); - return articleService.getArticleList(boardId, language); + return articleService.getArticleList(boardId, LanguageType.findTypeByRequest(request)); } + /** + * 단건 조회 + */ +// @GetMapping("/{boardId}/{articleId}") +// public ArticleRequestDto getArticle(@PathVariable Long articleId, HttpServletRequest request) { +// BoardType language = BoardType.valueOf(request.getHeader("language")); +// return articleService.getArticle(boardId, articleId, language); +// } + /** * 게시글 등록 */ @@ -45,12 +60,9 @@ public ResponseEntity createArticle(@RequestBody PostArticleRequestDto r /** * 게시물 수정 */ - @PostMapping("/{articleId}") - public ResponseEntity updateArticle(@RequestBody PostArticleRequestDto requestDto, - @PathVariable Long articleId) { - articleContentService.updateContent( - requestDto, articleService.updateArticle(articleId, requestDto) - ); + @PostMapping + public ResponseEntity updateArticle(@RequestBody PostArticleRequestDto requestDto) { + articleContentService.updateContent(requestDto, articleService.updateArticle(requestDto)); return ResponseEntity.ok("Article Create Success"); } diff --git a/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java b/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java index 5dec9fa..35f8e5d 100644 --- a/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java +++ b/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java @@ -3,6 +3,7 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.ArticleContent; import HookKiller.server.board.type.ArticleStatus; +import HookKiller.server.common.AbstractTimeStamp; import HookKiller.server.common.type.LanguageType; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; @@ -10,11 +11,9 @@ import lombok.Builder; import lombok.Getter; -import java.sql.Timestamp; - @Getter @Builder -public class ArticleRequestDto { +public class ArticleRequestDto extends AbstractTimeStamp { private Long boardId; private Long articleId; @@ -23,9 +22,7 @@ public class ArticleRequestDto { @Enumerated(EnumType.STRING) private ArticleStatus status; private int likeCount; - private Timestamp createdAt; private Long createdUserId; - private Timestamp updatedAt; private Long updatedUserId; private Long contentId; @@ -40,7 +37,6 @@ public static ArticleRequestDto of(Article article, ArticleContent articleConten .articleId(article.getId()) .title(articleContent.getTitle()) .content(articleContent.getContent()) - .createdAt(article.getCreateAt()) .likeCount(article.getLikeCount()) .build(); } diff --git a/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java b/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java index 959f6d0..9866a7f 100644 --- a/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java +++ b/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java @@ -6,10 +6,12 @@ import jakarta.persistence.Lob; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; @Getter -@Setter @Builder @NoArgsConstructor @AllArgsConstructor @@ -17,6 +19,8 @@ public class PostArticleRequestDto { @NotNull(message = "게시판 ID는 필수 입니다.") private Long boardId; + private Long articleId; + @NotNull(message = "원본 언어는 필수 선택 하셔야 합니다.") @Enumerated(EnumType.STRING) private LanguageType orgArticleLanguage; diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index 1ea688b..e3b8ab2 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -4,7 +4,17 @@ import HookKiller.server.common.AbstractTimeStamp; import HookKiller.server.common.type.LanguageType; import HookKiller.server.user.entity.User; -import jakarta.persistence.*; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Builder; @@ -15,14 +25,13 @@ import java.util.List; /** + * id : PK 키 * orgArticleLanguage : 원본으로 작성된 언어 타입. KOR:한국어, ENG:영어, CHI:중국어, JPN:일본어 - * articleType : 게시물 종류. NOTI:공지사항, NORMAL:일반적인 게시물 - * status : 게시물 상태. WRITING:작성중, PUBLIC:공개상태, HIDING:숨김처리, DELETE:삭제처리 + * status: 게시물 상태. PUBLIC:공개, DELETE:삭제처리 * likeCount : 좋아요 갯수. * isDeleted : 게시글 삭제 여부 * createdAt : 게시글 생성일 * createdUser : 게시글 작성 사용자 ID입력 - * updatedAt : 게시글 정보 업데이트 일자 * updatedUser : 마지막에 수정한 사용자 ID입력 */ @@ -66,8 +75,8 @@ public class Article extends AbstractTimeStamp { @ManyToOne(fetch = FetchType.EAGER) private User updatedUser; - public void updateStatus(String status) { - articleStatus = ArticleStatus.valueOf(status); + public void updateStatus(ArticleStatus status) { + articleStatus = status; } @Builder @@ -79,4 +88,5 @@ public Article(Board board, Long id, LanguageType orgArticleLanguage, ArticleSta this.createdUser = createdUser; this.updatedUser = updatedUser; } + } diff --git a/src/main/java/HookKiller/server/board/entity/Board.java b/src/main/java/HookKiller/server/board/entity/Board.java index 2292398..b359dfd 100644 --- a/src/main/java/HookKiller/server/board/entity/Board.java +++ b/src/main/java/HookKiller/server/board/entity/Board.java @@ -1,7 +1,15 @@ package HookKiller.server.board.entity; import HookKiller.server.board.type.BoardType; -import jakarta.persistence.*; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Getter; diff --git a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java index 74620f4..bbfc380 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java @@ -2,14 +2,15 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.ArticleContent; -import HookKiller.server.board.type.BoardType; +import HookKiller.server.common.type.LanguageType; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; import java.util.Optional; public interface ArticleContentRepository extends JpaRepository { - public Optional findByArticleAndLanguage(Article article, BoardType language); + + Optional findByArticleAndLanguage(Article article, LanguageType language); List findAllByArticle(Article article); diff --git a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java index 4b842e2..f0de7a9 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleRepository.java @@ -2,6 +2,7 @@ import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.Board; +import HookKiller.server.board.type.ArticleStatus; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; @@ -9,8 +10,6 @@ public interface ArticleRepository extends JpaRepository { - public List
findAllByBoard(Board board); - - List
findAllByBoardAndStatus(Board board, String status); + List
findAllByBoardAndArticleStatus(Board board, ArticleStatus status); } diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java index d603eca..819cc3b 100644 --- a/src/main/java/HookKiller/server/board/repository/BoardRepository.java +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -1,8 +1,12 @@ package HookKiller.server.board.repository; +import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.Board; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; +import java.util.Optional; + /** * BoardRepository: BoardRepository 인터페이스는 Board 엔터티를 데이터베이스에 @@ -11,5 +15,5 @@ */ public interface BoardRepository extends JpaRepository { - + Optional findBoardByArticle(List
article); } diff --git a/src/main/java/HookKiller/server/board/service/ArticleContentService.java b/src/main/java/HookKiller/server/board/service/ArticleContentService.java index 9e64f5a..ada76df 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleContentService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleContentService.java @@ -8,7 +8,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import org.springframework.web.reactive.function.client.WebClient; import java.util.ArrayList; import java.util.Arrays; @@ -31,8 +30,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(getTranslatePapagoTextArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getTitle())) + // .content(getTranslatePapagoHTMLArticleContent(requestDto.getOrgArticleLanguage().getLanguageCode(), languageType.getLanguageCode(), requestDto.getContent())) .build() ).toList() ); @@ -64,8 +63,8 @@ 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())); }); } } diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index 9eb1048..e6659a9 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -10,16 +10,14 @@ import HookKiller.server.board.repository.ArticleRepository; import HookKiller.server.board.repository.BoardRepository; import HookKiller.server.board.type.ArticleStatus; -import HookKiller.server.board.type.BoardType; +import HookKiller.server.common.type.LanguageType; import HookKiller.server.common.util.UserUtils; import HookKiller.server.user.entity.User; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.catalina.security.SecurityUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; import java.util.List; @Slf4j @@ -33,13 +31,10 @@ public class ArticleService { private final ArticleRepository articleRepository; private final ArticleContentRepository articleContentRepository; - public List getArticleList(Long boardId, BoardType language) { + public List getArticleList(Long boardId, LanguageType language) { // boardId로 board에 해당하는 Article들을 모두 뽑아온다 Board board = boardRepository.findById(boardId).orElseThrow(()-> BoardNotFoundException.EXCEPTION); - List
articleList = articleRepository.findAllByBoardAndStatus(board, "PUBLIC"); - - // Article들 하나하나 마다에 해당하는 ArticleContent랑 같이 DTO로 변환한다 - List articleDtoList = new ArrayList<>(); + List
articleList = articleRepository.findAllByBoardAndArticleStatus(board, ArticleStatus.PUBLIC); return articleList.stream() .map(article -> @@ -65,12 +60,12 @@ public Article createArticle(PostArticleRequestDto postArticleRequestDto) { } @Transactional - public Article updateArticle(Long articleId, PostArticleRequestDto postArticleRequestDto) { + public Article updateArticle(PostArticleRequestDto postArticleRequestDto) { Board board = boardRepository.findById(postArticleRequestDto.getBoardId()) .orElseThrow(() -> BoardNotFoundException.EXCEPTION); - Article article = articleRepository.findById(articleId) + Article article = articleRepository.findById(postArticleRequestDto.getArticleId()) .orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION); User requestUser = userUtils.getUser(); @@ -97,10 +92,6 @@ public void deleteArticle(Long articleId) { articleRepository .findById(articleId) .orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION) - .updateStatus("DELETE"); + .updateStatus(ArticleStatus.DELETE); } - - - - } diff --git a/src/main/java/HookKiller/server/common/controller/TranslateController.java b/src/main/java/HookKiller/server/common/controller/TranslateController.java deleted file mode 100644 index 5fcc869..0000000 --- a/src/main/java/HookKiller/server/common/controller/TranslateController.java +++ /dev/null @@ -1,19 +0,0 @@ -package HookKiller.server.common.controller; - -import HookKiller.server.common.dto.TranslateResponseDto; -import HookKiller.server.common.service.TranslateService; -import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; - - -@RestController -@RequiredArgsConstructor -public class TranslateController { - private final TranslateService translateService; - - @PostMapping("/api/translate") - public TranslateResponseDto.Result naverPapagoTranslate(String source, String target, String text) { - return translateService.naverPapagoTranslate(source, target, text); - } -} diff --git a/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java b/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java deleted file mode 100644 index 31ab6b2..0000000 --- a/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java +++ /dev/null @@ -1,29 +0,0 @@ -package HookKiller.server.common.dto; - -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@AllArgsConstructor -@NoArgsConstructor -public class TranslateResponseDto { - - private Message message; - - @AllArgsConstructor - @Getter - @NoArgsConstructor - public static class Message { - private Result result; - } - - @AllArgsConstructor - @Getter - @NoArgsConstructor - public static class Result { - private String srcLangType; - private String tarLangType; - private String translatedText; - } -} diff --git a/src/main/java/HookKiller/server/common/service/TranslateService.java b/src/main/java/HookKiller/server/common/service/TranslateService.java deleted file mode 100644 index 8ed234e..0000000 --- a/src/main/java/HookKiller/server/common/service/TranslateService.java +++ /dev/null @@ -1,35 +0,0 @@ -package HookKiller.server.common.service; - -import HookKiller.server.common.dto.TranslateResponseDto; -import org.springframework.stereotype.Service; -import org.springframework.web.reactive.function.client.WebClient; - -@Service -public class TranslateService { - - // @Value() - private String clientId; - // @Value() - private String clientSecret; - - public TranslateResponseDto.Result naverPapagoTranslate(String source, String target, String content) { - - WebClient webClient = WebClient.builder() - .baseUrl("https://") - .build(); - TranslateResponseDto response = webClient.post().uri( - uriBuilder -> uriBuilder.queryParam("source", source) - .queryParam("target", target) - .queryParam("text", content) - .build() - ) - .header("X-Naver-Client-Id", clientId) - .header("X-Naver-Client-Secret", clientSecret) - .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") - .retrieve() - .bodyToMono(TranslateResponseDto.class).block(); - return response.getMessage().getResult(); - } - -} - diff --git a/src/main/java/HookKiller/server/common/type/LanguageType.java b/src/main/java/HookKiller/server/common/type/LanguageType.java index 4b613d4..9c24f09 100644 --- a/src/main/java/HookKiller/server/common/type/LanguageType.java +++ b/src/main/java/HookKiller/server/common/type/LanguageType.java @@ -1,12 +1,24 @@ package HookKiller.server.common.type; -import lombok.AllArgsConstructor; +import jakarta.servlet.http.HttpServletRequest; import lombok.Getter; +import java.util.Arrays; + @Getter -@AllArgsConstructor public enum LanguageType { - KO("ko"), EN("en"), CN("zh-CN"), JP("ja"); + KO, EN, CN, JP; + + private static final String HTTP_REQUEST_HEADER_KEY_LANGUAGE = "language"; + + public static LanguageType findType(String value) { + return (value == null || value.isEmpty()) ? + KO : Arrays.stream(LanguageType.values()) + .filter(type -> type.name().equals(value.toUpperCase())) + .findFirst().orElse(KO); + } - private String languageCode; + public static LanguageType findTypeByRequest(HttpServletRequest request) { + return request == null ? KO : findType(request.getHeader(HTTP_REQUEST_HEADER_KEY_LANGUAGE)); + } } diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 0681673..999de09 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -2,7 +2,7 @@ hook: db: url: db-it7f7-kr.vpc-pub-cdb.ntruss.com - database: js + database: jw username: hooklocal password: hooklocal1234! port: 3306 From fbc844a3327eed4c529a1ddfeba8e8cd708a62d0 Mon Sep 17 00:00:00 2001 From: lljh1992 Date: Fri, 13 Oct 2023 00:45:56 +0900 Subject: [PATCH 13/15] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=8B=9C=ED=8C=90=20?= =?UTF-8?q?CRUD=20+=20papago=20=EA=B8=B0=EB=8A=A5=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 진석 + 정훈 기능 join --- .../board/controller/ArticleController.java | 10 +-- .../board/repository/BoardRepository.java | 1 - .../server/board/service/ArticleService.java | 1 + .../controller/HealthCheckController.java | 9 +- .../server/common/dto/PapagoHtmlRequest.java | 16 ++++ .../server/common/dto/PapagoHtmlResponse.java | 15 ++++ .../server/common/dto/PapagoTextRequest.java | 20 +++++ .../server/common/dto/PapagoTextResponse.java | 30 +++++++ .../common/dto/TranslateResponseDto.java | 29 +++++++ .../common/exception/GlobalException.java | 1 + .../common/exception/NaverErrorException.java | 14 +++ .../common/service/TranslateService.java | 85 +++++++++++++++++++ .../server/common/type/LanguageType.java | 6 +- .../config/ConfigurationProperties.java | 4 +- .../NaverObjectStorageProperties.java | 1 + .../server/properties/PapagoProperties.java | 19 +++++ src/main/resources/application-local.yml | 4 +- src/main/resources/application-papago.yml | 3 + src/main/resources/application.yml | 1 + 19 files changed, 253 insertions(+), 16 deletions(-) create mode 100644 src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java create mode 100644 src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java create mode 100644 src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java create mode 100644 src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java create mode 100644 src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java create mode 100644 src/main/java/HookKiller/server/common/exception/NaverErrorException.java create mode 100644 src/main/java/HookKiller/server/common/service/TranslateService.java create mode 100644 src/main/java/HookKiller/server/properties/PapagoProperties.java create mode 100644 src/main/resources/application-papago.yml diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index 0f20e95..46b31e4 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -10,13 +10,7 @@ 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 org.springframework.web.bind.annotation.*; import java.util.List; @@ -60,7 +54,7 @@ public ResponseEntity createArticle(@RequestBody PostArticleRequestDto r /** * 게시물 수정 */ - @PostMapping + @PutMapping public ResponseEntity updateArticle(@RequestBody PostArticleRequestDto requestDto) { articleContentService.updateContent(requestDto, articleService.updateArticle(requestDto)); return ResponseEntity.ok("Article Create Success"); diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java index 819cc3b..5b97e68 100644 --- a/src/main/java/HookKiller/server/board/repository/BoardRepository.java +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -15,5 +15,4 @@ */ public interface BoardRepository extends JpaRepository { - Optional findBoardByArticle(List
article); } diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index e6659a9..949209c 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -18,6 +18,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.ArrayList; import java.util.List; @Slf4j diff --git a/src/main/java/HookKiller/server/common/controller/HealthCheckController.java b/src/main/java/HookKiller/server/common/controller/HealthCheckController.java index 9768fc5..5ed6771 100644 --- a/src/main/java/HookKiller/server/common/controller/HealthCheckController.java +++ b/src/main/java/HookKiller/server/common/controller/HealthCheckController.java @@ -1,8 +1,9 @@ package HookKiller.server.common.controller; +import HookKiller.server.common.service.TranslateService; +import HookKiller.server.common.type.LanguageType; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.GetMapping; @@ -12,11 +13,13 @@ @RestController @RequiredArgsConstructor public class HealthCheckController { - + private final TranslateService translateService; @GetMapping("/health") public ResponseEntity healthCheck(){ + translateService.naverPapagoTextTranslate(LanguageType.KO, LanguageType.JP, "안녕하세요~~"); + translateService.naverPapagoHtmlTranslate(LanguageType.KO, LanguageType.JP, "
안녕하세요. 파파고입니다.
"); + log.error("authentication : {}", SecurityContextHolder.getContext().getAuthentication().getName()); - return ResponseEntity.ok().build(); } diff --git a/src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java b/src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java new file mode 100644 index 0000000..bf89efe --- /dev/null +++ b/src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java @@ -0,0 +1,16 @@ +package HookKiller.server.common.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.*; + +@Getter +@Setter +@Builder +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class PapagoHtmlRequest { + private String source; + private String target; + private String html; +} diff --git a/src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java b/src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java new file mode 100644 index 0000000..5b76e4c --- /dev/null +++ b/src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java @@ -0,0 +1,15 @@ +package HookKiller.server.common.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.*; + +@Getter +@Builder +@ToString +@AllArgsConstructor +@NoArgsConstructor +public class PapagoHtmlResponse { + + private String result; + +} diff --git a/src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java b/src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java new file mode 100644 index 0000000..b95c3d9 --- /dev/null +++ b/src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java @@ -0,0 +1,20 @@ +package HookKiller.server.common.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.*; + +@Getter +@Setter +@Builder +@AllArgsConstructor +@NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class PapagoTextRequest { + private String source; + private String target; + private String text; + private String html; + private String glossaryKey; + private String replaceInfo; + private Boolean honorific; +} diff --git a/src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java b/src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java new file mode 100644 index 0000000..50840b3 --- /dev/null +++ b/src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java @@ -0,0 +1,30 @@ +package HookKiller.server.common.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.*; + +@Getter +@Builder +@ToString +@NoArgsConstructor +@AllArgsConstructor +@JsonInclude(JsonInclude.Include.ALWAYS) +public class PapagoTextResponse { + + private PapagoMessage message; + @Getter + @Setter + public static class PapagoMessage{ + private PapagoResult result; + } + + @Getter + @Setter + @ToString + public static class PapagoResult { + private String srcLangType; + private String tarLangType; + private String translatedText; + } +} + diff --git a/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java b/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java new file mode 100644 index 0000000..31ab6b2 --- /dev/null +++ b/src/main/java/HookKiller/server/common/dto/TranslateResponseDto.java @@ -0,0 +1,29 @@ +package HookKiller.server.common.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class TranslateResponseDto { + + private Message message; + + @AllArgsConstructor + @Getter + @NoArgsConstructor + public static class Message { + private Result result; + } + + @AllArgsConstructor + @Getter + @NoArgsConstructor + public static class Result { + private String srcLangType; + private String tarLangType; + private String translatedText; + } +} diff --git a/src/main/java/HookKiller/server/common/exception/GlobalException.java b/src/main/java/HookKiller/server/common/exception/GlobalException.java index 1d0b496..a81a141 100644 --- a/src/main/java/HookKiller/server/common/exception/GlobalException.java +++ b/src/main/java/HookKiller/server/common/exception/GlobalException.java @@ -24,6 +24,7 @@ public enum GlobalException implements BaseErrorCode{ FILE_DELETE_ERROR(INTERNAL_SERVER_ERROR.value(), "500-11", "파일 삭제 중 오류가 발생하였습니다"), FILE_IO_ERROR(INTERNAL_SERVER_ERROR.value(), "500-12","파일 변환 중 오류가 발생하였습니다"), MAIL_SEND_ERROR(INTERNAL_SERVER_ERROR.value(), "500-20", "메일 발송중 오류가 발생하였습니다"), + NAVER_ERROR(INTERNAL_SERVER_ERROR.value(), "500-30", "네이버 번역 도중 오류가 발생하였습니다") ; private final Integer statusCode; diff --git a/src/main/java/HookKiller/server/common/exception/NaverErrorException.java b/src/main/java/HookKiller/server/common/exception/NaverErrorException.java new file mode 100644 index 0000000..aec83f0 --- /dev/null +++ b/src/main/java/HookKiller/server/common/exception/NaverErrorException.java @@ -0,0 +1,14 @@ +package HookKiller.server.common.exception; + + +import static HookKiller.server.common.exception.GlobalException.NAVER_ERROR; + +public class NaverErrorException extends BaseException { + + public static final BaseException EXCEPTION = new NaverErrorException(); + + private NaverErrorException() { + super(NAVER_ERROR); + } + +} \ No newline at end of file diff --git a/src/main/java/HookKiller/server/common/service/TranslateService.java b/src/main/java/HookKiller/server/common/service/TranslateService.java new file mode 100644 index 0000000..18eb9a0 --- /dev/null +++ b/src/main/java/HookKiller/server/common/service/TranslateService.java @@ -0,0 +1,85 @@ +package HookKiller.server.common.service; + +import HookKiller.server.common.dto.*; +import HookKiller.server.common.exception.NaverErrorException; +import HookKiller.server.common.type.LanguageType; +import HookKiller.server.properties.PapagoProperties; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.*; +import org.springframework.http.converter.FormHttpMessageConverter; +import org.springframework.stereotype.Service; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@Service +@RequiredArgsConstructor +public class TranslateService { + + private final PapagoProperties papagoProperties; + private final ObjectMapper objectMapper; + + public String naverPapagoHtmlTranslate(LanguageType source, LanguageType target, String html) { + RestTemplate restTemplate = new RestTemplate(); + + MultiValueMap papagoRequestBody = new LinkedMultiValueMap<>(); + papagoRequestBody.add("source", source.getLanguageCode()); + papagoRequestBody.add("target", target.getLanguageCode()); + papagoRequestBody.add("html", html); + +// Map papagoRequestBody = objectMapper.convertValue(PapagoHtmlRequest.builder() +// .source(source.getLanguageCode()) +// .target(target.getLanguageCode()) +// .html(html) +// .build(), Map.class); + +// HttpEntity request = new HttpEntity<>(papagoRequestBody, papagoRequestHeaders); + + HttpHeaders papagoRequestHeaders = new HttpHeaders(); + papagoRequestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + papagoProperties.getHeader().forEach(papagoRequestHeaders::set); + + HttpEntity> request = new HttpEntity<>(papagoRequestBody, papagoRequestHeaders); + + ResponseEntity response = + restTemplate.postForEntity(URI.create(papagoProperties.getHtmlRequestUrl()), request, String.class); + if (response.getStatusCode().equals(HttpStatus.OK)) { + log.info("result >>> {}", response.getBody()); + return response.getBody(); + } + throw NaverErrorException.EXCEPTION; + } + + public String naverPapagoTextTranslate(LanguageType source, LanguageType target, String content) { + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders papagoRequestHeaders = new HttpHeaders(); + papagoRequestHeaders.setContentType(MediaType.APPLICATION_JSON); + papagoProperties.getHeader().forEach(papagoRequestHeaders::set); + + Map papagoRequestBody = objectMapper.convertValue(PapagoTextRequest.builder() + .source(source.getLanguageCode()) + .target(target.getLanguageCode()) + .text(content) + .build(), Map.class); + + HttpEntity request = new HttpEntity<>(papagoRequestBody, papagoRequestHeaders); + + ResponseEntity response = + restTemplate.postForEntity(URI.create(papagoProperties.getTextRequestUrl()), request, PapagoTextResponse.class); + + if (response.getStatusCode().equals(HttpStatus.OK)) { + log.info("result >>> {}", response.getBody().getMessage().getResult().toString()); + return response.getBody().getMessage().getResult().getTranslatedText(); + } + throw NaverErrorException.EXCEPTION; + } + +} + diff --git a/src/main/java/HookKiller/server/common/type/LanguageType.java b/src/main/java/HookKiller/server/common/type/LanguageType.java index 9c24f09..1a76479 100644 --- a/src/main/java/HookKiller/server/common/type/LanguageType.java +++ b/src/main/java/HookKiller/server/common/type/LanguageType.java @@ -1,13 +1,17 @@ package HookKiller.server.common.type; import jakarta.servlet.http.HttpServletRequest; +import lombok.AllArgsConstructor; import lombok.Getter; import java.util.Arrays; @Getter +@AllArgsConstructor public enum LanguageType { - KO, EN, CN, JP; + KO("ko"), EN("en"), CN("zh-CN"), JP("ja"); + + private String languageCode; private static final String HTTP_REQUEST_HEADER_KEY_LANGUAGE = "language"; diff --git a/src/main/java/HookKiller/server/config/ConfigurationProperties.java b/src/main/java/HookKiller/server/config/ConfigurationProperties.java index 4dc9057..92aa3cf 100644 --- a/src/main/java/HookKiller/server/config/ConfigurationProperties.java +++ b/src/main/java/HookKiller/server/config/ConfigurationProperties.java @@ -4,6 +4,7 @@ import HookKiller.server.properties.CorsProperties; import HookKiller.server.properties.JwtProperties; import HookKiller.server.properties.NaverObjectStorageProperties; +import HookKiller.server.properties.PapagoProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; @@ -11,7 +12,8 @@ { JwtProperties.class, CorsProperties.class, - NaverObjectStorageProperties.class + NaverObjectStorageProperties.class, + PapagoProperties.class } ) @Configuration diff --git a/src/main/java/HookKiller/server/properties/NaverObjectStorageProperties.java b/src/main/java/HookKiller/server/properties/NaverObjectStorageProperties.java index ad9724d..e2d7726 100644 --- a/src/main/java/HookKiller/server/properties/NaverObjectStorageProperties.java +++ b/src/main/java/HookKiller/server/properties/NaverObjectStorageProperties.java @@ -17,3 +17,4 @@ public class NaverObjectStorageProperties { private String secretKey; private String bucketName; } + diff --git a/src/main/java/HookKiller/server/properties/PapagoProperties.java b/src/main/java/HookKiller/server/properties/PapagoProperties.java new file mode 100644 index 0000000..36a5883 --- /dev/null +++ b/src/main/java/HookKiller/server/properties/PapagoProperties.java @@ -0,0 +1,19 @@ +package HookKiller.server.properties; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.ToString; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import java.util.Map; + +@Getter +@ToString +@AllArgsConstructor +@ConfigurationProperties(prefix = "papago") +public class PapagoProperties { + private String textRequestUrl; + private String htmlRequestUrl; + private Map header; +} diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 999de09..2ecc533 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,8 +1,8 @@ # TODO : 각자 개발용 URL, 개인 Database명 입력 필요, 해당 2개의 속성은 커밋 제외 필수!!!!! hook: db: - url: db-it7f7-kr.vpc-pub-cdb.ntruss.com - database: jw + url: + database: username: hooklocal password: hooklocal1234! port: 3306 diff --git a/src/main/resources/application-papago.yml b/src/main/resources/application-papago.yml new file mode 100644 index 0000000..eacec33 --- /dev/null +++ b/src/main/resources/application-papago.yml @@ -0,0 +1,3 @@ +papago: + text-request-url: https://naveropenapi.apigw.ntruss.com/nmt/v1/translation + html-request-url: https://naveropenapi.apigw.ntruss.com/web-trans/v1/translate \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index b31fc07..f9b4372 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,6 +4,7 @@ spring: include: - db - auth + - papago mail: From 5df9c1a9871aea55bff50be9026dc26a9eab6945 Mon Sep 17 00:00:00 2001 From: donsonioc2010 Date: Fri, 13 Oct 2023 09:13:03 +0900 Subject: [PATCH 14/15] =?UTF-8?q?fix=20:=20=ED=8E=B8=EC=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/ArticleController.java | 1 - .../server/board/dto/ArticleRequestDto.java | 53 ++++++++++--------- .../board/dto/PostArticleRequestDto.java | 1 - .../server/board/entity/Article.java | 2 + .../server/board/entity/ReplyContent.java | 6 ++- .../repository/ArticleContentRepository.java | 4 +- .../board/repository/BoardRepository.java | 4 -- .../server/board/service/ArticleService.java | 13 +---- 8 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/main/java/HookKiller/server/board/controller/ArticleController.java b/src/main/java/HookKiller/server/board/controller/ArticleController.java index 46b31e4..67c4933 100644 --- a/src/main/java/HookKiller/server/board/controller/ArticleController.java +++ b/src/main/java/HookKiller/server/board/controller/ArticleController.java @@ -4,7 +4,6 @@ import HookKiller.server.board.dto.PostArticleRequestDto; import HookKiller.server.board.service.ArticleContentService; import HookKiller.server.board.service.ArticleService; -import HookKiller.server.board.type.BoardType; import HookKiller.server.common.type.LanguageType; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java b/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java index 35f8e5d..6108a86 100644 --- a/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java +++ b/src/main/java/HookKiller/server/board/dto/ArticleRequestDto.java @@ -15,30 +15,33 @@ @Builder public class ArticleRequestDto extends AbstractTimeStamp { - private Long boardId; - private Long articleId; - @Enumerated(EnumType.STRING) - private LanguageType orgArticleLanguage; - @Enumerated(EnumType.STRING) - private ArticleStatus status; - private int likeCount; - private Long createdUserId; - private Long updatedUserId; - - private Long contentId; - @Enumerated(EnumType.STRING) - private LanguageType contentLanguage; - private String title; - @Lob - private String content; - - public static ArticleRequestDto of(Article article, ArticleContent articleContent) { - return ArticleRequestDto.builder() - .articleId(article.getId()) - .title(articleContent.getTitle()) - .content(articleContent.getContent()) - .likeCount(article.getLikeCount()) - .build(); - } + private Long boardId; + + private Long articleId; + + @Enumerated(EnumType.STRING) + private LanguageType orgArticleLanguage; + + @Enumerated(EnumType.STRING) + private ArticleStatus status; + + private int likeCount; + + @Enumerated(EnumType.STRING) + private LanguageType contentLanguage; + + private String title; + + @Lob + private String content; + + public static ArticleRequestDto of(Article article, ArticleContent articleContent) { + return ArticleRequestDto.builder() + .articleId(article.getId()) + .title(articleContent.getTitle()) + .content(articleContent.getContent()) + .likeCount(article.getLikeCount()) + .build(); + } } diff --git a/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java b/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java index 9866a7f..3638bb5 100644 --- a/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java +++ b/src/main/java/HookKiller/server/board/dto/PostArticleRequestDto.java @@ -29,6 +29,5 @@ public class PostArticleRequestDto { private String title; @NotEmpty(message = "내용이 입력되지 않았습니다.") - @Lob private String content; } diff --git a/src/main/java/HookKiller/server/board/entity/Article.java b/src/main/java/HookKiller/server/board/entity/Article.java index e3b8ab2..0dea218 100644 --- a/src/main/java/HookKiller/server/board/entity/Article.java +++ b/src/main/java/HookKiller/server/board/entity/Article.java @@ -20,6 +20,7 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.Setter; import java.util.ArrayList; import java.util.List; @@ -37,6 +38,7 @@ @Entity @Getter +@Setter @Table(name = "tbl_article") @NoArgsConstructor(access = AccessLevel.PROTECTED) public class Article extends AbstractTimeStamp { diff --git a/src/main/java/HookKiller/server/board/entity/ReplyContent.java b/src/main/java/HookKiller/server/board/entity/ReplyContent.java index 0d021a2..f4f2266 100644 --- a/src/main/java/HookKiller/server/board/entity/ReplyContent.java +++ b/src/main/java/HookKiller/server/board/entity/ReplyContent.java @@ -1,6 +1,6 @@ package HookKiller.server.board.entity; -import HookKiller.server.board.type.BoardType; +import HookKiller.server.common.type.LanguageType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -11,6 +11,7 @@ import jakarta.persistence.Lob; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; +import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -33,7 +34,8 @@ public class ReplyContent { @JoinColumn(name="reply_id") private Reply reply; - private BoardType language; + @NotNull + private LanguageType language; @Column @Lob diff --git a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java index bbfc380..83d5801 100644 --- a/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java +++ b/src/main/java/HookKiller/server/board/repository/ArticleContentRepository.java @@ -10,8 +10,8 @@ public interface ArticleContentRepository extends JpaRepository { - Optional findByArticleAndLanguage(Article article, LanguageType language); + Optional findByArticleAndLanguage(Article article, LanguageType language); - List findAllByArticle(Article article); + List findAllByArticle(Article article); } diff --git a/src/main/java/HookKiller/server/board/repository/BoardRepository.java b/src/main/java/HookKiller/server/board/repository/BoardRepository.java index 5b97e68..a66803f 100644 --- a/src/main/java/HookKiller/server/board/repository/BoardRepository.java +++ b/src/main/java/HookKiller/server/board/repository/BoardRepository.java @@ -1,12 +1,8 @@ package HookKiller.server.board.repository; -import HookKiller.server.board.entity.Article; import HookKiller.server.board.entity.Board; import org.springframework.data.jpa.repository.JpaRepository; -import java.util.List; -import java.util.Optional; - /** * BoardRepository: BoardRepository 인터페이스는 Board 엔터티를 데이터베이스에 diff --git a/src/main/java/HookKiller/server/board/service/ArticleService.java b/src/main/java/HookKiller/server/board/service/ArticleService.java index 949209c..c3551e5 100644 --- a/src/main/java/HookKiller/server/board/service/ArticleService.java +++ b/src/main/java/HookKiller/server/board/service/ArticleService.java @@ -63,22 +63,13 @@ public Article createArticle(PostArticleRequestDto postArticleRequestDto) { @Transactional public Article updateArticle(PostArticleRequestDto postArticleRequestDto) { - Board board = boardRepository.findById(postArticleRequestDto.getBoardId()) - .orElseThrow(() -> BoardNotFoundException.EXCEPTION); - Article article = articleRepository.findById(postArticleRequestDto.getArticleId()) .orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION); User requestUser = userUtils.getUser(); - article = Article.builder() - .id(article.getId()) - .board(board) - .orgArticleLanguage(postArticleRequestDto.getOrgArticleLanguage()) - .articleStatus(article.getArticleStatus()) - .createdUser(article.getCreatedUser()) - .updatedUser(requestUser) - .build(); + article.setOrgArticleLanguage(postArticleRequestDto.getOrgArticleLanguage()); + article.setUpdatedUser(requestUser); // 게시물 내용 업데이트 articleContentService.updateContent(postArticleRequestDto, article); From c9b4cb5c3692d262822742f5e679b2ee85a99058 Mon Sep 17 00:00:00 2001 From: donsonioc2010 Date: Fri, 13 Oct 2023 09:24:49 +0900 Subject: [PATCH 15/15] =?UTF-8?q?refactor=20:=20Package=20Import=20*=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0=20=EB=B0=8F=20HealthController=EC=9D=98=20Pa?= =?UTF-8?q?pago=EA=B8=B0=EB=8A=A5=20PapagoSampleController=EB=A1=9C=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HealthCheckController.java | 8 ----- .../controller/PapagoSampleController.java | 29 +++++++++++++++++++ .../server/common/dto/PapagoHtmlRequest.java | 6 +++- .../server/common/dto/PapagoHtmlResponse.java | 7 +++-- .../server/common/dto/PapagoTextRequest.java | 6 +++- .../server/common/dto/PapagoTextResponse.java | 7 ++++- 6 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 src/main/java/HookKiller/server/common/controller/PapagoSampleController.java diff --git a/src/main/java/HookKiller/server/common/controller/HealthCheckController.java b/src/main/java/HookKiller/server/common/controller/HealthCheckController.java index 5ed6771..6bc7534 100644 --- a/src/main/java/HookKiller/server/common/controller/HealthCheckController.java +++ b/src/main/java/HookKiller/server/common/controller/HealthCheckController.java @@ -1,11 +1,8 @@ package HookKiller.server.common.controller; -import HookKiller.server.common.service.TranslateService; -import HookKiller.server.common.type.LanguageType; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -13,13 +10,8 @@ @RestController @RequiredArgsConstructor public class HealthCheckController { - private final TranslateService translateService; @GetMapping("/health") public ResponseEntity healthCheck(){ - translateService.naverPapagoTextTranslate(LanguageType.KO, LanguageType.JP, "안녕하세요~~"); - translateService.naverPapagoHtmlTranslate(LanguageType.KO, LanguageType.JP, "
안녕하세요. 파파고입니다.
"); - - log.error("authentication : {}", SecurityContextHolder.getContext().getAuthentication().getName()); return ResponseEntity.ok().build(); } diff --git a/src/main/java/HookKiller/server/common/controller/PapagoSampleController.java b/src/main/java/HookKiller/server/common/controller/PapagoSampleController.java new file mode 100644 index 0000000..69a5b80 --- /dev/null +++ b/src/main/java/HookKiller/server/common/controller/PapagoSampleController.java @@ -0,0 +1,29 @@ +package HookKiller.server.common.controller; + +import HookKiller.server.common.service.TranslateService; +import HookKiller.server.common.type.LanguageType; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.util.ArrayList; +import java.util.List; + +// TODO : 추후 삭제 필요 예정, 해당 클래스통해 번역의뢰 하는거 아니고 서비스 사용방법 기록하려 한거에요, 여기 추가하지마세요. by.Jong1 +@Slf4j +@RequiredArgsConstructor +@RequestMapping("/papago") +public class PapagoSampleController { + private final TranslateService translateService; + + @GetMapping("/test") + public ResponseEntity> testSample() { + + return ResponseEntity.ok(new ArrayList<>() {{ + add(translateService.naverPapagoTextTranslate(LanguageType.KO, LanguageType.JP, "안녕하세요~~")); + add(translateService.naverPapagoHtmlTranslate(LanguageType.KO, LanguageType.JP, "
안녕하세요. 파파고입니다.
")); + }}); + } +} diff --git a/src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java b/src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java index bf89efe..6949585 100644 --- a/src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java +++ b/src/main/java/HookKiller/server/common/dto/PapagoHtmlRequest.java @@ -1,7 +1,11 @@ package HookKiller.server.common.dto; import com.fasterxml.jackson.annotation.JsonInclude; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; @Getter @Setter diff --git a/src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java b/src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java index 5b76e4c..366ab6c 100644 --- a/src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java +++ b/src/main/java/HookKiller/server/common/dto/PapagoHtmlResponse.java @@ -1,7 +1,10 @@ package HookKiller.server.common.dto; -import com.fasterxml.jackson.annotation.JsonInclude; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; @Getter @Builder diff --git a/src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java b/src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java index b95c3d9..762fe88 100644 --- a/src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java +++ b/src/main/java/HookKiller/server/common/dto/PapagoTextRequest.java @@ -1,7 +1,11 @@ package HookKiller.server.common.dto; import com.fasterxml.jackson.annotation.JsonInclude; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; @Getter @Setter diff --git a/src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java b/src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java index 50840b3..af4664c 100644 --- a/src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java +++ b/src/main/java/HookKiller/server/common/dto/PapagoTextResponse.java @@ -1,7 +1,12 @@ package HookKiller.server.common.dto; import com.fasterxml.jackson.annotation.JsonInclude; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; @Getter @Builder