post command repository

This commit is contained in:
jinho jeong
2022-05-03 16:43:58 +09:00
parent 7e30c938e7
commit d630c568e4
7 changed files with 212 additions and 1 deletions

View File

@@ -0,0 +1,9 @@
package com.example.oneul.domain.post.dao;
import com.example.oneul.domain.post.domain.Post;
public interface PostCommandRepository{
Post save(Post post);
Post update(Post post);
void delete(Long id);
}

View File

@@ -0,0 +1,14 @@
package com.example.oneul.domain.post.dao;
import java.util.Optional;
import com.example.oneul.domain.post.domain.Post;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PostQueryRepository extends CrudRepository<Post, Long>{
Optional<Post> findById(Long id);
Iterable<Post> findAll();
}

View File

@@ -0,0 +1,40 @@
package com.example.oneul.domain.post.dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.LockModeType;
import javax.transaction.Transactional;
import com.example.oneul.domain.post.domain.Post;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.stereotype.Repository;
@Repository
@Transactional
public class PostQueryRepositoryImpl implements PostCommandRepository {
private final EntityManager em;
public PostQueryRepositoryImpl(EntityManagerFactory entityManagerFactory){
this.em = entityManagerFactory.createEntityManager();
}
@Override
public Post save(Post post) {
em.persist(post);
return post;
}
@Override
@Lock(LockModeType.PESSIMISTIC_WRITE)
public Post update(Post post){
return em.merge(post);
}
@Override
public void delete(Long id){
em.createQuery("delete from Post where id = :id")
.setParameter("id", id)
.executeUpdate();
}
}

View File

@@ -99,6 +99,10 @@ public class Post {
+ "]";
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private Long id;
private String content;

View File

@@ -0,0 +1,14 @@
package com.example.oneul.domain.post.service.command;
import javax.servlet.http.HttpSession;
import com.example.oneul.domain.post.domain.Post;
import org.springframework.stereotype.Service;
@Service
public interface PostCommandService {
Post createPost(Post post, HttpSession httpSession);
Post updatePost(Long id, Post post, HttpSession httpSession);
void deletePost(Long id, HttpSession httpSession);
}

View File

@@ -0,0 +1,44 @@
package com.example.oneul.domain.post.service.command;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class PostCommnadServiceImpl implements PostCommandService{
private final PostCommandRepository postCommandRepository;
private final Logger log = LoggerFactory.getLogger(PostCommnadServiceImpl.class);
public PostCommnadServiceImpl(PostCommandRepository postCommandRepository){
this.postCommandRepository = postCommandRepository;
}
@Override
public Post createPost(Post post, HttpSession httpSession){
UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
return postCommandRepository.save(
Post.builder()
.content(post.getContent())
.writer(userEntity)
.build());
}
@Override
public Post updatePost(Long id, Post post, HttpSession httpSession){
return new Post();
}
@Override
public void deletePost(Long id, HttpSession httpSession){
}
}

View File

@@ -0,0 +1,86 @@
package com.example.oneul.repository;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.example.oneul.domain.post.dao.PostCommandRepository;
import com.example.oneul.domain.post.domain.Post;
import com.example.oneul.domain.user.dao.UserRepository;
import com.example.oneul.domain.user.domain.UserEntity;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
@ActiveProfiles("test")
@SpringBootTest
public class PostCommandServiceTest {
@Autowired private PostCommandRepository postCommandRepository;
@Autowired private UserRepository userRepository;
static {
GenericContainer redis = new GenericContainer("redis:3-alpine")
.withExposedPorts(6379);
redis.start();
System.setProperty("spring.redis.host", redis.getContainerIpAddress());
System.setProperty("spring.redis.port", redis.getFirstMappedPort() + "");
}
private UserEntity createTestUser(){
return userRepository.save(UserEntity.builder()
.username("test user")
.password("test pw")
.build());
}
@Test
public void saveTest(){
UserEntity testUser = createTestUser();
Post post = Post.builder()
.content("test content")
.writer(testUser)
.build();
Post createdPost = postCommandRepository.save(post);
assertEquals(false, createdPost.getId() == null);
assertEquals(post, createdPost);
}
@Test
public void updateTest(){
UserEntity testUser = createTestUser();
Post post = Post.builder()
.content("test content")
.writer(testUser)
.build();
Post createdPost = postCommandRepository.save(post);
createdPost.setConent("updated");
Post updatedPost = postCommandRepository.update(createdPost);
assertEquals(createdPost, updatedPost);;
}
// TODO: 왜 Transaction이 없다고 할까
@Test
public void deleteTest(){
UserEntity testUser = createTestUser();
Post post = Post.builder()
.content("test content")
.writer(testUser)
.build();
Post createdPost = postCommandRepository.save(post);
Long id = createdPost.getId();
postCommandRepository.delete(id);
}
}