post update and delete

This commit is contained in:
jinho jeong
2022-05-23 12:20:23 +09:00
parent 5bea957015
commit 9b5fe7623b
3 changed files with 31 additions and 4 deletions

View File

@@ -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);
}

View File

@@ -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");
}
}

View File

@@ -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);
}
}