Skip to content
Snippets Groups Projects
Commit a00ef900 authored by Gwan Ju's avatar Gwan Ju
Browse files

feat : implement post creation, deletion, search, and comment functionality

parent 2f9b0ff0
No related branches found
No related tags found
1 merge request!1게시글 작성, 댓글 작성, 유저 관리 기능 추가
package com.umc.post.controller; package umc.spring.post.controller;
import com.umc.post.data.dto.PostDto; import com.fasterxml.jackson.databind.ObjectMapper;
import com.umc.post.data.entity.Post; import jakarta.servlet.http.HttpServletResponse;
import com.umc.post.service.service.PostService; 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.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 java.util.List;
import static org.springframework.data.jpa.domain.AbstractPersistable_.id;
@RestController @RestController
@RequestMapping("/api/posts")
public class PostController { public class PostController {
@Autowired
private final PostService postService; private final PostService postService;
public PostController(PostService postService) { public PostController(PostService postService) {
this.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){ public void upload(@RequestBody PostDto postDto){
postService.upload(postDto); postService.upload(postDto);
} }
@GetMapping("")
public List<Post> getAllPost(){
return postService.getAllPost();
} @GetMapping("/post/{id}")
@GetMapping("/{id}") public PostResDto getPostById(@PathVariable Long id){
public Post getPostById(@PathVariable Long id){
return postService.getPostById(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");
}
}
} }
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.PostDto;
import umc.spring.post.data.dto.PostResDto;
import umc.spring.post.data.entity.Post; import umc.spring.post.data.entity.Post;
import java.io.IOException;
import java.util.List; import java.util.List;
public interface PostService { public interface PostService {
void upload(PostDto postDto); void upload(PostDto postDto);
List<Post> getAllPost(); List<PostResDto> getAllPost();
Post getPostById(Long id); PostResDto getPostById(Long id);
void likeCrew(Long id); void likeCrew(Long id);
void dislikeCrew(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);
} }
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 org.springframework.stereotype.Service;
import umc.spring.post.data.dto.CommentDto;
import umc.spring.post.data.dto.PostDto; 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.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.io.IOException;
import java.util.Date; import java.util.*;
import java.util.List;
@Service @Service
public class PostServiceImpl implements PostService{ public class PostServiceImpl implements PostService{
@Autowired
private final PostRepository postRepository; private final PostRepository postRepository;
public PostServiceImpl(PostRepository postRepository) { @Autowired
private final CommentRepository commentRepository;
public PostServiceImpl(PostRepository postRepository, CommentRepository commentRepository) {
this.postRepository = postRepository; this.postRepository = postRepository;
this.commentRepository = commentRepository;
} }
@Override @Override
public void upload(PostDto postDto){ public void upload(PostDto postDto){
Post post = new Post(); Post post = new Post();
setPost(postDto, post); setPost(postDto, post);
post.setUserId(postDto.getUserId());
post.setCreatedTime((new Date())); post.setCreatedTime((new Date()));
post.setModifiedTime(post.getCreatedTime()); post.setModifiedTime(post.getCreatedTime());
postRepository.save(post); postRepository.save(post);
} }
@Override @Override
public List<Post> getAllPost(){ public List<PostResDto> getAllPost(){
return postRepository.findAll(); List<Post> posts = postRepository.findAll();
List<PostResDto> resDtos = new ArrayList<>();
posts.forEach(post -> {
PostResDto dto = PostResDto.toDTO(post);
resDtos.add(dto);
});
return resDtos;
} }
@Override @Override
public Post getPostById(Long id){ public PostResDto getPostById(Long id){
return postRepository.findById(id).get(); Post post = postRepository.findById(id).get();
return PostResDto.toDTO(post);
} }
@Override @Override
public void likeCrew(Long id) { public void likeCrew(Long id) {
Post post = getPostById(id); Post post = postRepository.findById(id).get();
int likeCount = post.getLikeCount(); int likeCount = post.getLikeCount();
post.setLikeCount(++likeCount); post.setLikeCount(++likeCount);
postRepository.save(post);
} }
@Override @Override
public void dislikeCrew(Long id) { public void dislikeCrew(Long id) {
Post post = getPostById(id); Post post = postRepository.findById(id).get();
int likeCount = post.getLikeCount(); int likeCount = post.getLikeCount();
if(likeCount!=0){ if(likeCount!=0){
post.setLikeCount(--likeCount); post.setLikeCount(--likeCount);
postRepository.save(post);
} }
} }
@Override @Override
public void deletePost(Long id) { public boolean deletePost(Long id) {
if(postRepository.findById(id).isPresent()){
postRepository.deleteById(id); postRepository.deleteById(id);
return true;
}
else return false;
} }
@Override @Override
public void editPost(PostDto postDto,Long id) { public boolean editPost(PostDto postDto, Long id) {
Post post = getPostById(id); Post post = postRepository.findById(id).get();
setPost(postDto,post); 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()); post.setModifiedTime(new Date());
postRepository.save(post);
return true;
}
else return false;
} }
@Override @Override
public List<Post> search(String title) { public List<PostResDto> search(String title) {
List<Post> postList = postRepository.findAll(); 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)){ if(post.getTitle().equals(title)){
findList.add(post); findList.add(PostResDto.toDTO(post));
} }
}); });
return findList; return findList;
} }
@Override @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) { private static void setPost(PostDto postDto, Post post) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment