post update and delete
This commit is contained in:
@@ -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, Long> {
|
||||
Post save(Post post);
|
||||
Optional<Post> findByIdAndWriter(Long id, UserEntity writer);
|
||||
void deleteById(Long id);
|
||||
void delete(Post post);
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user