Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Do not merge] Compare cod before apply stream #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM amazoncorretto:11
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ 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);
return "board/commentWrite";
}

@PostMapping("/v1/comment/save")
public String saveComment(@RequestParam("id") Long postId,CommentRequest commentRequest, Model model) {
commentService.writeComment(commentRequest,postId);
List<CommentResponse> 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<CommentResponse> 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";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
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;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

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

@Controller
Expand Down Expand Up @@ -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<PosterResponse> 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();
Expand All @@ -85,6 +73,6 @@ public String updatePosterSave(PosterUpdateRequest posterUpdateRequest, Model mo
List<PosterResponse> posterList = posterService.getPosterList();
model.addAttribute("posterList", posterList);
return "board/list";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
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;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


@Service
Expand All @@ -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> 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<CommentResponse> getCommentList() {
List<Comment> commentList = commentRepository.findAll();
List<CommentResponse> 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<CommentResponse> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,36 +53,18 @@ public PosterResponse getPoster(Long id) {
return new PosterResponse(poster);
}


@Transactional
public List<PosterResponse> getPosterList() {
List<Poster> posterList = posterRepository.findAll();
List<PosterResponse> 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> poster = posterRepository.findById(id);
// List<Comment> comments = poster.get().getComments();

List<CommentResponse> 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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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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
Expand Down