diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..7fa232c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM amazoncorretto:11 +ARG JAR_FILE=build/libs/*.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/src/main/java/com/example/makingboard/application/CommentControllerV1.java b/src/main/java/com/example/makingboard/application/CommentControllerV1.java index d140952..9fb1b38 100644 --- a/src/main/java/com/example/makingboard/application/CommentControllerV1.java +++ b/src/main/java/com/example/makingboard/application/CommentControllerV1.java @@ -20,7 +20,7 @@ public class CommentControllerV1 { private final CommentService commentService; @GetMapping("/v1/comment/write") - public String writeComment(@RequestParam("id") Long postId,Model model) { + public String writeComment(@RequestParam("id") Long postId, Model model) { CommentRequest commentRequest = new CommentRequest(); commentRequest.setId(postId); model.addAttribute("commentRequest", commentRequest); @@ -28,16 +28,16 @@ public String writeComment(@RequestParam("id") Long postId,Model model) { } @PostMapping("/v1/comment/save") - public String saveComment(@RequestParam("id") Long postId,CommentRequest commentRequest, Model model) { - commentService.writeComment(commentRequest,postId); - List commentList = commentService.getCommentList(); - model.addAttribute("commentList", commentList); - return "board/commentList"; + public String saveComment(@RequestParam("id") Long postId, CommentRequest commentRequest, Model model) { + commentService.writeComment(commentRequest, postId); + List commentList = commentService.getCommentList(postId); + model.addAttribute("commentList", commentList); + return "board/commentList"; } @PostMapping("/v1/comment/delete") - public String deleteComment(@RequestParam(value = "commentId") Long id){ + public String deleteComment(@RequestParam(value = "commentId") Long id) { commentService.deleteComment(id); return "board/commentList"; } diff --git a/src/main/java/com/example/makingboard/application/PostControllerV1.java b/src/main/java/com/example/makingboard/application/PostControllerV1.java index c7cc5f3..614b1f8 100644 --- a/src/main/java/com/example/makingboard/application/PostControllerV1.java +++ b/src/main/java/com/example/makingboard/application/PostControllerV1.java @@ -1,15 +1,9 @@ package com.example.makingboard.application; -import com.example.makingboard.application.board.CommentService; import com.example.makingboard.application.board.PosterService; -import com.example.makingboard.application.board.dto.CommentResponse; import com.example.makingboard.application.board.dto.PosterRequest; import com.example.makingboard.application.board.dto.PosterResponse; import com.example.makingboard.application.board.dto.PosterUpdateRequest; -import com.example.makingboard.application.board.persistence.CommentRepository; -import com.example.makingboard.application.board.persistence.PosterRepository; -import com.example.makingboard.application.board.persistence.entity.Comment; -import com.example.makingboard.application.board.persistence.entity.Poster; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -17,7 +11,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; -import java.util.ArrayList; import java.util.List; @Controller @@ -48,28 +41,23 @@ public String openPosterList(Model model) { @GetMapping("/v1/posts/write") public String writePoster(Model model) { model.addAttribute("posterRequest", new PosterRequest()); - return "board/write"; } @PostMapping(value = "/v1/posts/save") - public String savePoster(PosterRequest posterRequest, Model model) { + public String savePoster(PosterRequest posterRequest) { posterService.savePoster(posterRequest); - List posterList = posterService.getPosterList(); - model.addAttribute("posterList", posterList); - return "board/list"; + return "redirect:/v1/posts/list"; } - @PostMapping (value = "/v1/posts/delete") - public String deletePoster(@RequestParam(value = "postId") Long id){ + @PostMapping(value = "/v1/posts/delete") + public String deletePoster(@RequestParam(value = "postId") Long id) { posterService.deletePoster(id); return "redirect:/v1/posts/list"; } - - @GetMapping("/v1/posts/update") public String updatePoster(@RequestParam("id") Long postId, Model model) { PosterUpdateRequest request = new PosterUpdateRequest(); @@ -85,6 +73,6 @@ public String updatePosterSave(PosterUpdateRequest posterUpdateRequest, Model mo List posterList = posterService.getPosterList(); model.addAttribute("posterList", posterList); return "board/list"; - } + } } diff --git a/src/main/java/com/example/makingboard/application/board/CommentService.java b/src/main/java/com/example/makingboard/application/board/CommentService.java index 55593d3..e9470d6 100644 --- a/src/main/java/com/example/makingboard/application/board/CommentService.java +++ b/src/main/java/com/example/makingboard/application/board/CommentService.java @@ -7,7 +7,6 @@ import com.example.makingboard.application.board.persistence.entity.Comment; import com.example.makingboard.application.board.persistence.entity.Poster; import com.example.makingboard.application.member.MemberService; -import com.example.makingboard.application.member.persistence.MemberRepository; import com.example.makingboard.application.member.persistence.entity.Member; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -15,7 +14,7 @@ import java.util.ArrayList; import java.util.List; -import java.util.Optional; +import java.util.stream.Collectors; @Service @@ -29,34 +28,32 @@ public class CommentService { @Transactional public CommentResponse writeComment(CommentRequest commentRequest, Long posterId) { Member member = memberService.getOrCreate(commentRequest.getUserName(), commentRequest.getPassword()); - Optional poster = posterRepository.findById(posterId); - Comment comment = new Comment().builder() - .post(poster.get()) - .member(member) - .text(commentRequest.getText()) - .build(); - commentRepository.save(comment); - return new CommentResponse(comment); - + Poster poster = posterRepository.findById(posterId).orElseThrow(RuntimeException::new); + Comment saved = commentRepository.save(poster.writeComment(member, commentRequest.getText())); + return new CommentResponse(saved); } + @Transactional public List getCommentList() { List commentList = commentRepository.findAll(); List commentResponseList = new ArrayList<>(); for (Comment comment : commentList) { - CommentResponse commentResponse = new CommentResponse(comment); - commentResponseList.add(commentResponse); + commentResponseList.add(new CommentResponse(comment)); } + return commentResponseList; } - - + @Transactional + public List getCommentList(Long postId) { + Poster poster = posterRepository.findById(postId).orElseThrow(RuntimeException::new); + return poster.getComments().stream().map(CommentResponse::new).collect(Collectors.toList()); + } //댓글 지우기 @Transactional - public void deleteComment(Long id){ + public void deleteComment(Long id) { commentRepository.deleteById(id); } } diff --git a/src/main/java/com/example/makingboard/application/board/PosterService.java b/src/main/java/com/example/makingboard/application/board/PosterService.java index dae56f2..dfc4652 100644 --- a/src/main/java/com/example/makingboard/application/board/PosterService.java +++ b/src/main/java/com/example/makingboard/application/board/PosterService.java @@ -15,8 +15,10 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; @Service @@ -51,36 +53,18 @@ public PosterResponse getPoster(Long id) { return new PosterResponse(poster); } + @Transactional public List getPosterList() { - List posterList = posterRepository.findAll(); - List posterResponseList = new ArrayList<>(); - - for (Poster poster : posterList) { - PosterResponse posterResponse = new PosterResponse(poster); - posterResponseList.add(posterResponse); - } - return posterResponseList; + return posterRepository.findAll().stream() + .map(PosterResponse::new).collect(Collectors.toList()); } @Transactional public void deletePoster(Long id) { - -// Optional poster = posterRepository.findById(id); -// List comments = poster.get().getComments(); - - List commentResponseList = commentService.getCommentList(); - for(CommentResponse commentResponse : commentResponseList){ - Long commentResponseId = commentResponse.getId(); - Comment comment= commentRepository.getById(commentResponseId); - Poster findPoster = comment.getPost(); - Poster checkPoster = posterRepository.getById(id); - if(findPoster == checkPoster){ - commentService.deleteComment(commentResponseId); - } - } - posterRepository.deleteById(id); + Poster poster = posterRepository.findById(id).orElseThrow(RuntimeException::new); + poster.getComments().forEach(x -> commentService.deleteComment(x.getId())); } } diff --git a/src/main/java/com/example/makingboard/application/board/persistence/entity/Comment.java b/src/main/java/com/example/makingboard/application/board/persistence/entity/Comment.java index 7c38e84..4bcab9f 100644 --- a/src/main/java/com/example/makingboard/application/board/persistence/entity/Comment.java +++ b/src/main/java/com/example/makingboard/application/board/persistence/entity/Comment.java @@ -41,8 +41,10 @@ public Comment(Member member, Poster post, String text) { public void modifyComment(String comment) { this.text = comment; } + public void setAuthor(Member member) { this.member = member; member.getComments().add(this); } + } diff --git a/src/main/java/com/example/makingboard/application/board/persistence/entity/Poster.java b/src/main/java/com/example/makingboard/application/board/persistence/entity/Poster.java index a784b20..969c45b 100644 --- a/src/main/java/com/example/makingboard/application/board/persistence/entity/Poster.java +++ b/src/main/java/com/example/makingboard/application/board/persistence/entity/Poster.java @@ -52,4 +52,12 @@ public void setAuthor(Member member) { this.member = member; member.getPosts().add(this); } + + public Comment writeComment(Member member, String text) { + return Comment.builder() + .post(this) + .member(member) + .text(text) + .build(); + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index af34c71..5d80f1f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,10 @@ -server.port=9801 +server.port=9810 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/board?characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=WangHye1! spring.jpa.show-sql=true -spring.jpa.hibernate.ddl-auto=none +spring.jpa.hibernate.ddl-auto=create spring.jpa.properties.hibernate.format_sql=true cloud.aws.credentials.access-key=AKIA2X3WOTDZ3ZIPWPEV cloud.aws.credentials.secret-key=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