Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
package com.blog.domain.board.application.dto;

import com.blog.domain.board.domain.entity.Post;
import com.blog.domain.comment.domain.entity.Comment;
import com.blog.domain.user.domain.entity.User;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;
import java.util.UUID;

import lombok.Builder;

@Builder
public record PostReadAllResponse(
@Schema(description = "게시글 id", example = "3fa85f64-5717-4562-b3fc-2c963f66afa6")
UUID postId,
@Schema(description = "게시글 제목", example = "게시글 제목")
String title,
@ArraySchema(arraySchema = @Schema(implementation = ContentDto.class))
List<ContentDto> contents,
@Schema(description = "게시글 소유 여부", example = "true")
Boolean isOwner,
@Schema(description = "댓글 개수", example = "1")
int commentCount
List<PostReadResponse> post,
long pageMax
) {
public static PostReadAllResponse toResponse(Post post, User user, List<ContentDto> contentDtos,
List<Comment> comments) {
return PostReadAllResponse.builder()
.postId(post.getId())
.title(post.getTitle())
.contents(contentDtos)
.isOwner(user != null && post.getUser().equals(user))
.commentCount(comments.size())
.build();
}
public static PostReadAllResponse toResponse(List<PostReadResponse> postDtos, int pageSize) {
return PostReadAllResponse.builder()
.post(postDtos)
.pageMax(pageSize)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.blog.domain.board.application.usecase;

import com.blog.domain.board.application.dto.ContentDto;
import java.util.List;
import java.util.UUID;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blog.domain.board.application.dto.PostCreateRequest;
import com.blog.domain.board.application.dto.PostReadAllResponse;
import com.blog.domain.board.application.dto.PostReadResponse;
Expand All @@ -20,105 +26,106 @@
import com.blog.domain.comment.domain.service.CommentGetService;
import com.blog.domain.user.domain.entity.User;
import com.blog.domain.user.domain.service.UserGetService;
import java.util.List;
import java.util.UUID;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class PostManageUsecase {
private final UserGetService userGetService;
private final PostSaveService postSaveService;
private final PostGetService postGetService;
private final PostUpdateService postUpdateService;
private final PostValidateService postValidateService;
private final PostDeleteService postDeleteService;
private final CommentGetService commentGetService;
private final CommentDeleteService commentDeleteService;
private final ContentSaveService contentCreateService;
private final ContentGetService contentGetService;
private final ContentDeleteService contentDeleteService;

@Transactional
public void createPost(Long userId, PostCreateRequest dto) {
User user = userGetService.find(userId);
Post post = Post.CreatePost(dto.title(), user);

contentCreateService.create(dto.contents(), post);
postSaveService.save(post);
}

@Transactional(readOnly = true)
public PostReadResponse readPost(Long userId, UUID postId) {
User user = userGetService.find(userId);
Post post = postGetService.find(postId);

List<Comment> comments = commentGetService.findALlByPost(post);
List<Content> contents = contentGetService.findAll(post);

return PostReadResponse.toResponse(post, user, contents, comments);
}

@Transactional(readOnly = true)
public PostReadResponse readPostNoToken(UUID postId) {
Post post = postGetService.find(postId);
List<Comment> comments = commentGetService.findALlByPost(post);
List<Content> contents = contentGetService.findAll(post);

return PostReadResponse.toResponse(post, null, contents, comments);
}

@Transactional(readOnly = true)
public List<PostReadAllResponse> readAllPost(Long userId, int size, int page) {
User user = userGetService.find(userId);
List<Post> posts = postGetService.findAll(size, page);

return posts.stream()
.map(post -> {
List<Content> contents = contentGetService.findAll(post);
List<Comment> comments = commentGetService.findALlByPost(post);
return PostReadAllResponse.toResponse(post, user,
contents.stream().map(ContentDto::fromContent).toList(), comments);
}).toList();
}

@Transactional(readOnly = true)
public List<PostReadAllResponse> readAllPostNoToken(int size, int page) {
List<Post> posts = postGetService.findAll(size, page);

return posts.stream()
.map(post -> {
List<Content> contents = contentGetService.findAll(post);
List<Comment> comments = commentGetService.findALlByPost(post);
return PostReadAllResponse.toResponse(post, null,
contents.stream().map(ContentDto::fromContent).toList(), comments);
}).toList();
}

@Transactional
public void updatePost(Long userId, UUID postId, PostUpdateRequest dto) {
User user = userGetService.find(userId);
Post post = postGetService.find(postId);

postValidateService.certificate(post, user);

contentDeleteService.deleteAllByPost(post);
contentCreateService.create(dto.contents(), post);

postUpdateService.update(post, dto);
}

@Transactional
public void deletePost(Long userId, UUID postId) {
User user = userGetService.find(userId);
Post post = postGetService.find(postId);

postValidateService.certificate(post, user);

contentDeleteService.deleteAllByPost(post);
commentDeleteService.deleteAll(post);
postDeleteService.delete(post);
}
private final UserGetService userGetService;
private final PostSaveService postSaveService;
private final PostGetService postGetService;
private final PostUpdateService postUpdateService;
private final PostValidateService postValidateService;
private final PostDeleteService postDeleteService;
private final CommentGetService commentGetService;
private final CommentDeleteService commentDeleteService;
private final ContentSaveService contentCreateService;
private final ContentGetService contentGetService;
private final ContentDeleteService contentDeleteService;

@Transactional
public void createPost(Long userId, PostCreateRequest dto) {
User user = userGetService.find(userId);
Post post = Post.CreatePost(dto.title(), user);

contentCreateService.create(dto.contents(), post);
postSaveService.save(post);
}

@Transactional(readOnly = true)
public PostReadResponse readPost(Long userId, UUID postId) {
User user = userGetService.find(userId);
Post post = postGetService.find(postId);

List<Comment> comments = commentGetService.findALlByPost(post);
List<Content> contents = contentGetService.findAll(post);

return PostReadResponse.toResponse(post, user, contents, comments);
}

@Transactional(readOnly = true)
public PostReadResponse readPostNoToken(UUID postId) {
Post post = postGetService.find(postId);
List<Comment> comments = commentGetService.findALlByPost(post);
List<Content> contents = contentGetService.findAll(post);

return PostReadResponse.toResponse(post, null, contents, comments);
}

@Transactional(readOnly = true)
public PostReadAllResponse readAllPost(Long userId, int size, int page) {
User user = userGetService.find(userId);
Page<Post> postPage = postGetService.findAll(size, page);
List<Post> posts = postPage.getContent();

List<PostReadResponse> dtos = posts.stream()
.map(post -> {
List<Content> contents = contentGetService.findAll(post);
List<Comment> comments = commentGetService.findALlByPost(post);
return PostReadResponse.toResponse(post, user, contents, comments);
}).toList();

return PostReadAllResponse.toResponse(dtos, postPage.getTotalPages());
}

@Transactional(readOnly = true)
public PostReadAllResponse readAllPostNoToken(int size, int page) {
Page<Post> postPage = postGetService.findAll(size, page);
List<Post> posts = postPage.getContent();

List<PostReadResponse> dtos = posts.stream()
.map(post -> {
List<Content> contents = contentGetService.findAll(post);
List<Comment> comments = commentGetService.findALlByPost(post);
return PostReadResponse.toResponse(post, null, contents, comments);
}).toList();

return PostReadAllResponse.toResponse(dtos, postPage.getTotalPages());
}

@Transactional
public void updatePost(Long userId, UUID postId, PostUpdateRequest dto) {
User user = userGetService.find(userId);
Post post = postGetService.find(postId);

postValidateService.certificate(post, user);

contentDeleteService.deleteAllByPost(post);
contentCreateService.create(dto.contents(), post);

postUpdateService.update(post, dto);
}

@Transactional
public void deletePost(Long userId, UUID postId) {
User user = userGetService.find(userId);
Post post = postGetService.find(postId);

postValidateService.certificate(post, user);

contentDeleteService.deleteAllByPost(post);
commentDeleteService.deleteAll(post);
postDeleteService.delete(post);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package com.blog.domain.board.domain.service;

import com.blog.domain.board.domain.entity.Post;
import com.blog.domain.board.domain.repository.PostRepository;
import com.blog.domain.board.exception.PostNotFoundException;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.blog.domain.board.domain.entity.Post;
import com.blog.domain.board.domain.repository.PostRepository;
import com.blog.domain.board.exception.PostNotFoundException;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class PostGetService {
private final PostRepository postRepository;
private final PostRepository postRepository;

public Post find(UUID postId) {
return postRepository.findById(postId).orElseThrow(PostNotFoundException::new);
}
public Post find(UUID postId) {
return postRepository.findById(postId).orElseThrow(PostNotFoundException::new);
}

public List<Post> findAll(int size, int page) {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
return postRepository.findAll(pageable).getContent();
}
public Page<Post> findAll(int size, int page) {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
return postRepository.findAll(pageable);
}
}
Loading