From 9b5fe7623b72748b6f145818222d514ab8a0743d Mon Sep 17 00:00:00 2001 From: jinho jeong Date: Mon, 23 May 2022 12:20:23 +0900 Subject: [PATCH] post update and delete --- .../post/dao/PostCommandRepository.java | 4 ++++ .../command/PostCommnadServiceImpl.java | 20 +++++++++++++++---- .../error/exception/NotFoundException.java | 11 ++++++++++ 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/example/oneul/global/error/exception/NotFoundException.java diff --git a/src/main/java/com/example/oneul/domain/post/dao/PostCommandRepository.java b/src/main/java/com/example/oneul/domain/post/dao/PostCommandRepository.java index b31d76b..df1745b 100644 --- a/src/main/java/com/example/oneul/domain/post/dao/PostCommandRepository.java +++ b/src/main/java/com/example/oneul/domain/post/dao/PostCommandRepository.java @@ -1,11 +1,15 @@ package com.example.oneul.domain.post.dao; +import java.util.Optional; + import com.example.oneul.domain.post.domain.Post; +import com.example.oneul.domain.user.domain.UserEntity; import org.springframework.data.repository.CrudRepository; public interface PostCommandRepository extends CrudRepository { Post save(Post post); + Optional findByIdAndWriter(Long id, UserEntity writer); void deleteById(Long id); void delete(Post post); } diff --git a/src/main/java/com/example/oneul/domain/post/service/command/PostCommnadServiceImpl.java b/src/main/java/com/example/oneul/domain/post/service/command/PostCommnadServiceImpl.java index c06e640..628a1fd 100644 --- a/src/main/java/com/example/oneul/domain/post/service/command/PostCommnadServiceImpl.java +++ b/src/main/java/com/example/oneul/domain/post/service/command/PostCommnadServiceImpl.java @@ -5,6 +5,7 @@ import javax.servlet.http.HttpSession; import com.example.oneul.domain.post.dao.PostCommandRepository; import com.example.oneul.domain.post.domain.Post; import com.example.oneul.domain.user.domain.UserEntity; +import com.example.oneul.global.error.exception.NotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,20 +26,31 @@ public class PostCommnadServiceImpl implements PostCommandService{ @Override public Post createPost(Post post, HttpSession httpSession){ UserEntity userEntity = (UserEntity) httpSession.getAttribute("user"); - return postCommandRepository.save( + + Post postEntity = postCommandRepository.save( Post.builder() .content(post.getContent()) .writer(userEntity) .build()); + + log.info("user: " + userEntity.toString() + " create " + post.toString()); + return postEntity; } @Override - public Post updatePost(Long id, Post post, HttpSession httpSession){ - return new Post(); + public Post updatePost(Long id, Post post, HttpSession httpSession){ + UserEntity userEntity = (UserEntity) httpSession.getAttribute("user"); + Post postEntity = postCommandRepository.findByIdAndWriter(id, userEntity).orElseThrow(() -> new NotFoundException(id + " post not found")); + postEntity.setConent(post.getContent()); + postEntity = postCommandRepository.save(postEntity); + log.info(postEntity.toString() + " is updated"); + + return postEntity; } @Override public void deletePost(Long id, HttpSession httpSession){ - + postCommandRepository.deleteById(id); + log.info("post " + id + " is deleted"); } } diff --git a/src/main/java/com/example/oneul/global/error/exception/NotFoundException.java b/src/main/java/com/example/oneul/global/error/exception/NotFoundException.java new file mode 100644 index 0000000..3d57241 --- /dev/null +++ b/src/main/java/com/example/oneul/global/error/exception/NotFoundException.java @@ -0,0 +1,11 @@ +package com.example.oneul.global.error.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "entity not found") +public class NotFoundException extends RuntimeException { + public NotFoundException(String message){ + super(message); + } +}