This commit is contained in:
jinho jeong
2022-06-04 16:48:09 +09:00
parent 988827fea0
commit 882d3889c8
3 changed files with 14 additions and 3 deletions

View File

@@ -14,6 +14,7 @@ import com.example.oneul.domain.post.service.command.PostCommandService;
@RestController
@RequestMapping(value = "/post")
public class PostCommandApi {
@@ -35,4 +36,10 @@ public class PostCommandApi {
return post;
}
@RequestMapping(value="/{postId}/", method=RequestMethod.DELETE)
public String deletePost(HttpSession httpSession, @PathVariable Long postId) {
postCommandService.deletePost(postId, httpSession);
return postId + " is deleted";
}
}

View File

@@ -4,11 +4,11 @@ import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.oneul.domain.post.domain.Post;
import com.example.oneul.domain.user.domain.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PostCommandRepository extends JpaRepository<Post, Long> {
Post save(Post post);
Optional<Post> findByIdAndWriter(Long id, UserEntity writer);

View File

@@ -61,6 +61,9 @@ public class PostCommnadServiceImpl implements PostCommandService{
Post postEntity = postCommandRepository.findByIdAndWriter(id, userEntity).orElseThrow(() -> new NotFoundException(id + " post not found"));
postEntity.setConent(post.getContent());
postEntity = postCommandRepository.save(postEntity);
PostDocument postDocument = postQueryRepository.findById(postEntity.getId()).orElseThrow(() -> new NotFoundException("query repository doesn't have " + id));
postDocument.setContent(postEntity.getContent());
postQueryRepository.save(postDocument);
log.info(postEntity.toString() + " is updated");
return postEntity;
@@ -71,6 +74,7 @@ public class PostCommnadServiceImpl implements PostCommandService{
// TODO: 이 때 세션이 만기되면 어떡함
UserEntity userEntity = (UserEntity)httpSession.getAttribute("user");
postCommandRepository.deleteByIdAndWriter(id, userEntity);
postQueryRepository.deleteById(id);
log.info("post " + id + " is deleted");
}
}
}