diff --git a/src/main/java/umc/spring/post/controller/PostController.java b/src/main/java/umc/spring/post/controller/PostController.java index 0909aca648c0e31a35a7a2fda5ca9f02f19fed5e..ef71aec36276efc31c9238e8399fdbb2e7164457 100644 --- a/src/main/java/umc/spring/post/controller/PostController.java +++ b/src/main/java/umc/spring/post/controller/PostController.java @@ -1,33 +1,88 @@ -package com.umc.post.controller; +package umc.spring.post.controller; -import com.umc.post.data.dto.PostDto; -import com.umc.post.data.entity.Post; -import com.umc.post.service.service.PostService; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.transaction.Transactional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; +import umc.spring.post.data.dto.CommentDto; +import umc.spring.post.data.dto.PostDto; +import umc.spring.post.data.dto.PostResDto; +import umc.spring.post.data.entity.Post; +import umc.spring.post.service.PostService; import java.util.List; +import static org.springframework.data.jpa.domain.AbstractPersistable_.id; + @RestController -@RequestMapping("/api/posts") public class PostController { - + @Autowired private final PostService postService; - public PostController(PostService postService) { this.postService = postService; } + @ResponseStatus(HttpStatus.OK) + @GetMapping("/posts") + public List<PostResDto> getAllPost(){ + return postService.getAllPost(); + } - @PostMapping("/upload") + @ResponseStatus(HttpStatus.OK) + @PostMapping("/post/upload") public void upload(@RequestBody PostDto postDto){ postService.upload(postDto); } - @GetMapping("") - public List<Post> getAllPost(){ - return postService.getAllPost(); - } - @GetMapping("/{id}") - public Post getPostById(@PathVariable Long id){ + @GetMapping("/post/{id}") + public PostResDto getPostById(@PathVariable Long id){ return postService.getPostById(id); } + + @DeleteMapping("/post/{id}") + @ResponseStatus(HttpStatus.OK) + public void deletePost(@PathVariable Long id){ + if(!postService.deletePost(id)){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Post not found"); + } + } + @PutMapping("/post/{id}") + @ResponseStatus(HttpStatus.OK) + public void editPost(@RequestBody PostDto postDto,@PathVariable Long id){ + if(!postService.editPost(postDto,id)){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Post not found"); + } + } + @PostMapping("/post/likes") + @ResponseStatus(HttpStatus.OK) + public void likeCrew(@RequestParam Long id){ + postService.likeCrew(id); + } + @DeleteMapping ("/post/likes") + @ResponseStatus(HttpStatus.OK) + public void dislikeCrew(@RequestParam Long id){ + postService.dislikeCrew(id); + } + + @PostMapping("/post/search") + public List<PostResDto> search(@RequestBody PostDto postDto){ + return postService.search(postDto.getTitle()); + } + + @PostMapping("/post/comments") + @ResponseStatus(HttpStatus.OK) + public void addComment(@RequestBody CommentDto commentDto){ + postService.addComment(commentDto); + } + + @DeleteMapping("/post/comments") + @ResponseStatus(HttpStatus.OK) + public void deleteComment(@RequestParam Long id){ + if(!postService.deleteComment(id)){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Post not found"); + } + } } diff --git a/src/main/java/umc/spring/post/service/PostService.java b/src/main/java/umc/spring/post/service/PostService.java index f24774bcc84006b4b22b9df158a76cf189126a15..b3382a7aa1cda94e07ef117a6ed803a7ab1b46c1 100644 --- a/src/main/java/umc/spring/post/service/PostService.java +++ b/src/main/java/umc/spring/post/service/PostService.java @@ -1,29 +1,35 @@ -package umc.spring.post.service.service; +package umc.spring.post.service; +import jakarta.servlet.http.HttpServletResponse; +import umc.spring.post.data.dto.CommentDto; import umc.spring.post.data.dto.PostDto; +import umc.spring.post.data.dto.PostResDto; import umc.spring.post.data.entity.Post; +import java.io.IOException; import java.util.List; public interface PostService { void upload(PostDto postDto); - List<Post> getAllPost(); + List<PostResDto> getAllPost(); - Post getPostById(Long id); + PostResDto getPostById(Long id); void likeCrew(Long id); void dislikeCrew(Long id); - void deletePost(Long id); + boolean deletePost(Long id); - void editPost(PostDto postDto, Long id); + boolean editPost(PostDto postDto, Long id); - List<Post> search(String title); + List<PostResDto> search(String title); - void createComment(Long id); + void addComment(CommentDto commentDto); + + boolean deleteComment(Long id); } diff --git a/src/main/java/umc/spring/post/service/PostServiceImpl.java b/src/main/java/umc/spring/post/service/PostServiceImpl.java index fcb0dae82baef00b3904d41e74d341e3d62f9f6a..40082cc0328f460cc1a9d2962578f9d3991aa8d7 100644 --- a/src/main/java/umc/spring/post/service/PostServiceImpl.java +++ b/src/main/java/umc/spring/post/service/PostServiceImpl.java @@ -1,92 +1,153 @@ -package umc.spring.post.service.service; +package umc.spring.post.service; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import umc.spring.post.data.dto.CommentDto; import umc.spring.post.data.dto.PostDto; +import umc.spring.post.data.dto.PostResDto; +import umc.spring.post.data.entity.Comment; import umc.spring.post.data.entity.Post; -import umc.spring.post.repository.repository.PostRepository; +import umc.spring.post.repository.CommentRepository; +import umc.spring.post.repository.PostRepository; +import umc.spring.post.repository.UserRepository; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; +import java.io.IOException; +import java.util.*; @Service public class PostServiceImpl implements PostService{ + @Autowired private final PostRepository postRepository; - public PostServiceImpl(PostRepository postRepository) { + @Autowired + private final CommentRepository commentRepository; + + public PostServiceImpl(PostRepository postRepository, CommentRepository commentRepository) { this.postRepository = postRepository; + this.commentRepository = commentRepository; } @Override public void upload(PostDto postDto){ Post post = new Post(); setPost(postDto, post); + post.setUserId(postDto.getUserId()); post.setCreatedTime((new Date())); post.setModifiedTime(post.getCreatedTime()); postRepository.save(post); } @Override - public List<Post> getAllPost(){ - return postRepository.findAll(); + public List<PostResDto> getAllPost(){ + List<Post> posts = postRepository.findAll(); + List<PostResDto> resDtos = new ArrayList<>(); + posts.forEach(post -> { + PostResDto dto = PostResDto.toDTO(post); + resDtos.add(dto); + }); + return resDtos; } @Override - public Post getPostById(Long id){ - return postRepository.findById(id).get(); + public PostResDto getPostById(Long id){ + Post post = postRepository.findById(id).get(); + return PostResDto.toDTO(post); } @Override public void likeCrew(Long id) { - Post post = getPostById(id); + Post post = postRepository.findById(id).get(); int likeCount = post.getLikeCount(); post.setLikeCount(++likeCount); + postRepository.save(post); } @Override public void dislikeCrew(Long id) { - Post post = getPostById(id); + Post post = postRepository.findById(id).get(); int likeCount = post.getLikeCount(); if(likeCount!=0){ post.setLikeCount(--likeCount); + postRepository.save(post); + } } @Override - public void deletePost(Long id) { - postRepository.deleteById(id); + public boolean deletePost(Long id) { + if(postRepository.findById(id).isPresent()){ + postRepository.deleteById(id); + return true; + } + else return false; } @Override - public void editPost(PostDto postDto,Long id) { - Post post = getPostById(id); - setPost(postDto,post); - post.setModifiedTime(new Date()); + public boolean editPost(PostDto postDto, Long id) { + Post post = postRepository.findById(id).get(); + if(post!=null){ + post.setTitle(postDto.getTitle() != null ? postDto.getTitle() : post.getTitle()); + post.setBody(postDto.getBody() != null ? postDto.getBody() : post.getBody()); + post.setImage(postDto.getImage() != null ? postDto.getImage() : post.getImage()); + post.setModifiedTime(new Date()); + postRepository.save(post); + return true; + } + else return false; } @Override - public List<Post> search(String title) { + public List<PostResDto> search(String title) { List<Post> postList = postRepository.findAll(); - List<Post> findList = new ArrayList<>(); + List<PostResDto> findList = new ArrayList<>(); - postList.iterator().forEachRemaining(post->{ + postList.forEach(post->{ if(post.getTitle().equals(title)){ - findList.add(post); + findList.add(PostResDto.toDTO(post)); } }); return findList; } @Override - public void createComment(Long id) { + public void addComment(CommentDto commentDto){ + Comment comment = setComment(commentDto); + commentRepository.save(comment); + } - Post post = getPostById(id); + @Override + public boolean deleteComment(Long id) { + Optional<Comment> option = commentRepository.findById(id); + if(option.isPresent()){ + Comment comment = option.get(); + Post post = comment.getPost(); + if(post!=null){ + post.getComments().removeIf(data -> + data.getId().equals(id) + ); + postRepository.save(post); + } + return true; + } + else return false; + } - post.set + private Comment setComment(CommentDto commentDto) { + Comment comment = new Comment(); + Post post = postRepository.findById(commentDto.getPostId()).get(); + post.getComments().add(comment); + comment.setPost(post); + comment.setUserId(commentDto.getUserId()); + comment.setTimestamp(new Date()); + comment.setText(commentDto.getText()); + comment.setAuthor(commentDto.getAuthor()); + comment.setPostId(commentDto.getPostId()); + return comment; } private static void setPost(PostDto postDto, Post post) {