Skip to content

Commit

Permalink
#24 - 서비스 로직 테스트 코드 업데이트 및 DTO 재설계, 쿼리 메소드 작성
Browse files Browse the repository at this point in the history
테스트 코드를 작성하고 연관된 DTO 를 재설계함
테스트가 필요로 하는 repository 쿼리 메소드도 작성

댓글 Repo 에 존재하는
List<ArticleComment> findByArticle_Id(Long articleId);
Article 테이블에서 id를 조회 ((_) == `의`)
  • Loading branch information
rhakdnj committed Aug 9, 2022
1 parent b4bc5e8 commit 1c24d49
Show file tree
Hide file tree
Showing 14 changed files with 598 additions and 57 deletions.
45 changes: 39 additions & 6 deletions src/main/java/com/example/boardproject/dto/ArticleCommentDto.java
@@ -1,19 +1,52 @@
package com.example.boardproject.dto;


import com.example.boardproject.domain.Article;
import com.example.boardproject.domain.ArticleComment;

import java.time.LocalDateTime;

public record ArticleCommentDto(
Long id,
Long articleId,
UserDto userDto,
String content,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy,
String content
String modifiedBy
) {
public static ArticleCommentDto of(LocalDateTime createdAt,

public static ArticleCommentDto of(Long id,
Long articleId,
UserDto userDto,
String content,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy,
String content) {
return new ArticleCommentDto(createdAt, createdBy, modifiedAt, modifiedBy, content);
String modifiedBy) {
return new ArticleCommentDto(id, articleId, userDto, content, createdAt, createdBy, modifiedAt, modifiedBy);
}

public static ArticleCommentDto from(ArticleComment entity) {
return new ArticleCommentDto(
entity.getId(),
entity.getArticle().getId(),
UserDto.from(entity.getUser()),
entity.getContent(),
entity.getCreatedAt(),
entity.getCreatedBy(),
entity.getModifiedAt(),
entity.getModifiedBy()
);
}

public ArticleComment toEntity(Article entity) {
return ArticleComment.of(
entity,
userDto.toEntity(),
content
);
}

}
48 changes: 41 additions & 7 deletions src/main/java/com/example/boardproject/dto/ArticleDto.java
@@ -1,20 +1,54 @@
package com.example.boardproject.dto;

import com.example.boardproject.domain.Article;

import java.time.LocalDateTime;

public record ArticleDto(
LocalDateTime createdAt,
String createdBy,
Long id,
UserDto userDto,
String title,
String content,
String hashtag
String hashtag,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy
) {
public static ArticleDto of(LocalDateTime createdAt,
String createdBy,
public static ArticleDto of(Long id,
UserDto userDto,
String title,
String content,
String hashtag) {
return new ArticleDto(createdAt, createdBy, title, content, hashtag);
String hashtag,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy) {
return new ArticleDto(id, userDto, title, content, hashtag, createdAt, createdBy, modifiedAt, modifiedBy);
}

// 위 아래의 2가지 method 를 통해 dto -> domain 영향을 없앰
public static ArticleDto from(Article entity) {
return new ArticleDto(
entity.getId(),
UserDto.from(entity.getUser()),
entity.getTitle(),
entity.getContent(),
entity.getHashtag(),
entity.getCreatedAt(),
entity.getCreatedBy(),
entity.getModifiedAt(),
entity.getModifiedBy()
);
}

public Article toEntity() {
return Article.of(
userDto.toEntity(),
title,
content,
hashtag
);
}

}
11 changes: 0 additions & 11 deletions src/main/java/com/example/boardproject/dto/ArticleUpdateDto.java

This file was deleted.

@@ -0,0 +1,54 @@
package com.example.boardproject.dto;

import com.example.boardproject.domain.Article;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

public record ArticleWithCommentsDto(
Long id,
UserDto userDto,
Set<ArticleCommentDto> articleCommentDtos,
String title,
String content,
String hashtag,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy
) {

public static ArticleWithCommentsDto of(Long id,
UserDto userAccountDto,
Set<ArticleCommentDto> articleCommentDtos,
String title,
String content,
String hashtag,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy) {
return new ArticleWithCommentsDto(id, userAccountDto, articleCommentDtos, title,
content, hashtag, createdAt, createdBy, modifiedAt, modifiedBy);
}

public static ArticleWithCommentsDto from(Article entity) {
return new ArticleWithCommentsDto(
entity.getId(),
UserDto.from(entity.getUser()),
entity.getArticleComments().stream()
.map(ArticleCommentDto::from)
.collect(Collectors.toCollection(LinkedHashSet::new)),
entity.getTitle(),
entity.getContent(),
entity.getHashtag(),
entity.getCreatedAt(),
entity.getCreatedBy(),
entity.getModifiedAt(),
entity.getModifiedBy()
);
}

}
54 changes: 54 additions & 0 deletions src/main/java/com/example/boardproject/dto/UserDto.java
@@ -0,0 +1,54 @@
package com.example.boardproject.dto;

import com.example.boardproject.domain.User;

import java.time.LocalDateTime;

public record UserDto(
Long id,
String email,
String userPassword,
String nickname,
String memo,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy
) {

public static UserDto of(Long id,
String email,
String userPassword,
String nickname,
String memo,
LocalDateTime createdAt,
String createdBy,
LocalDateTime modifiedAt,
String modifiedBy) {
return new UserDto(id, email, userPassword, nickname, memo, createdAt, createdBy, modifiedAt, modifiedBy);
}

public static UserDto from(User entity) {
return new UserDto(
entity.getId(),
entity.getEmail(),
entity.getUserPassword(),
entity.getNickname(),
entity.getMemo(),
entity.getCreatedAt(),
entity.getCreatedBy(),
entity.getModifiedAt(),
entity.getModifiedBy()
);
}

public User toEntity() {
return User.of(
email,
userPassword,
nickname,
memo
);
}

}
@@ -0,0 +1,33 @@
package com.example.boardproject.dto.response;

import com.example.boardproject.dto.ArticleCommentDto;

import java.time.LocalDateTime;

public record ArticleCommentResponse(
Long id,
String content,
LocalDateTime createdAt,
String email,
String nickname
) {

public static ArticleCommentResponse of(Long id, String content, LocalDateTime createdAt, String email, String nickname) {
return new ArticleCommentResponse(id, content, createdAt, email, nickname);
}

public static ArticleCommentResponse from(ArticleCommentDto dto) {
String nickname = dto.userDto().nickname();
if (nickname == null || nickname.isBlank()) {
nickname = dto.userDto().email();
}

return new ArticleCommentResponse(
dto.id(),
dto.content(),
dto.createdAt(),
dto.userDto().email(),
nickname
);
}
}
@@ -0,0 +1,38 @@
package com.example.boardproject.dto.response;

import com.example.boardproject.dto.ArticleDto;

import java.time.LocalDateTime;

public record ArticleResponse(
Long id,
String title,
String content,
String hashtag,
LocalDateTime createdAt,
String email,
String nickname
) {

public static ArticleResponse of(Long id, String title, String content, String hashtag, LocalDateTime createdAt, String email, String nickname) {
return new ArticleResponse(id, title, content, hashtag, createdAt, email, nickname);
}

public static ArticleResponse from(ArticleDto dto) {
String nickname = dto.userDto().nickname();
if (nickname == null || nickname.isBlank()) {
nickname = dto.userDto().email();
}

return new ArticleResponse(
dto.id(),
dto.title(),
dto.content(),
dto.hashtag(),
dto.createdAt(),
dto.userDto().email(),
nickname
);
}

}
@@ -0,0 +1,45 @@
package com.example.boardproject.dto.response;

import com.example.boardproject.dto.ArticleWithCommentsDto;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

public record ArticleWithCommentResponse(
Long id,
String title,
String content,
String hashtag,
LocalDateTime createdAt,
String email,
String nickname,
Set<ArticleCommentResponse> articleCommentResponses
) {

public static ArticleWithCommentResponse of(Long id, String title, String content, String hashtag, LocalDateTime createdAt, String email, String nickname, Set<ArticleCommentResponse> articleCommentResponses) {
return new ArticleWithCommentResponse(id, title, content, hashtag, createdAt, email, nickname, articleCommentResponses);
}

public static ArticleWithCommentResponse from(ArticleWithCommentsDto dto) {
String nickname = dto.userDto().nickname();
if (nickname == null || nickname.isBlank()) {
nickname = dto.userDto().email();
}

return new ArticleWithCommentResponse(
dto.id(),
dto.title(),
dto.content(),
dto.hashtag(),
dto.createdAt(),
dto.userDto().email(),
nickname,
dto.articleCommentDtos().stream()
.map(ArticleCommentResponse::from)
.collect(Collectors.toCollection(LinkedHashSet::new))
);
}

}
Expand Up @@ -10,12 +10,17 @@
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource
public interface ArticleCommentRepository extends
JpaRepository<ArticleComment, Long>,
QuerydslPredicateExecutor<ArticleComment>,
QuerydslBinderCustomizer<QArticleComment> {

// Article Repo 의(_) Id
List<ArticleComment> findByArticle_Id(Long articleId);

@Override
default void customize(QuerydslBindings bindings, QArticleComment root) {
bindings.excludeUnlistedProperties(true);
Expand Down
Expand Up @@ -4,6 +4,8 @@
import com.example.boardproject.domain.QArticle;
import com.querydsl.core.types.dsl.DateTimeExpression;
import com.querydsl.core.types.dsl.StringExpression;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
Expand All @@ -16,6 +18,8 @@ public interface ArticleRepository extends
QuerydslPredicateExecutor<Article>,
QuerydslBinderCustomizer<QArticle> {

Page<Article> findByTitle(String title, Pageable pageable);

@Override
default void customize(QuerydslBindings bindings, QArticle root) {
bindings.excludeUnlistedProperties(true);
Expand Down

0 comments on commit 1c24d49

Please sign in to comment.