This commit is contained in:
jinho jeong
2022-06-04 13:47:42 +09:00
parent 3088a7ea9f
commit 8d3499bcb0
9 changed files with 134 additions and 45 deletions

View File

@@ -2,34 +2,30 @@ package com.example.oneul.domain.post.api;
import javax.servlet.http.HttpSession;
import com.example.oneul.domain.post.domain.Post;
import com.example.oneul.domain.post.dto.PostDTO;
import com.example.oneul.domain.post.service.command.PostCommandService;
import com.example.oneul.domain.post.service.query.PostQueryService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.oneul.domain.post.domain.Post;
import com.example.oneul.domain.post.dto.PostDTO;
import com.example.oneul.domain.post.service.command.PostCommandService;
@RestController
@RequestMapping(value = "/post")
public class PostCommandApi {
private final PostCommandService postCommandService;
private final PostQueryService postQueryService;
public PostCommandApi(PostCommandService postCommandService, PostQueryService postQueryService){
public PostCommandApi(PostCommandService postCommandService){
this.postCommandService = postCommandService;
this.postQueryService = postQueryService;
}
@RequestMapping(value="/", method=RequestMethod.POST)
public Post createPost(HttpSession httpSession, @RequestBody PostDTO postDTO) {
Post post = postCommandService.createPost(postDTO.toEntity(), httpSession);
postQueryService.insertPost(post);
return post;
}

View File

@@ -1,8 +1,5 @@
package com.example.oneul.domain.post.api;
import com.example.oneul.domain.post.domain.Post;
import com.example.oneul.domain.post.service.query.PostQueryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PathVariable;
@@ -11,6 +8,9 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.oneul.domain.post.domain.PostDocument;
import com.example.oneul.domain.post.service.query.PostQueryService;
@@ -24,14 +24,14 @@ public class PostQueryApi {
}
@RequestMapping(value="", method=RequestMethod.GET)
public Page<Post> findAll(@RequestParam("page") Integer page) {
Page<Post> posts = postQueryService.findAll(PageRequest.of(page, 10));
public Page<PostDocument> findAll(@RequestParam("page") Integer page) {
Page<PostDocument> posts = postQueryService.findAll(PageRequest.of(page, 10));
return posts;
}
@RequestMapping(value="/writer/{writerId}", method=RequestMethod.GET)
public Page<Post> findAllByWriter(@PathVariable Long writerId, @RequestParam("page") Integer page) {
Page<Post> posts = postQueryService.findByWriter(writerId, PageRequest.of(page, 10));
public Page<PostDocument> findAllByWriter(@PathVariable Long writerId, @RequestParam("page") Integer page) {
Page<PostDocument> posts = postQueryService.findByWriter(writerId, PageRequest.of(page, 10));
return posts;
}
}

View File

@@ -1,11 +1,11 @@
package com.example.oneul.domain.post.dao.query;
import com.example.oneul.domain.post.domain.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface PostQueryRepository extends MongoRepository<Post, Long> {
Page<Post> findAll(Pageable pageable);
import com.example.oneul.domain.post.domain.PostDocument;
public interface PostQueryRepository extends MongoRepository<PostDocument, Long> {
Page<PostDocument> findAll(Pageable pageable);
}

View File

@@ -17,14 +17,12 @@ import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.example.oneul.domain.user.domain.UserEntity;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.data.mongodb.core.mapping.Document;
import com.example.oneul.domain.user.domain.UserEntity;
@Entity
@Document
@EntityListeners(AuditingEntityListener.class)
@Table(indexes = @Index(name = "i_post", columnList="createdAt"))
public class Post implements Serializable {

View File

@@ -0,0 +1,82 @@
package com.example.oneul.domain.post.domain;
import java.time.LocalDateTime;
import java.util.Objects;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class PostDocument {
private Long id;
private LocalDateTime createdAt;
private String content;
private String writer;
public PostDocument() {}
public PostDocument(Long id, LocalDateTime createdAt, String content, String writer){
this.id = id;
this.createdAt = createdAt;
this.content = content;
this.writer = writer;
}
public Long getId(){
return this.id;
}
public void setId(Long id){
this.id = id;
}
public LocalDateTime getCreatedAt(){
return this.createdAt;
}
public void setCreatedAt(LocalDateTime createdAt){
this.createdAt = createdAt;
}
public String getContent(){
return this.content;
}
public void setContent(String content){
this.content = content;
}
public String getWriter(){
return this.writer;
}
public void setWriter(String writer){
this.writer = writer;
}
@Override
public boolean equals(Object object){
if(this == object){
return true;
}
if(object == null || getClass() != object.getClass()){
return false;
}
PostDocument that = (PostDocument)object;
return this.id == that.id;
}
@Override
public int hashCode() {
return Objects.hash(this.id);
}
@Override
public String toString(){
return "PostDocumnet["
+ "id: " + this.id
+ ", createdAt: " + this.createdAt
+ ", content: " + this.content
+ ", writer: " + this.writer
+ "]";
}
}

View File

@@ -4,25 +4,29 @@ import java.time.LocalDateTime;
import javax.servlet.http.HttpSession;
import com.example.oneul.domain.post.dao.command.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;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.oneul.domain.post.dao.command.PostCommandRepository;
import com.example.oneul.domain.post.dao.query.PostQueryRepository;
import com.example.oneul.domain.post.domain.Post;
import com.example.oneul.domain.post.domain.PostDocument;
import com.example.oneul.domain.user.domain.UserEntity;
import com.example.oneul.global.error.exception.NotFoundException;
@Service
@Transactional
public class PostCommnadServiceImpl implements PostCommandService{
private final Logger log = LoggerFactory.getLogger(PostCommnadServiceImpl.class);
private final PostCommandRepository postCommandRepository;
public PostCommnadServiceImpl(PostCommandRepository postCommandRepository){
private final PostQueryRepository postQueryRepository;
public PostCommnadServiceImpl(PostCommandRepository postCommandRepository, PostQueryRepository postQueryRepository){
this.postCommandRepository = postCommandRepository;
this.postQueryRepository = postQueryRepository;
}
@Override
@@ -38,6 +42,14 @@ public class PostCommnadServiceImpl implements PostCommandService{
.writer(userEntity)
.build());
// TODO: 메시지 큐잉으로 전환
postQueryRepository.save(
new PostDocument(
postEntity.getId(),
postEntity.getCreatedAt(),
postEntity.getContent(),
postEntity.getWriter().getUsername()));
log.info("user: " + userEntity.toString() + " create " + postEntity.toString());
return postEntity;
}

View File

@@ -1,12 +1,12 @@
package com.example.oneul.domain.post.service.query;
import com.example.oneul.domain.post.domain.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import com.example.oneul.domain.post.domain.PostDocument;
public interface PostQueryService {
Page<Post> findAll(PageRequest pageRequest);
Page<Post> findByWriter(Long writerId, PageRequest pageRequest);
Post insertPost(Post post);
Page<PostDocument> findAll(PageRequest pageRequest);
Page<PostDocument> findByWriter(Long writerId, PageRequest pageRequest);
PostDocument insertPost(PostDocument post);
}

View File

@@ -1,13 +1,13 @@
package com.example.oneul.domain.post.service.query;
import com.example.oneul.domain.post.dao.query.PostQueryRepository;
import com.example.oneul.domain.post.domain.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.oneul.domain.post.dao.query.PostQueryRepository;
import com.example.oneul.domain.post.domain.PostDocument;
@Service
@Transactional
public class PostQueryServiceImpl implements PostQueryService {
@@ -18,17 +18,17 @@ public class PostQueryServiceImpl implements PostQueryService {
}
@Override
public Post insertPost(Post post){
public PostDocument insertPost(PostDocument post){
return postQueryRepository.insert(post);
}
@Override
public Page<Post> findAll(PageRequest pageRequest){
public Page<PostDocument> findAll(PageRequest pageRequest){
return postQueryRepository.findAll(pageRequest);
}
@Override
public Page<Post> findByWriter(Long writerId, PageRequest pageRequest){
public Page<PostDocument> findByWriter(Long writerId, PageRequest pageRequest){
return postQueryRepository.findAll(pageRequest);
}
}

View File

@@ -13,12 +13,12 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import com.example.oneul.domain.post.domain.Post;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.example.oneul.domain.post.domain.Post;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@EntityListeners(AuditingEntityListener.class)
public class UserEntity implements Serializable {
@@ -30,6 +30,7 @@ public class UserEntity implements Serializable {
private String password;
@JsonIgnore @CreatedDate
private LocalDateTime createdAt;
@JsonIgnore
@OneToMany(orphanRemoval = true, cascade = CascadeType.REMOVE)
private Set<Post> posts;