From ba3a03d2ed08cb8f90c544501cd782f7e430bb60 Mon Sep 17 00:00:00 2001 From: JaehwanH <54016683+ja7811@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:13:04 +0900 Subject: [PATCH 01/15] updated README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c7584e0..4deabcf 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ # UMC Spring A팀 레포지토리입니다. +Json의 브랜치입니다 😀 + +## ERD +![image](https://github.com/UMC-CAU-6th/Spring-A/assets/54016683/4fb51df0-9bac-4144-ab57-2008fc3c12cc) From 58691346d64229358eb83e95f9bd3a399d346d57 Mon Sep 17 00:00:00 2001 From: JaehwanH <54016683+ja7811@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:14:59 +0900 Subject: [PATCH 02/15] updated README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4deabcf..eb2c927 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,5 @@ Json의 브랜치입니다 😀 ## ERD -![image](https://github.com/UMC-CAU-6th/Spring-A/assets/54016683/4fb51df0-9bac-4144-ab57-2008fc3c12cc) +![image](https://github.com/UMC-CAU-6th/Spring-A/assets/54016683/a2b739bc-4e01-45ae-b57f-5de76c454c2a) + From b022f3ae31b7b902dfd3f9ff5691a15c5f628aa2 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Thu, 27 Jun 2024 02:40:34 +0900 Subject: [PATCH 03/15] =?UTF-8?q?add=20:=20=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20&=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC?= =?UTF-8?q?=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 13 +++++ .../com/umc/domain/board/entity/Board.java | 4 ++ .../board/repository/BoardRepository.java | 4 ++ .../domain/board/service/BoardService.java | 7 +++ .../comment/controller/CommentController.java | 43 +++++++++++++++ .../umc/domain/comment/entity/Comment.java | 4 ++ .../comment/repository/CommentRepository.java | 4 ++ .../comment/service/CommentService.java | 7 +++ .../post/controller/PostController.java | 52 +++++++++++++++++++ .../java/com/umc/domain/post/entity/Post.java | 4 ++ .../com/umc/domain/post/entity/PostImage.java | 4 ++ .../post/repository/PostRepository.java | 4 ++ .../umc/domain/post/service/PostService.java | 7 +++ .../user/dto/MemberLoginRequestDTO.java | 5 +- 14 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 umc/src/main/java/com/umc/domain/board/controller/BoardController.java create mode 100644 umc/src/main/java/com/umc/domain/board/entity/Board.java create mode 100644 umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java create mode 100644 umc/src/main/java/com/umc/domain/board/service/BoardService.java create mode 100644 umc/src/main/java/com/umc/domain/comment/controller/CommentController.java create mode 100644 umc/src/main/java/com/umc/domain/comment/entity/Comment.java create mode 100644 umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java create mode 100644 umc/src/main/java/com/umc/domain/comment/service/CommentService.java create mode 100644 umc/src/main/java/com/umc/domain/post/controller/PostController.java create mode 100644 umc/src/main/java/com/umc/domain/post/entity/Post.java create mode 100644 umc/src/main/java/com/umc/domain/post/entity/PostImage.java create mode 100644 umc/src/main/java/com/umc/domain/post/repository/PostRepository.java create mode 100644 umc/src/main/java/com/umc/domain/post/service/PostService.java diff --git a/umc/src/main/java/com/umc/domain/board/controller/BoardController.java b/umc/src/main/java/com/umc/domain/board/controller/BoardController.java new file mode 100644 index 0000000..b8182ce --- /dev/null +++ b/umc/src/main/java/com/umc/domain/board/controller/BoardController.java @@ -0,0 +1,13 @@ +package com.umc.domain.board.controller; + +import com.umc.domain.board.service.BoardService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RequiredArgsConstructor +@RestController +public class BoardController { + private final BoardService boardService; +} diff --git a/umc/src/main/java/com/umc/domain/board/entity/Board.java b/umc/src/main/java/com/umc/domain/board/entity/Board.java new file mode 100644 index 0000000..bcc538f --- /dev/null +++ b/umc/src/main/java/com/umc/domain/board/entity/Board.java @@ -0,0 +1,4 @@ +package com.umc.domain.board.entity; + +public class Board { +} diff --git a/umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java b/umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java new file mode 100644 index 0000000..0b3bfc2 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java @@ -0,0 +1,4 @@ +package com.umc.domain.board.repository; + +public class BoardRepository { +} diff --git a/umc/src/main/java/com/umc/domain/board/service/BoardService.java b/umc/src/main/java/com/umc/domain/board/service/BoardService.java new file mode 100644 index 0000000..19fb3ef --- /dev/null +++ b/umc/src/main/java/com/umc/domain/board/service/BoardService.java @@ -0,0 +1,7 @@ +package com.umc.domain.board.service; + +import org.springframework.stereotype.Service; + +@Service +public class BoardService { +} diff --git a/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java b/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java new file mode 100644 index 0000000..50afdb3 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java @@ -0,0 +1,43 @@ +package com.umc.domain.comment.controller; + +import com.umc.common.response.ApiResponse; +import com.umc.domain.comment.service.CommentService; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +@Slf4j +@RequiredArgsConstructor +@RestController +public class CommentController { + private final CommentService commentService; + + @CrossOrigin + @Operation(summary = "댓글 조회 API") + @GetMapping("/posts/{postId}/comments") + public ApiResponse findComments(@PathVariable String postId){ + return null; + } + + @CrossOrigin + @Operation(summary = "댓글 작성 API") + @PostMapping("/posts/{postId}/comments") + public ApiResponse writeComment(@PathVariable String postId){ + return null; + } + + @CrossOrigin + @Operation(summary = "댓글 삭제 API") + @DeleteMapping("/posts/{postId}/comments/{commentId}") + public ApiResponse deleteComment(@PathVariable String postId, @PathVariable String commentId){ + return null; + } + + @CrossOrigin + @Operation(summary = "댓글 수정 API") + @PutMapping("/posts/{postId}/comments/{commentId}") + public ApiResponse updateComment(@PathVariable String postId, @PathVariable String commentId){ + return null; + } +} diff --git a/umc/src/main/java/com/umc/domain/comment/entity/Comment.java b/umc/src/main/java/com/umc/domain/comment/entity/Comment.java new file mode 100644 index 0000000..779cccf --- /dev/null +++ b/umc/src/main/java/com/umc/domain/comment/entity/Comment.java @@ -0,0 +1,4 @@ +package com.umc.domain.comment.entity; + +public class Comment { +} diff --git a/umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java b/umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java new file mode 100644 index 0000000..97f653d --- /dev/null +++ b/umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java @@ -0,0 +1,4 @@ +package com.umc.domain.comment.repository; + +public class CommentRepository { +} diff --git a/umc/src/main/java/com/umc/domain/comment/service/CommentService.java b/umc/src/main/java/com/umc/domain/comment/service/CommentService.java new file mode 100644 index 0000000..e01dace --- /dev/null +++ b/umc/src/main/java/com/umc/domain/comment/service/CommentService.java @@ -0,0 +1,7 @@ +package com.umc.domain.comment.service; + +import org.springframework.stereotype.Service; + +@Service +public class CommentService { +} diff --git a/umc/src/main/java/com/umc/domain/post/controller/PostController.java b/umc/src/main/java/com/umc/domain/post/controller/PostController.java new file mode 100644 index 0000000..c4cc6ed --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/controller/PostController.java @@ -0,0 +1,52 @@ +package com.umc.domain.post.controller; + +import com.umc.common.response.ApiResponse; +import com.umc.domain.post.service.PostService; +import io.swagger.v3.oas.annotations.Operation; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +@Slf4j +@RequiredArgsConstructor +@RequestMapping("/api/posts") +@RestController +public class PostController { + + private final PostService postService; + + @CrossOrigin + @Operation(summary = "게시글 조회 API") + @GetMapping() + public ApiResponse findPosts(@RequestParam String params) { + return null; + } + + @CrossOrigin + @Operation(summary = "특정 게시글 조회 API") + @GetMapping("/{postId}") + public ApiResponse findPost(@PathVariable String postId){ + return null; + } + + @CrossOrigin + @Operation(summary = "특정 게시글 삭제 API") + @DeleteMapping("/{postId}") + public ApiResponse deleteById(@PathVariable String postId){ + return null; + } + + @CrossOrigin + @Operation(summary = "게시글 생성 API") + @PostMapping() + public ApiResponse postPost(){ + return null; + } + + @CrossOrigin + @Operation(summary = "게시글 수정 API") + @PutMapping() + public ApiResponse updatePost(){ + return null; + } +} diff --git a/umc/src/main/java/com/umc/domain/post/entity/Post.java b/umc/src/main/java/com/umc/domain/post/entity/Post.java new file mode 100644 index 0000000..512f764 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/entity/Post.java @@ -0,0 +1,4 @@ +package com.umc.domain.post.entity; + +public class Post { +} diff --git a/umc/src/main/java/com/umc/domain/post/entity/PostImage.java b/umc/src/main/java/com/umc/domain/post/entity/PostImage.java new file mode 100644 index 0000000..5f01824 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/entity/PostImage.java @@ -0,0 +1,4 @@ +package com.umc.domain.post.entity; + +public class PostImage { +} diff --git a/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java b/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java new file mode 100644 index 0000000..75490d3 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java @@ -0,0 +1,4 @@ +package com.umc.domain.post.repository; + +public class PostRepository { +} diff --git a/umc/src/main/java/com/umc/domain/post/service/PostService.java b/umc/src/main/java/com/umc/domain/post/service/PostService.java new file mode 100644 index 0000000..c11a1bf --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/service/PostService.java @@ -0,0 +1,7 @@ +package com.umc.domain.post.service; + +import org.springframework.stereotype.Service; + +@Service +public class PostService { +} diff --git a/umc/src/main/java/com/umc/domain/user/dto/MemberLoginRequestDTO.java b/umc/src/main/java/com/umc/domain/user/dto/MemberLoginRequestDTO.java index af097dc..050743a 100644 --- a/umc/src/main/java/com/umc/domain/user/dto/MemberLoginRequestDTO.java +++ b/umc/src/main/java/com/umc/domain/user/dto/MemberLoginRequestDTO.java @@ -1,8 +1,7 @@ package com.umc.domain.user.dto; -import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.Pattern; - +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.Pattern; import lombok.Builder; import lombok.Getter; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; From 0bab55ac06b970093bd3afc62c070716a1635aa4 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Fri, 28 Jun 2024 18:10:58 +0900 Subject: [PATCH 04/15] =?UTF-8?q?add=20:=20=EC=97=94=ED=8B=B0=ED=8B=B0=20?= =?UTF-8?q?=EB=A7=8C=EB=93=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/umc/common/entity/BaseTimeEntity.java | 6 +-- .../board/controller/BoardController.java | 33 ++++++++++++++- .../com/umc/domain/board/entity/Board.java | 21 +++++++++- .../comment/controller/CommentController.java | 1 + .../umc/domain/comment/entity/Comment.java | 34 ++++++++++++++- .../umc/domain/post/dto/SimplePostDTO.java | 17 ++++++++ .../com/umc/domain/post/entity/LikePost.java | 28 +++++++++++++ .../java/com/umc/domain/post/entity/Post.java | 42 ++++++++++++++++++- .../com/umc/domain/post/entity/PostImage.java | 22 ++++++++++ .../com/umc/domain/user/entity/Member.java | 2 +- 10 files changed, 198 insertions(+), 8 deletions(-) create mode 100644 umc/src/main/java/com/umc/domain/post/dto/SimplePostDTO.java create mode 100644 umc/src/main/java/com/umc/domain/post/entity/LikePost.java diff --git a/umc/src/main/java/com/umc/common/entity/BaseTimeEntity.java b/umc/src/main/java/com/umc/common/entity/BaseTimeEntity.java index 15bb6ab..d650141 100644 --- a/umc/src/main/java/com/umc/common/entity/BaseTimeEntity.java +++ b/umc/src/main/java/com/umc/common/entity/BaseTimeEntity.java @@ -2,9 +2,9 @@ import java.time.LocalDateTime; -import javax.persistence.Column; -import javax.persistence.EntityListeners; -import javax.persistence.MappedSuperclass; +import jakarta.persistence.Column; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; import lombok.Getter; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; diff --git a/umc/src/main/java/com/umc/domain/board/controller/BoardController.java b/umc/src/main/java/com/umc/domain/board/controller/BoardController.java index b8182ce..ebaecf5 100644 --- a/umc/src/main/java/com/umc/domain/board/controller/BoardController.java +++ b/umc/src/main/java/com/umc/domain/board/controller/BoardController.java @@ -1,13 +1,44 @@ package com.umc.domain.board.controller; +import com.umc.common.response.ApiResponse; import com.umc.domain.board.service.BoardService; +import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @Slf4j @RequiredArgsConstructor +@RequestMapping("/api/board") @RestController public class BoardController { private final BoardService boardService; + + @CrossOrigin + @Operation(summary = "게시판 생성 API") + @PostMapping("/") + public ApiResponse createBoard(){ + return null; + } + + @CrossOrigin + @Operation(summary = "게시판 조회 API") + @GetMapping("/{boardId}") + public ApiResponse readBoard(@PathVariable String boardId){ + return null; + } + + @CrossOrigin + @Operation(summary = "게시판 삭제 API") + @DeleteMapping("/{boardId}") + public ApiResponse deleteBoard(@PathVariable String boardId){ + return null; + } + + @CrossOrigin + @Operation(summary = "게시판 수정 API") + @PutMapping("/{boardId}") + public ApiResponse updateBoard(@PathVariable String boardId){ + return null; + } } diff --git a/umc/src/main/java/com/umc/domain/board/entity/Board.java b/umc/src/main/java/com/umc/domain/board/entity/Board.java index bcc538f..0a09e10 100644 --- a/umc/src/main/java/com/umc/domain/board/entity/Board.java +++ b/umc/src/main/java/com/umc/domain/board/entity/Board.java @@ -1,4 +1,23 @@ package com.umc.domain.board.entity; -public class Board { +import com.umc.common.entity.BaseTimeEntity; +import jakarta.persistence.*; +import lombok.*; + +@Getter +@Builder +@Setter +@NoArgsConstructor +@Entity +@Table(name= "board") +@AllArgsConstructor +public class Board extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String title; + } diff --git a/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java b/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java index 50afdb3..70aaf85 100644 --- a/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java +++ b/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java @@ -9,6 +9,7 @@ @Slf4j @RequiredArgsConstructor +@RequestMapping("/api") @RestController public class CommentController { private final CommentService commentService; diff --git a/umc/src/main/java/com/umc/domain/comment/entity/Comment.java b/umc/src/main/java/com/umc/domain/comment/entity/Comment.java index 779cccf..6ad0498 100644 --- a/umc/src/main/java/com/umc/domain/comment/entity/Comment.java +++ b/umc/src/main/java/com/umc/domain/comment/entity/Comment.java @@ -1,4 +1,36 @@ package com.umc.domain.comment.entity; -public class Comment { +import com.umc.common.entity.BaseTimeEntity; +import com.umc.domain.post.entity.Post; +import com.umc.domain.user.entity.Member; +import jakarta.persistence.*; +import lombok.*; + +@Getter +@Builder +@Setter +@NoArgsConstructor +@Entity +@Table(name= "comment") +@AllArgsConstructor +public class Comment extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "writer_id") + private Member writer; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "post_id") + private Post post; + + private String content; + + public void setPost(Post post){ + this.post = post; + post.getComments().add(this); + } } diff --git a/umc/src/main/java/com/umc/domain/post/dto/SimplePostDTO.java b/umc/src/main/java/com/umc/domain/post/dto/SimplePostDTO.java new file mode 100644 index 0000000..e3b8311 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/dto/SimplePostDTO.java @@ -0,0 +1,17 @@ +package com.umc.domain.post.dto; + +import com.umc.domain.board.entity.Board; +import com.umc.domain.user.entity.Member; +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDateTime; + +@Builder +@Getter +public class SimplePostDTO { + private String title; + private Board board; + private Member writer; + private LocalDateTime createdAt; +} diff --git a/umc/src/main/java/com/umc/domain/post/entity/LikePost.java b/umc/src/main/java/com/umc/domain/post/entity/LikePost.java new file mode 100644 index 0000000..052b24c --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/entity/LikePost.java @@ -0,0 +1,28 @@ +package com.umc.domain.post.entity; + +import com.umc.common.entity.BaseTimeEntity; +import com.umc.domain.user.entity.Member; +import jakarta.persistence.*; +import lombok.*; + +@Getter +@Builder +@Setter +@NoArgsConstructor +@Entity +@Table(name= "post") +@AllArgsConstructor +public class LikePost extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "post_id") + private Post post; +} diff --git a/umc/src/main/java/com/umc/domain/post/entity/Post.java b/umc/src/main/java/com/umc/domain/post/entity/Post.java index 512f764..411e65f 100644 --- a/umc/src/main/java/com/umc/domain/post/entity/Post.java +++ b/umc/src/main/java/com/umc/domain/post/entity/Post.java @@ -1,4 +1,44 @@ package com.umc.domain.post.entity; -public class Post { +import com.umc.common.entity.BaseTimeEntity; +import com.umc.domain.board.entity.Board; +import com.umc.domain.comment.entity.Comment; +import com.umc.domain.user.entity.Member; +import jakarta.persistence.*; +import lombok.*; + +import java.util.List; + +@Getter +@Builder +@Setter +@NoArgsConstructor +@Entity +@Table(name= "post") +@AllArgsConstructor +public class Post extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "board_id") + private Board board; + + @Column + private String title; + + @Column + private String content; + + @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) + private List images; + + @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) + private List comments; } diff --git a/umc/src/main/java/com/umc/domain/post/entity/PostImage.java b/umc/src/main/java/com/umc/domain/post/entity/PostImage.java index 5f01824..7998d6d 100644 --- a/umc/src/main/java/com/umc/domain/post/entity/PostImage.java +++ b/umc/src/main/java/com/umc/domain/post/entity/PostImage.java @@ -1,4 +1,26 @@ package com.umc.domain.post.entity; +import jakarta.persistence.*; +import lombok.*; + +@Getter +@Builder +@Setter +@NoArgsConstructor +@Entity +@Table(name= "post_image") +@AllArgsConstructor public class PostImage { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "post_id") + private Post post; + + private String originalFilename; + + private String storedFilename; } diff --git a/umc/src/main/java/com/umc/domain/user/entity/Member.java b/umc/src/main/java/com/umc/domain/user/entity/Member.java index 271c7c1..9587743 100644 --- a/umc/src/main/java/com/umc/domain/user/entity/Member.java +++ b/umc/src/main/java/com/umc/domain/user/entity/Member.java @@ -5,7 +5,7 @@ import java.util.Collection; import java.util.List; import java.util.stream.Collectors; -import javax.persistence.*; +import jakarta.persistence.*; import com.umc.common.entity.BaseTimeEntity; import lombok.AllArgsConstructor; From 40a57919acc079de955929fc87c72d809288eaa4 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 19:20:11 +0900 Subject: [PATCH 05/15] =?UTF-8?q?add=20:=20DTO=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/domain/board/dto/BoardRequestDTO.java | 12 ++++++++++ .../domain/comment/dto/CommentRequestDTO.java | 10 +++++++++ .../comment/dto/CommentResponseDTO.java | 12 ++++++++++ .../com/umc/domain/post/dto/PostImageDTO.java | 11 ++++++++++ .../umc/domain/post/dto/PostRequestDTO.java | 11 ++++++++++ .../umc/domain/post/dto/PostResponseDTO.java | 22 +++++++++++++++++++ ...ostDTO.java => SimplePostResponseDTO.java} | 11 +++++----- 7 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 umc/src/main/java/com/umc/domain/board/dto/BoardRequestDTO.java create mode 100644 umc/src/main/java/com/umc/domain/comment/dto/CommentRequestDTO.java create mode 100644 umc/src/main/java/com/umc/domain/comment/dto/CommentResponseDTO.java create mode 100644 umc/src/main/java/com/umc/domain/post/dto/PostImageDTO.java create mode 100644 umc/src/main/java/com/umc/domain/post/dto/PostRequestDTO.java create mode 100644 umc/src/main/java/com/umc/domain/post/dto/PostResponseDTO.java rename umc/src/main/java/com/umc/domain/post/dto/{SimplePostDTO.java => SimplePostResponseDTO.java} (54%) diff --git a/umc/src/main/java/com/umc/domain/board/dto/BoardRequestDTO.java b/umc/src/main/java/com/umc/domain/board/dto/BoardRequestDTO.java new file mode 100644 index 0000000..b262a68 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/board/dto/BoardRequestDTO.java @@ -0,0 +1,12 @@ +package com.umc.domain.board.dto; + +import jakarta.validation.constraints.NotEmpty; +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class BoardRequestDTO { + @NotEmpty(message = "제목은 필수 입력값입니다.") + private String title; +} diff --git a/umc/src/main/java/com/umc/domain/comment/dto/CommentRequestDTO.java b/umc/src/main/java/com/umc/domain/comment/dto/CommentRequestDTO.java new file mode 100644 index 0000000..3987b41 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/comment/dto/CommentRequestDTO.java @@ -0,0 +1,10 @@ +package com.umc.domain.comment.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class CommentRequestDTO { + private String content; +} diff --git a/umc/src/main/java/com/umc/domain/comment/dto/CommentResponseDTO.java b/umc/src/main/java/com/umc/domain/comment/dto/CommentResponseDTO.java new file mode 100644 index 0000000..f2848bf --- /dev/null +++ b/umc/src/main/java/com/umc/domain/comment/dto/CommentResponseDTO.java @@ -0,0 +1,12 @@ +package com.umc.domain.comment.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class CommentResponseDTO { + private Long id; + private String writerNickname; + private String content; +} diff --git a/umc/src/main/java/com/umc/domain/post/dto/PostImageDTO.java b/umc/src/main/java/com/umc/domain/post/dto/PostImageDTO.java new file mode 100644 index 0000000..a5e00b5 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/dto/PostImageDTO.java @@ -0,0 +1,11 @@ +package com.umc.domain.post.dto; + +import lombok.Builder; +import lombok.Getter; + +@Builder +@Getter +public class PostImageDTO { + private String originalFilename; + private String storedFilename; +} diff --git a/umc/src/main/java/com/umc/domain/post/dto/PostRequestDTO.java b/umc/src/main/java/com/umc/domain/post/dto/PostRequestDTO.java new file mode 100644 index 0000000..ce0eb5c --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/dto/PostRequestDTO.java @@ -0,0 +1,11 @@ +package com.umc.domain.post.dto; + +import lombok.Builder; +import lombok.Getter; + +@Getter +@Builder +public class PostRequestDTO { + private String title; + private String content; +} diff --git a/umc/src/main/java/com/umc/domain/post/dto/PostResponseDTO.java b/umc/src/main/java/com/umc/domain/post/dto/PostResponseDTO.java new file mode 100644 index 0000000..8410dcd --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/dto/PostResponseDTO.java @@ -0,0 +1,22 @@ +package com.umc.domain.post.dto; + +import com.umc.domain.board.entity.Board; +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDateTime; +import java.util.List; + +@Builder +@Getter +public class PostResponseDTO { + private Long id; + private String title; + private Long boardId; + private String writerNickname; + private String content; + private List images; + + private LocalDateTime createdAt; + private LocalDateTime modifiedAt; +} diff --git a/umc/src/main/java/com/umc/domain/post/dto/SimplePostDTO.java b/umc/src/main/java/com/umc/domain/post/dto/SimplePostResponseDTO.java similarity index 54% rename from umc/src/main/java/com/umc/domain/post/dto/SimplePostDTO.java rename to umc/src/main/java/com/umc/domain/post/dto/SimplePostResponseDTO.java index e3b8311..9409378 100644 --- a/umc/src/main/java/com/umc/domain/post/dto/SimplePostDTO.java +++ b/umc/src/main/java/com/umc/domain/post/dto/SimplePostResponseDTO.java @@ -1,7 +1,5 @@ package com.umc.domain.post.dto; -import com.umc.domain.board.entity.Board; -import com.umc.domain.user.entity.Member; import lombok.Builder; import lombok.Getter; @@ -9,9 +7,12 @@ @Builder @Getter -public class SimplePostDTO { +public class SimplePostResponseDTO { + private Long id; private String title; - private Board board; - private Member writer; + private Long boardId; + private String writerNickname; + private LocalDateTime createdAt; + private LocalDateTime modifiedAt; } From fc303cfd778d9ba945360a7c616a9b8cecee10a0 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 19:21:37 +0900 Subject: [PATCH 06/15] =?UTF-8?q?update=20:=20=EC=97=94=ED=8B=B0=ED=8B=B0?= =?UTF-8?q?=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- umc/src/main/java/com/umc/domain/post/entity/Post.java | 4 ++-- umc/src/main/java/com/umc/domain/post/entity/PostImage.java | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/umc/src/main/java/com/umc/domain/post/entity/Post.java b/umc/src/main/java/com/umc/domain/post/entity/Post.java index 411e65f..47070bd 100644 --- a/umc/src/main/java/com/umc/domain/post/entity/Post.java +++ b/umc/src/main/java/com/umc/domain/post/entity/Post.java @@ -23,8 +23,8 @@ public class Post extends BaseTimeEntity { private Long id; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "member_id") - private Member member; + @JoinColumn(name = "writer_id") + private Member writer; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "board_id") diff --git a/umc/src/main/java/com/umc/domain/post/entity/PostImage.java b/umc/src/main/java/com/umc/domain/post/entity/PostImage.java index 7998d6d..cf1992b 100644 --- a/umc/src/main/java/com/umc/domain/post/entity/PostImage.java +++ b/umc/src/main/java/com/umc/domain/post/entity/PostImage.java @@ -23,4 +23,9 @@ public class PostImage { private String originalFilename; private String storedFilename; + + public void setPost(Post post){ + post.getImages().add(this); + this.post = post; + } } From fbac39855a03abba83958b7a79cc49ad52b029b0 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 19:21:55 +0900 Subject: [PATCH 07/15] =?UTF-8?q?update=20:=20Repository=20=EC=99=84?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/domain/board/repository/BoardRepository.java | 7 ++++++- .../domain/comment/repository/CommentRepository.java | 8 +++++++- .../domain/post/repository/PostImageRepository.java | 10 ++++++++++ .../umc/domain/post/repository/PostRepository.java | 11 ++++++++++- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 umc/src/main/java/com/umc/domain/post/repository/PostImageRepository.java diff --git a/umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java b/umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java index 0b3bfc2..7e785ad 100644 --- a/umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java +++ b/umc/src/main/java/com/umc/domain/board/repository/BoardRepository.java @@ -1,4 +1,9 @@ package com.umc.domain.board.repository; -public class BoardRepository { +import com.umc.domain.board.entity.Board; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface BoardRepository extends JpaRepository { } diff --git a/umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java b/umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java index 97f653d..239329b 100644 --- a/umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java +++ b/umc/src/main/java/com/umc/domain/comment/repository/CommentRepository.java @@ -1,4 +1,10 @@ package com.umc.domain.comment.repository; -public class CommentRepository { +import com.umc.domain.comment.entity.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface CommentRepository extends JpaRepository { + List findAllByPostId(Long postId); } diff --git a/umc/src/main/java/com/umc/domain/post/repository/PostImageRepository.java b/umc/src/main/java/com/umc/domain/post/repository/PostImageRepository.java new file mode 100644 index 0000000..201a3d6 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/repository/PostImageRepository.java @@ -0,0 +1,10 @@ +package com.umc.domain.post.repository; + +import com.umc.domain.post.entity.PostImage; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface PostImageRepository extends JpaRepository { + List findAllByPostId(Long id); +} diff --git a/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java b/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java index 75490d3..5798c07 100644 --- a/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java +++ b/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java @@ -1,4 +1,13 @@ package com.umc.domain.post.repository; -public class PostRepository { +import com.umc.domain.post.entity.Post; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface PostRepository extends JpaRepository { + + List findAllByBoardId(Long aLong); } From 529fbf7a94a35da4700d813485ee28a8666c6d81 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 19:22:06 +0900 Subject: [PATCH 08/15] =?UTF-8?q?update=20:=20Service=20=EC=99=84=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/board/service/BoardService.java | 56 +++++++++ .../comment/service/CommentService.java | 51 +++++++++ .../domain/post/service/PostImageService.java | 66 +++++++++++ .../umc/domain/post/service/PostService.java | 106 ++++++++++++++++++ 4 files changed, 279 insertions(+) create mode 100644 umc/src/main/java/com/umc/domain/post/service/PostImageService.java diff --git a/umc/src/main/java/com/umc/domain/board/service/BoardService.java b/umc/src/main/java/com/umc/domain/board/service/BoardService.java index 19fb3ef..d9f17fc 100644 --- a/umc/src/main/java/com/umc/domain/board/service/BoardService.java +++ b/umc/src/main/java/com/umc/domain/board/service/BoardService.java @@ -1,7 +1,63 @@ package com.umc.domain.board.service; +import com.umc.common.response.ApiResponse; +import com.umc.common.response.status.SuccessCode; +import com.umc.domain.board.dto.BoardRequestDTO; +import com.umc.domain.board.entity.Board; +import com.umc.domain.board.repository.BoardRepository; +import com.umc.domain.post.dto.SimplePostResponseDTO; +import com.umc.domain.post.entity.Post; +import com.umc.domain.post.repository.PostRepository; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; @Service +@RequiredArgsConstructor public class BoardService { + private final BoardRepository boardRepository; + private final PostRepository postRepository; + + public ApiResponse createBoard(BoardRequestDTO boardRequestDTO) { + Board board = Board.builder() + .title(boardRequestDTO.getTitle()) + .build(); + + boardRepository.save(board); + return ApiResponse.of(SuccessCode._OK, "저장되었습니다"); + } + + @Transactional + public ApiResponse readBoardPosts(String boardId) { + List posts = postRepository.findAllByBoardId(Long.getLong(boardId)); + + List postDTOs = posts.stream().map(post -> + SimplePostResponseDTO.builder() + .boardId(Long.getLong(boardId)) + .writerNickname(post.getWriter().getNickname()) + .title(post.getTitle()) + .createdAt(post.getCreatedAt()) + .modifiedAt(post.getModifiedAt()) + .build() + ).toList(); + + return ApiResponse.of(SuccessCode._OK, postDTOs); + } + + public ApiResponse delete(String boardId) { + Board target = boardRepository.findById(Long.getLong(boardId)).orElseThrow(); + boardRepository.delete(target); + + return ApiResponse.of(SuccessCode._OK, "삭제되었습니다"); + } + + public ApiResponse update(String boardId, BoardRequestDTO boardUpdateDTO) { + Board target = boardRepository.findById(Long.getLong(boardId)).orElseThrow(); + target.setTitle(boardUpdateDTO.getTitle()); + boardRepository.save(target); + + return ApiResponse.of(SuccessCode._OK, "수정되었습니다"); + } } diff --git a/umc/src/main/java/com/umc/domain/comment/service/CommentService.java b/umc/src/main/java/com/umc/domain/comment/service/CommentService.java index e01dace..d2e04da 100644 --- a/umc/src/main/java/com/umc/domain/comment/service/CommentService.java +++ b/umc/src/main/java/com/umc/domain/comment/service/CommentService.java @@ -1,7 +1,58 @@ package com.umc.domain.comment.service; +import com.umc.common.response.ApiResponse; +import com.umc.common.response.status.SuccessCode; +import com.umc.domain.comment.dto.CommentRequestDTO; +import com.umc.domain.comment.entity.Comment; +import com.umc.domain.comment.repository.CommentRepository; +import com.umc.domain.post.entity.Post; +import com.umc.domain.post.repository.PostRepository; +import com.umc.domain.user.entity.Member; +import com.umc.domain.user.repository.MemberRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import java.util.List; + +@RequiredArgsConstructor @Service public class CommentService { + private final PostRepository postRepository; + private final CommentRepository commentRepository; + private final MemberRepository memberRepository; + + public ApiResponse> findByPostId(String postId) { + List comments = commentRepository.findAllByPostId(Long.getLong(postId)); + return ApiResponse.of(SuccessCode._OK, comments); + } + + public ApiResponse save(String postId, CommentRequestDTO commentRequest) { + Member writer = memberRepository.findByEmail( + SecurityContextHolder.getContext().getAuthentication().getName() + ).orElseThrow(); + Post post = postRepository.findById(Long.getLong(postId)).orElseThrow(); + Comment comment = Comment.builder() + .content(commentRequest.getContent()) + .build(); + + comment.setWriter(writer); + comment.setPost(post); + + commentRepository.save(comment); + return ApiResponse.of(SuccessCode._OK, "저장되었습니다"); + } + + public ApiResponse delete(String commentId) { + Comment target = commentRepository.findById(Long.getLong(commentId)).orElseThrow(); + commentRepository.delete(target); + return ApiResponse.of(SuccessCode._OK, "삭제되었습니다"); + } + + public ApiResponse update(String commentId, CommentRequestDTO commentRequestDTO) { + Comment target = commentRepository.findById(Long.getLong(commentId)).orElseThrow(); + target.setContent(commentRequestDTO.getContent()); + commentRepository.save(target); + return ApiResponse.of(SuccessCode._OK, "수정되었습니다"); + } } diff --git a/umc/src/main/java/com/umc/domain/post/service/PostImageService.java b/umc/src/main/java/com/umc/domain/post/service/PostImageService.java new file mode 100644 index 0000000..109e383 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/service/PostImageService.java @@ -0,0 +1,66 @@ +package com.umc.domain.post.service; + +import com.umc.domain.post.entity.Post; +import com.umc.domain.post.entity.PostImage; +import com.umc.domain.post.repository.PostImageRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; + +@Service +@RequiredArgsConstructor +public class PostImageService { + private final PostImageRepository postImageRepository; + private static String savePath = ""; // TODO + + public static String createRandomFilename(String original){ + // 저장용 파일명 생성 (랜덤 문자열을 앞에 붙임) + return UUID.randomUUID().toString() + "_" + original; + } + + public List save(MultipartFile[] imageFiles, Post post){ + List postImages = new ArrayList<>(); + Arrays.stream(imageFiles).forEach(image -> { + // 1. PostImage 엔티티 생성 + postImages.add(PostImage.builder() + .originalFilename(image.getOriginalFilename()) + .storedFilename(PostImageService.createRandomFilename(image.getOriginalFilename())) + .post(post) + .build()); + // 2. 파일 저장 + try { + saveToDevice(image); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + // 3. 엔티티 저장 + postImageRepository.saveAll(postImages); + return postImages; + } + + public static void saveToDevice(MultipartFile image) throws IOException { + image.transferTo(new File(PostImageService.savePath)); + } + + public static void saveToDevice(MultipartFile image, String path) throws IOException { + image.transferTo(new File(path)); + } + + public List update(MultipartFile[] imageFiles, Post post) { + deleteImagesOnPost(post); + return save(imageFiles, post); + } + + public void deleteImagesOnPost(Post post){ + List postImages = postImageRepository.findAllByPostId(post.getId()); + postImageRepository.deleteAll(postImages); + } +} diff --git a/umc/src/main/java/com/umc/domain/post/service/PostService.java b/umc/src/main/java/com/umc/domain/post/service/PostService.java index c11a1bf..8e1c9a7 100644 --- a/umc/src/main/java/com/umc/domain/post/service/PostService.java +++ b/umc/src/main/java/com/umc/domain/post/service/PostService.java @@ -1,7 +1,113 @@ package com.umc.domain.post.service; +import com.umc.common.response.ApiResponse; +import com.umc.common.response.status.SuccessCode; +import com.umc.domain.board.entity.Board; +import com.umc.domain.board.repository.BoardRepository; +import com.umc.domain.post.dto.PostImageDTO; +import com.umc.domain.post.dto.PostRequestDTO; +import com.umc.domain.post.dto.PostResponseDTO; +import com.umc.domain.post.dto.SimplePostResponseDTO; +import com.umc.domain.post.entity.Post; +import com.umc.domain.post.entity.PostImage; +import com.umc.domain.post.repository.PostRepository; +import com.umc.domain.user.entity.Member; +import com.umc.domain.user.repository.MemberRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; +import java.util.List; +import java.util.Objects; + +@RequiredArgsConstructor @Service public class PostService { + private final PostRepository postRepository; + private final MemberRepository memberRepository; + private final BoardRepository boardRepository; + private final PostImageService postImageService; + + @Transactional + public ApiResponse> findAll() { + List posts = postRepository.findAll(); + + List postDTO = posts.stream().map( + post -> SimplePostResponseDTO.builder() + .id(post.getId()) + .title(post.getTitle()) + .boardId(post.getBoard().getId()) + .writerNickname(post.getWriter().getNickname()) + .createdAt(post.getCreatedAt()) + .modifiedAt(post.getModifiedAt()) + .build() + ).toList(); + return ApiResponse.of(SuccessCode._OK, postDTO); + } + + public ApiResponse find(String postId) { + Post post = postRepository.findById(Long.getLong(postId)).orElseThrow(); + + return ApiResponse.of(SuccessCode._OK, PostResponseDTO.builder() + .id(Long.getLong(postId)) + .title(post.getTitle()) + .content(post.getContent()) + .writerNickname(post.getWriter().getNickname()) + .boardId(post.getBoard().getId()) + .images( + post.getImages().stream().map( image-> + PostImageDTO.builder() + .originalFilename(image.getOriginalFilename()) + .storedFilename(image.getStoredFilename()) + .build() + ).toList() + ) + .createdAt(post.getCreatedAt()) + .modifiedAt(post.getModifiedAt()) + .build() + ); + } + + public ApiResponse delete(String postId) { + Post target = postRepository.findById(Long.getLong(postId)).orElseThrow(); + postRepository.delete(target); + + return ApiResponse.of(SuccessCode._OK, "삭제되었습니다."); + } + + public ApiResponse save(String boardId, PostRequestDTO postRequest, MultipartFile[] imageFiles) { + Member writer = memberRepository.findByEmail( + SecurityContextHolder.getContext().getAuthentication().getName() + ).orElseThrow(); // 되나? + Board board = boardRepository.findById(Long.getLong(boardId)).orElseThrow(); + // Post 엔티티 생성 + Post post = Post.builder() + .title(postRequest.getTitle()) + .content(postRequest.getContent()) + .writer(writer) + .board(board) + .build(); + // 이미지 저장 + List images = postImageService.save(imageFiles, post); + post.setImages(images); + + postRepository.save(post); + return ApiResponse.of(SuccessCode._OK, "저장되었습니다"); + } + + public ApiResponse update(String postId, PostRequestDTO postRequest, MultipartFile[] imageFiles) { + Post target = postRepository.findById(Long.getLong(postId)).orElseThrow(); + target.setTitle(postRequest.getTitle()); + target.setContent(postRequest.getContent()); + + if(!Objects.isNull(imageFiles)) { + List images = postImageService.update(imageFiles, target); + target.setImages(images); + } + + postRepository.save(target); + return ApiResponse.of(SuccessCode._OK, "수정되었습니다"); + } } From 0058accfff8f3498f20828062a7ba2d388b8ed85 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 19:22:14 +0900 Subject: [PATCH 09/15] =?UTF-8?q?update=20:=20Controller=20=EC=99=84?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 18 ++++++---- .../comment/controller/CommentController.java | 24 ++++++++----- .../post/controller/PostController.java | 36 ++++++++++++------- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/umc/src/main/java/com/umc/domain/board/controller/BoardController.java b/umc/src/main/java/com/umc/domain/board/controller/BoardController.java index ebaecf5..6ecb828 100644 --- a/umc/src/main/java/com/umc/domain/board/controller/BoardController.java +++ b/umc/src/main/java/com/umc/domain/board/controller/BoardController.java @@ -1,8 +1,11 @@ package com.umc.domain.board.controller; import com.umc.common.response.ApiResponse; +import com.umc.domain.board.dto.BoardRequestDTO; import com.umc.domain.board.service.BoardService; import io.swagger.v3.oas.annotations.Operation; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotEmpty; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -17,28 +20,29 @@ public class BoardController { @CrossOrigin @Operation(summary = "게시판 생성 API") @PostMapping("/") - public ApiResponse createBoard(){ - return null; + public ApiResponse createBoard(@Valid @RequestBody BoardRequestDTO boardRequestDTO){ + return boardService.createBoard(boardRequestDTO); } @CrossOrigin - @Operation(summary = "게시판 조회 API") + @Operation(summary = "게시판 게시글 조회 API") @GetMapping("/{boardId}") public ApiResponse readBoard(@PathVariable String boardId){ - return null; + return boardService.readBoardPosts(boardId); } @CrossOrigin @Operation(summary = "게시판 삭제 API") @DeleteMapping("/{boardId}") public ApiResponse deleteBoard(@PathVariable String boardId){ - return null; + return boardService.delete(boardId); } @CrossOrigin @Operation(summary = "게시판 수정 API") @PutMapping("/{boardId}") - public ApiResponse updateBoard(@PathVariable String boardId){ - return null; + public ApiResponse updateBoard(@PathVariable String boardId, + @Valid @RequestBody BoardRequestDTO boardUpdateDTO){ + return boardService.update(boardId, boardUpdateDTO); } } diff --git a/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java b/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java index 70aaf85..45adc69 100644 --- a/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java +++ b/umc/src/main/java/com/umc/domain/comment/controller/CommentController.java @@ -1,12 +1,16 @@ package com.umc.domain.comment.controller; import com.umc.common.response.ApiResponse; +import com.umc.domain.comment.dto.CommentRequestDTO; +import com.umc.domain.comment.entity.Comment; import com.umc.domain.comment.service.CommentService; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; +import java.util.List; + @Slf4j @RequiredArgsConstructor @RequestMapping("/api") @@ -17,28 +21,32 @@ public class CommentController { @CrossOrigin @Operation(summary = "댓글 조회 API") @GetMapping("/posts/{postId}/comments") - public ApiResponse findComments(@PathVariable String postId){ - return null; + public ApiResponse> findComments(@PathVariable String postId){ + return commentService.findByPostId(postId); } @CrossOrigin @Operation(summary = "댓글 작성 API") @PostMapping("/posts/{postId}/comments") - public ApiResponse writeComment(@PathVariable String postId){ - return null; + public ApiResponse writeComment(@PathVariable String postId, + @RequestBody CommentRequestDTO commentRequestDTO){ + return commentService.save(postId, commentRequestDTO); } @CrossOrigin @Operation(summary = "댓글 삭제 API") @DeleteMapping("/posts/{postId}/comments/{commentId}") - public ApiResponse deleteComment(@PathVariable String postId, @PathVariable String commentId){ - return null; + public ApiResponse deleteComment(@PathVariable String postId, + @PathVariable String commentId){ + return commentService.delete(commentId); } @CrossOrigin @Operation(summary = "댓글 수정 API") @PutMapping("/posts/{postId}/comments/{commentId}") - public ApiResponse updateComment(@PathVariable String postId, @PathVariable String commentId){ - return null; + public ApiResponse updateComment(@PathVariable String postId, + @PathVariable String commentId, + @RequestBody CommentRequestDTO commentUpdateRequestDTO){ + return commentService.update(commentId, commentUpdateRequestDTO); } } diff --git a/umc/src/main/java/com/umc/domain/post/controller/PostController.java b/umc/src/main/java/com/umc/domain/post/controller/PostController.java index c4cc6ed..f414267 100644 --- a/umc/src/main/java/com/umc/domain/post/controller/PostController.java +++ b/umc/src/main/java/com/umc/domain/post/controller/PostController.java @@ -1,11 +1,17 @@ package com.umc.domain.post.controller; import com.umc.common.response.ApiResponse; +import com.umc.domain.post.dto.PostRequestDTO; +import com.umc.domain.post.dto.PostResponseDTO; +import com.umc.domain.post.dto.SimplePostResponseDTO; import com.umc.domain.post.service.PostService; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; @Slf4j @RequiredArgsConstructor @@ -17,36 +23,40 @@ public class PostController { @CrossOrigin @Operation(summary = "게시글 조회 API") - @GetMapping() - public ApiResponse findPosts(@RequestParam String params) { - return null; + @GetMapping + public ApiResponse> findPosts(@RequestParam String params) { + return postService.findAll(); } @CrossOrigin @Operation(summary = "특정 게시글 조회 API") @GetMapping("/{postId}") - public ApiResponse findPost(@PathVariable String postId){ - return null; + public ApiResponse findPost(@PathVariable String postId){ + return postService.find(postId); } @CrossOrigin @Operation(summary = "특정 게시글 삭제 API") @DeleteMapping("/{postId}") - public ApiResponse deleteById(@PathVariable String postId){ - return null; + public ApiResponse deleteById(@PathVariable String postId){ + return postService.delete(postId); } @CrossOrigin @Operation(summary = "게시글 생성 API") - @PostMapping() - public ApiResponse postPost(){ - return null; + @PostMapping + public ApiResponse postPost(@RequestParam(name = "boardId") String boardId, + @ModelAttribute PostRequestDTO postRequestDto, + @RequestParam(value = "images", required = false) MultipartFile[] images){ + return postService.save(boardId, postRequestDto, images); } @CrossOrigin @Operation(summary = "게시글 수정 API") - @PutMapping() - public ApiResponse updatePost(){ - return null; + @PutMapping("/{postId}") + public ApiResponse updatePost(@PathVariable String postId, + @ModelAttribute PostRequestDTO postRequestDto, + @RequestParam(value = "images", required = false) MultipartFile[] images){ + return postService.update(postId, postRequestDto, images); } } From e73c4d9030157e74af74a2ce30ac600787644f8c Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 19:39:55 +0900 Subject: [PATCH 10/15] =?UTF-8?q?fix=20:=20=EC=97=90=EB=9F=AC=EB=82=98?= =?UTF-8?q?=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EC=A0=9C=EA=B1=B0=20+=20h2?= =?UTF-8?q?=EB=A1=9C=20DB=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- umc/build.gradle | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/umc/build.gradle b/umc/build.gradle index 73dda60..8eb8f6a 100644 --- a/umc/build.gradle +++ b/umc/build.gradle @@ -26,20 +26,24 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' - testImplementation 'junit:junit:4.13.1' + compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' + + testImplementation 'junit:junit:4.13.1' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' implementation 'org.springframework.boot:spring-boot-starter-validation' - runtimeOnly 'com.mysql:mysql-connector-j' - implementation group: 'org.hibernate', name: 'hibernate-spatial', version: '5.6.15.Final' + + //runtimeOnly 'com.mysql:mysql-connector-j' + runtimeOnly 'com.h2database:h2' + //implementation group: 'org.hibernate', name: 'hibernate-spatial', version: '5.6.15.Final' // jwt implementation 'io.jsonwebtoken:jjwt-api:0.11.5' implementation 'io.jsonwebtoken:jjwt-impl:0.11.5' implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5' implementation 'org.springdoc:springdoc-openapi-ui:1.6.15' - implementation 'io.springfox:springfox-swagger2:2.9.2' +// implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2' } From 0d381d8c81c901e10c7da27cd1e4ea2fcb68a063 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 20:19:31 +0900 Subject: [PATCH 11/15] =?UTF-8?q?fix:=20RequestParam=20=EC=95=88=EB=B0=9B?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/umc/domain/post/controller/PostController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/umc/src/main/java/com/umc/domain/post/controller/PostController.java b/umc/src/main/java/com/umc/domain/post/controller/PostController.java index f414267..cc7355c 100644 --- a/umc/src/main/java/com/umc/domain/post/controller/PostController.java +++ b/umc/src/main/java/com/umc/domain/post/controller/PostController.java @@ -24,7 +24,7 @@ public class PostController { @CrossOrigin @Operation(summary = "게시글 조회 API") @GetMapping - public ApiResponse> findPosts(@RequestParam String params) { + public ApiResponse> findPosts() { return postService.findAll(); } From c0622a25033c21f1002d5bec6aae8ea81c0b5db1 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 20:21:12 +0900 Subject: [PATCH 12/15] =?UTF-8?q?fix:=20=EC=9D=BC=EB=8B=A8=20url=20?= =?UTF-8?q?=EB=AA=A8=EB=91=90=20=ED=97=88=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- umc/src/main/java/com/umc/common/config/SecurityConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/umc/src/main/java/com/umc/common/config/SecurityConfig.java b/umc/src/main/java/com/umc/common/config/SecurityConfig.java index 47758ec..fad1d67 100644 --- a/umc/src/main/java/com/umc/common/config/SecurityConfig.java +++ b/umc/src/main/java/com/umc/common/config/SecurityConfig.java @@ -38,8 +38,9 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti .requestMatchers("/upload").permitAll() .requestMatchers("/api/**").permitAll() .requestMatchers("/swagger-ui/**").permitAll() + .requestMatchers("/swagger-ui.html").permitAll() .requestMatchers("/v3/api-docs/**").permitAll() - .anyRequest().authenticated() + .anyRequest().permitAll() ) .addFilterBefore(new JwtAuthenticationFilter(jwtTokenProvider), UsernamePasswordAuthenticationFilter.class); From a7edd4d70668c377d6498ef119f4558771b373e5 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Sat, 29 Jun 2024 20:25:02 +0900 Subject: [PATCH 13/15] =?UTF-8?q?=EC=8A=A4=EC=9B=A8=EA=B1=B0=20=EB=B2=84?= =?UTF-8?q?=EC=A0=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- umc/build.gradle | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/umc/build.gradle b/umc/build.gradle index 8eb8f6a..417c92e 100644 --- a/umc/build.gradle +++ b/umc/build.gradle @@ -42,9 +42,13 @@ dependencies { implementation 'io.jsonwebtoken:jjwt-api:0.11.5' implementation 'io.jsonwebtoken:jjwt-impl:0.11.5' implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5' - implementation 'org.springdoc:springdoc-openapi-ui:1.6.15' + + implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' +// implementation 'org.springdoc:springdoc-openapi-ui:1.6.15' // implementation 'io.springfox:springfox-swagger2:2.9.2' - implementation 'io.springfox:springfox-swagger-ui:2.9.2' +// implementation 'io.springfox:springfox-swagger-ui:2.9.2' +// implementation 'io.springfox:springfox-boot-starter:3.0.0' +// implementation 'io.springfox:springfox-swagger-ui:3.0.0' } From e1a2350b3c0419f2b5588e98f88a2b52e3dab160 Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Tue, 2 Jul 2024 03:43:31 +0900 Subject: [PATCH 14/15] =?UTF-8?q?feat=20:=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=A2=8B=EC=95=84=EC=9A=94=EB=8A=94=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=EC=97=90=20=ED=95=9C=20=EB=B2=88?= =?UTF-8?q?=EB=A7=8C=20=EB=88=84=EB=A5=BC=20=EC=88=98=20=EC=9E=88=EC=9D=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/umc/common/config/SwaggerConfig.java | 4 +- .../umc/common/response/status/ErrorCode.java | 2 + .../post/controller/PostController.java | 32 +++++++++- .../post/repository/LikePostRepository.java | 15 +++++ .../post/repository/PostRepository.java | 4 ++ .../umc/domain/post/service/PostService.java | 58 ++++++++++++++++++- 6 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 umc/src/main/java/com/umc/domain/post/repository/LikePostRepository.java diff --git a/umc/src/main/java/com/umc/common/config/SwaggerConfig.java b/umc/src/main/java/com/umc/common/config/SwaggerConfig.java index c2a158a..7720770 100644 --- a/umc/src/main/java/com/umc/common/config/SwaggerConfig.java +++ b/umc/src/main/java/com/umc/common/config/SwaggerConfig.java @@ -15,8 +15,8 @@ public class SwaggerConfig { @Bean public OpenAPI SchrodingerApi() { Info info = new Info() - .title("BuddyU API") - .description("BuddyU API 명세서") + .title("제목") + .description("명세서 설명") .version("1.0.0"); String jwtSchemeName = "JWT TOKEN"; diff --git a/umc/src/main/java/com/umc/common/response/status/ErrorCode.java b/umc/src/main/java/com/umc/common/response/status/ErrorCode.java index d307c38..aa6da48 100644 --- a/umc/src/main/java/com/umc/common/response/status/ErrorCode.java +++ b/umc/src/main/java/com/umc/common/response/status/ErrorCode.java @@ -24,6 +24,8 @@ public enum ErrorCode implements BaseErrorCode { MEMBER_SIGNUP_ERROR(HttpStatus.BAD_REQUEST, "SIGNUP4001", "회원가입 유효성 검사 실패"), EMAIL_ALREADY_EXIST(HttpStatus.BAD_REQUEST, "SIGNUP4002", "이미 존재하는 이메일입니다."), + // 게시글 관련 에러 + LIKE_ALREADY_EXISTS(HttpStatus.BAD_REQUEST, "POST4001", "이미 좋아요를 눌렀습니다."), ; diff --git a/umc/src/main/java/com/umc/domain/post/controller/PostController.java b/umc/src/main/java/com/umc/domain/post/controller/PostController.java index cc7355c..beb32f7 100644 --- a/umc/src/main/java/com/umc/domain/post/controller/PostController.java +++ b/umc/src/main/java/com/umc/domain/post/controller/PostController.java @@ -45,7 +45,7 @@ public ApiResponse deleteById(@PathVariable String postId){ @CrossOrigin @Operation(summary = "게시글 생성 API") @PostMapping - public ApiResponse postPost(@RequestParam(name = "boardId") String boardId, + public ApiResponse postPost(@RequestParam(name = "boardId") String boardId, @ModelAttribute PostRequestDTO postRequestDto, @RequestParam(value = "images", required = false) MultipartFile[] images){ return postService.save(boardId, postRequestDto, images); @@ -54,9 +54,37 @@ public ApiResponse postPost(@RequestParam(name = "boardId") String board @CrossOrigin @Operation(summary = "게시글 수정 API") @PutMapping("/{postId}") - public ApiResponse updatePost(@PathVariable String postId, + public ApiResponse updatePost(@PathVariable String postId, @ModelAttribute PostRequestDTO postRequestDto, @RequestParam(value = "images", required = false) MultipartFile[] images){ return postService.update(postId, postRequestDto, images); } + + @CrossOrigin + @Operation(summary = "게시글 좋아요 API") + @PostMapping("/{postId}/likes") + public ApiResponse likePost(@PathVariable String postId){ + return postService.hitLike(postId); + } + + @CrossOrigin + @Operation(summary = "게시글 좋아요 개수 조회 API") + @GetMapping("/{postId}/likes") + public ApiResponse getLikes(@PathVariable String postId){ + return postService.getLikes(postId); + } + + @CrossOrigin + @Operation(summary = "사용자가 좋아요 누른 게시글 조회 API") + @GetMapping("/liked") + public ApiResponse> getLikedPosts(){ + return postService.getLikedPostsByUser(); + } + + @CrossOrigin + @Operation(summary = "게시글 좋아요 누름 여부 조회 API") + @GetMapping("/{postId}/liked") + public ApiResponse checkLike(@PathVariable String postId){ + return postService.checkLike(postId); + } } diff --git a/umc/src/main/java/com/umc/domain/post/repository/LikePostRepository.java b/umc/src/main/java/com/umc/domain/post/repository/LikePostRepository.java new file mode 100644 index 0000000..ad3d1d1 --- /dev/null +++ b/umc/src/main/java/com/umc/domain/post/repository/LikePostRepository.java @@ -0,0 +1,15 @@ +package com.umc.domain.post.repository; + +import com.umc.domain.post.entity.LikePost; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface LikePostRepository extends JpaRepository { + + boolean existsByPostIdAndMemberId(Long postId, Long memberId); + + int countByPostId(Long postId); +} diff --git a/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java b/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java index 5798c07..1c02237 100644 --- a/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java +++ b/umc/src/main/java/com/umc/domain/post/repository/PostRepository.java @@ -2,6 +2,7 @@ import com.umc.domain.post.entity.Post; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; @@ -10,4 +11,7 @@ public interface PostRepository extends JpaRepository { List findAllByBoardId(Long aLong); + + @Query("SELECT p FROM Post p JOIN LikePost lp ON p.id = lp.post.id WHERE lp.member.id = :memberId ORDER BY lp.createdAt DESC") + List findAllLikedPostsByMemberIdOrderByLikedTime(Long memberId); } diff --git a/umc/src/main/java/com/umc/domain/post/service/PostService.java b/umc/src/main/java/com/umc/domain/post/service/PostService.java index 8e1c9a7..eb0cf6a 100644 --- a/umc/src/main/java/com/umc/domain/post/service/PostService.java +++ b/umc/src/main/java/com/umc/domain/post/service/PostService.java @@ -1,6 +1,8 @@ package com.umc.domain.post.service; +import com.umc.common.jwt.SecurityUtil; import com.umc.common.response.ApiResponse; +import com.umc.common.response.status.ErrorCode; import com.umc.common.response.status.SuccessCode; import com.umc.domain.board.entity.Board; import com.umc.domain.board.repository.BoardRepository; @@ -8,8 +10,10 @@ import com.umc.domain.post.dto.PostRequestDTO; import com.umc.domain.post.dto.PostResponseDTO; import com.umc.domain.post.dto.SimplePostResponseDTO; +import com.umc.domain.post.entity.LikePost; import com.umc.domain.post.entity.Post; import com.umc.domain.post.entity.PostImage; +import com.umc.domain.post.repository.LikePostRepository; import com.umc.domain.post.repository.PostRepository; import com.umc.domain.user.entity.Member; import com.umc.domain.user.repository.MemberRepository; @@ -28,6 +32,7 @@ public class PostService { private final PostRepository postRepository; private final MemberRepository memberRepository; private final BoardRepository boardRepository; + private final LikePostRepository likePostRepository; private final PostImageService postImageService; @Transactional @@ -77,7 +82,7 @@ public ApiResponse delete(String postId) { return ApiResponse.of(SuccessCode._OK, "삭제되었습니다."); } - public ApiResponse save(String boardId, PostRequestDTO postRequest, MultipartFile[] imageFiles) { + public ApiResponse save(String boardId, PostRequestDTO postRequest, MultipartFile[] imageFiles) { Member writer = memberRepository.findByEmail( SecurityContextHolder.getContext().getAuthentication().getName() ).orElseThrow(); // 되나? @@ -97,7 +102,7 @@ public ApiResponse save(String boardId, PostRequestDTO postRequest, Mult return ApiResponse.of(SuccessCode._OK, "저장되었습니다"); } - public ApiResponse update(String postId, PostRequestDTO postRequest, MultipartFile[] imageFiles) { + public ApiResponse update(String postId, PostRequestDTO postRequest, MultipartFile[] imageFiles) { Post target = postRepository.findById(Long.getLong(postId)).orElseThrow(); target.setTitle(postRequest.getTitle()); target.setContent(postRequest.getContent()); @@ -110,4 +115,53 @@ public ApiResponse update(String postId, PostRequestDTO postRequest, Mul postRepository.save(target); return ApiResponse.of(SuccessCode._OK, "수정되었습니다"); } + + public ApiResponse hitLike(String postId) { + Post post = postRepository.findById(Long.getLong(postId)).orElseThrow(); + Member member = memberRepository.findByEmail( + SecurityContextHolder.getContext().getAuthentication().getName() + ).orElseThrow(); + + if(likePostRepository.existsByPostIdAndMemberId(post.getId(), member.getId())) + return ApiResponse.ofFailure(ErrorCode.LIKE_ALREADY_EXISTS, "이미 좋아요를 누른 게시글입니다"); + + LikePost likePost = LikePost.builder() + .post(post) + .member(member) + .build(); + likePostRepository.save(likePost); + return ApiResponse.of(SuccessCode._OK, post.getId() + " 게시물에 좋아요 완료 되었습니다"); + } + + + public ApiResponse getLikes(String postId) { + int likes = likePostRepository.countByPostId(Long.getLong(postId)); + return ApiResponse.of(SuccessCode._OK, likes); + } + + @Transactional + public ApiResponse> getLikedPostsByUser() { + Member member = memberRepository.findByEmail( + SecurityUtil.getCurrentUserEmail() + ).orElseThrow(); + List posts = postRepository.findAllLikedPostsByMemberIdOrderByLikedTime(member.getId()); + return ApiResponse.of(SuccessCode._OK, posts.stream() + .map(post -> SimplePostResponseDTO.builder() + .title(post.getTitle()) + .writerNickname(post.getWriter().getNickname()) + .createdAt(post.getCreatedAt()) + .modifiedAt(post.getModifiedAt()) + .build() + ).toList() + ); + } + + public ApiResponse checkLike(String postId) { + Member member = memberRepository.findByEmail( + SecurityUtil.getCurrentUserEmail() + ).orElseThrow(); + boolean liked = likePostRepository.existsByPostIdAndMemberId(Long.getLong(postId), member.getId()); + + return ApiResponse.of(SuccessCode._OK, liked); + } } From 6fa6eef4af656e75524e318413662701ea74e55a Mon Sep 17 00:00:00 2001 From: Jaehwan Hyun Date: Tue, 2 Jul 2024 03:45:01 +0900 Subject: [PATCH 15/15] =?UTF-8?q?fix=20:=20=ED=98=84=EC=9E=AC=20=EC=9C=A0?= =?UTF-8?q?=EC=A0=80=20=EC=9D=B4=EB=A9=94=EC=9D=BC=20SecurityUtil=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=EA=B0=80=EC=A0=B8=EC=98=A4=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/umc/domain/comment/service/CommentService.java | 4 ++-- .../main/java/com/umc/domain/post/service/PostService.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/umc/src/main/java/com/umc/domain/comment/service/CommentService.java b/umc/src/main/java/com/umc/domain/comment/service/CommentService.java index d2e04da..22c6b9d 100644 --- a/umc/src/main/java/com/umc/domain/comment/service/CommentService.java +++ b/umc/src/main/java/com/umc/domain/comment/service/CommentService.java @@ -1,5 +1,6 @@ package com.umc.domain.comment.service; +import com.umc.common.jwt.SecurityUtil; import com.umc.common.response.ApiResponse; import com.umc.common.response.status.SuccessCode; import com.umc.domain.comment.dto.CommentRequestDTO; @@ -10,7 +11,6 @@ import com.umc.domain.user.entity.Member; import com.umc.domain.user.repository.MemberRepository; import lombok.RequiredArgsConstructor; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.List; @@ -29,7 +29,7 @@ public ApiResponse> findByPostId(String postId) { public ApiResponse save(String postId, CommentRequestDTO commentRequest) { Member writer = memberRepository.findByEmail( - SecurityContextHolder.getContext().getAuthentication().getName() + SecurityUtil.getCurrentUserEmail() ).orElseThrow(); Post post = postRepository.findById(Long.getLong(postId)).orElseThrow(); Comment comment = Comment.builder() diff --git a/umc/src/main/java/com/umc/domain/post/service/PostService.java b/umc/src/main/java/com/umc/domain/post/service/PostService.java index eb0cf6a..6159221 100644 --- a/umc/src/main/java/com/umc/domain/post/service/PostService.java +++ b/umc/src/main/java/com/umc/domain/post/service/PostService.java @@ -18,7 +18,6 @@ import com.umc.domain.user.entity.Member; import com.umc.domain.user.repository.MemberRepository; import lombok.RequiredArgsConstructor; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -84,7 +83,7 @@ public ApiResponse delete(String postId) { public ApiResponse save(String boardId, PostRequestDTO postRequest, MultipartFile[] imageFiles) { Member writer = memberRepository.findByEmail( - SecurityContextHolder.getContext().getAuthentication().getName() + SecurityUtil.getCurrentUserEmail() ).orElseThrow(); // 되나? Board board = boardRepository.findById(Long.getLong(boardId)).orElseThrow(); // Post 엔티티 생성 @@ -119,7 +118,7 @@ public ApiResponse update(String postId, PostRequestDTO postRequest, Mul public ApiResponse hitLike(String postId) { Post post = postRepository.findById(Long.getLong(postId)).orElseThrow(); Member member = memberRepository.findByEmail( - SecurityContextHolder.getContext().getAuthentication().getName() + SecurityUtil.getCurrentUserEmail() ).orElseThrow(); if(likePostRepository.existsByPostIdAndMemberId(post.getId(), member.getId()))