Skip to content

Commit

Permalink
Merge pull request #193 from ITZipProject/feature/techinfo
Browse files Browse the repository at this point in the history
☔️ 테스트 : 기술 정보 레파지토리 테스트 코드 구현
  • Loading branch information
rowing0328 authored Dec 12, 2024
2 parents 07b1bb9 + 14c9561 commit 6ab7ac1
Show file tree
Hide file tree
Showing 57 changed files with 1,626 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public String editBlogIntro(
summary = "블로그 임시 삭제 (비공개 처리)",
description = "블로그를 비공개 상태로 설정합니다."
)
@ExceptionCodeAnnotations({CommonExceptionCode.UPDATE_FAIL_BLOG})
@ExceptionCodeAnnotations({
CommonExceptionCode.NOT_FOUND_BLOG,
CommonExceptionCode.UPDATE_FAIL_BLOG
})
@PatchMapping("/{blogId}/status")
public String editBlogStatus(
@Parameter(description = "블로그 ID", example = "1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public Like(String id, String postId, Long userId) {
*/
public static Like from(LikeStatus likeStatus) {
return Like.builder()
.id(likeStatus.getId())
.postId(likeStatus.getPostId())
.userId(likeStatus.getUserId())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public static Post from(PostCreateRequest request, Long blogId) {
.categoryId(request.categoryId())
.title(request.title())
.content(request.content())
.viewCount(0)
.likeCount(0)
.isPublic(true)
.thumbnailImagePath(request.thumbnailImagePath())
.contentImagePaths(request.contentImagePaths())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public Scrap(String id, String postId, Long userId) {
*/
public static Scrap from(ScrapStatus scrapStatus) {
return Scrap.builder()
.id(scrapStatus.getId())
.postId(scrapStatus.getPostId())
.userId(scrapStatus.getUserId())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
@Getter
public class LikeStatus {

private final String id;
private final String postId;
private final Long userId;
private final Boolean isLiked;

@Builder
public LikeStatus(String id, String postId, Long userId, Boolean isLiked) {
this.id = id;
public LikeStatus(String postId, Long userId, Boolean isLiked) {
this.postId = postId;
this.userId = userId;
this.isLiked = isLiked;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
@Getter
public class ScrapStatus {

private final String id;
private final String postId;
private final Long userId;
private final Boolean isScrapped;

@Builder
public ScrapStatus(String id, String postId, Long userId, Boolean isScrapped) {
this.id = id;
public ScrapStatus(String postId, Long userId, Boolean isScrapped) {
this.postId = postId;
this.userId = userId;
this.isScrapped = isScrapped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class CommentDocument extends MongoAuditingFields {
*/
public static CommentDocument from(Comment comment) {
return CommentDocument.builder()
.id(comment.getId() != null ? new ObjectId(comment.getId()) : null)
.postId(new ObjectId(comment.getPostId()))
.userId(comment.getUserId())
.content(comment.getContent())
Expand All @@ -58,10 +59,10 @@ public static CommentDocument from(Comment comment) {
public Comment toModel() {
return Comment.builder()
.id(this.id.toHexString())
.postId(this.postId.toHexString())
.postId(this.postId != null ? this.postId.toHexString() : null)
.userId(this.userId)
.content(this.content)
.isPublic(this.isPublic)
.isPublic(this.isPublic != null ? this.isPublic : null)
.createDate(this.createDate)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class LikeDocument extends MongoAuditingFields {
/**
* Like 로부터 LikeDocument 생성합니다.
*
* @param like
* @return
* @param like 변환할 Like
* @return LikeDocument
*/
public static LikeDocument from(Like like) {
return LikeDocument.builder()
.id(new ObjectId(like.getId()))
.id(like.getId() != null ? new ObjectId(like.getId()) : null)
.postId(new ObjectId(like.getPostId()))
.userId(like.getUserId())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public class PostDocument extends MongoAuditingFields {
*/
public static PostDocument from(Post post) {
return PostDocument.builder()
.id(new ObjectId(post.getId()))
.id(post.getId() != null ? new ObjectId(post.getId()) : null)
.blogId(post.getBlogId())
.categoryId(new ObjectId(post.getCategoryId()))
.title(post.getTitle())
.content(post.getContent())
.viewCount(post.getViewCount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import darkoverload.itzip.feature.techinfo.domain.scrap.Scrap;
import darkoverload.itzip.global.entity.MongoAuditingFields;
import jakarta.persistence.Id;
import lombok.*;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

Expand Down Expand Up @@ -37,7 +37,7 @@ public class ScrapDocument extends MongoAuditingFields {
*/
public static ScrapDocument from(Scrap scrap) {
return ScrapDocument.builder()
.id(new ObjectId(scrap.getId()))
.id(scrap.getId() != null ? new ObjectId(scrap.getId()) : null)
.postId(new ObjectId(scrap.getPostId()))
.userId(scrap.getUserId())
.build();
Expand All @@ -50,7 +50,6 @@ public static ScrapDocument from(Scrap scrap) {
*/
public Scrap toModel() {
return Scrap.builder()
.id(this.id.toHexString())
.postId(this.postId.toHexString())
.userId(this.userId)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BlogEntity extends AuditingFields {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne(fetch = FetchType.LAZY)
@OneToOne
@JoinColumn(name = "user_id")
private UserEntity user;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import darkoverload.itzip.feature.techinfo.domain.blog.Blog;
import darkoverload.itzip.feature.techinfo.model.entity.BlogEntity;
import darkoverload.itzip.feature.techinfo.service.blog.port.BlogCommandRepository;
import darkoverload.itzip.feature.techinfo.service.blog.port.BlogReadRepository;
import darkoverload.itzip.global.config.response.code.CommonExceptionCode;
import darkoverload.itzip.global.config.response.exception.RestApiException;
import lombok.RequiredArgsConstructor;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Repository;
import lombok.RequiredArgsConstructor;

/**
* 블로그 명령(생성, 수정)을 처리하는 레포지토리 구현 클래스.
Expand All @@ -16,15 +18,16 @@
public class BlogCommandRepositoryImpl implements BlogCommandRepository {

private final JpaBlogCommandRepository repository;
private final BlogReadRepository readRepository;

/**
* 새로운 블로그를 저장합니다.
*
* @param blog 저장할 블로그
*/
@Override
public void save(Blog blog) {
repository.save(BlogEntity.from(blog));
public Blog save(Blog blog) {
return repository.save(BlogEntity.from(blog)).toModel();
}

/**
Expand All @@ -35,10 +38,11 @@ public void save(Blog blog) {
* @throws RestApiException 블로그 업데이트 실패 시 발생
*/
@Override
public void update(Long userId, String newIntro) {
public Blog update(Long userId, String newIntro) {
if (repository.update(userId, newIntro) < 0) {
throw new RestApiException(CommonExceptionCode.UPDATE_FAIL_BLOG);
}
return readRepository.getByUserId(userId);
}

/**
Expand All @@ -49,9 +53,14 @@ public void update(Long userId, String newIntro) {
* @throws RestApiException 블로그 상태 업데이트 실패 시 발생
*/
@Override
public void update(Long blogId, boolean status) {
if (repository.update(blogId, status) < 0) {
throw new RestApiException(CommonExceptionCode.UPDATE_FAIL_BLOG);
public Blog update(Long blogId, boolean status) {
try {
if (repository.update(blogId, status) < 0) {
throw new RestApiException(CommonExceptionCode.UPDATE_FAIL_BLOG);
}
return readRepository.getReferenceById(blogId);
} catch (EntityNotFoundException e) {
throw new RestApiException(CommonExceptionCode.NOT_FOUND_BLOG);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,15 @@ public Blog getByNickname(String nickname) {
);
}

/**
* 블로그 ID로 블로그의 프록시 객체를 조회합니다.
*
* @param id 블로그 ID
* @return Blog
*/
@Override
public Blog getReferenceById(Long id) {
return repository.getReferenceById(id).toModel();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.bson.types.ObjectId;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* 댓글 명령(생성, 수정, 삭제)을 처리하는 레포지토리 구현 클래스.
*/
Expand All @@ -24,8 +26,25 @@ public class CommentCommandRepositoryImpl implements CommentCommandRepository {
* @param comment 저장할 댓글
*/
@Override
public void save(Comment comment) {
repository.save(CommentDocument.from(comment));
public Comment save(Comment comment) {
return repository.save(CommentDocument.from(comment)).toModel();
}

/**
* 여러 댓글을 한꺼번에 저장합니다.
*
* @param comments 저장할 댓글 리스트
* @return 저장된 댓글 리스트
*/
@Override
public List<Comment> saveAll(List<Comment> comments) {
return repository.saveAll(
comments.stream()
.map(CommentDocument::from)
.toList()
).stream()
.map(CommentDocument::toModel)
.toList();
}

/**
Expand All @@ -36,10 +55,13 @@ public void save(Comment comment) {
* @param content 새로운 댓글 내용
* @throws RestApiException 댓글 업데이트 실패 시 발생
*/
public void update(ObjectId commentId, Long userId, String content) {
if (repository.update(commentId, userId, content) < 0) {
throw new RestApiException(CommonExceptionCode.UPDATE_FAIL_COMMENT);
}
@Override
public Comment update(ObjectId commentId, Long userId, String content) {
return repository.update(commentId, userId, content)
.map(CommentDocument::toModel)
.orElseThrow(
() -> new RestApiException(CommonExceptionCode.UPDATE_FAIL_COMMENT)
);
}

/**
Expand All @@ -50,10 +72,22 @@ public void update(ObjectId commentId, Long userId, String content) {
* @param status 새로운 공개 상태
* @throws RestApiException 댓글 상태 업데이트 실패 시 발생
*/
public void update(ObjectId commentId, Long userId, boolean status) {
if (repository.update(commentId, userId, status) < 0) {
throw new RestApiException(CommonExceptionCode.UPDATE_FAIL_COMMENT);
}
@Override
public Comment update(ObjectId commentId, Long userId, boolean status) {
return repository.update(commentId, userId, status)
.map(CommentDocument::toModel)
.orElseThrow(
() -> new RestApiException(CommonExceptionCode.UPDATE_FAIL_COMMENT)
);
}

/**
* 모든 댓글을 삭제합니다.
* 주로 테스트 환경이나 데이터 초기화에 사용됩니다.
*/
@Override
public void deleteAll() {
repository.deleteAll();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package darkoverload.itzip.feature.techinfo.repository.comment.custom;

import darkoverload.itzip.feature.techinfo.model.document.CommentDocument;
import org.bson.types.ObjectId;
import java.util.Optional;

public interface CustomCommentCommandRepository {

long update(ObjectId commentId, Long userId, String content);
Optional<CommentDocument> update(ObjectId commentId, Long userId, String content);

long update(ObjectId commentId, Long userId, boolean status);
Optional<CommentDocument> update(ObjectId commentId, Long userId, boolean status);

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package darkoverload.itzip.feature.techinfo.repository.comment.custom;

import com.mongodb.client.result.UpdateResult;
import darkoverload.itzip.feature.techinfo.model.document.CommentDocument;
import lombok.RequiredArgsConstructor;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import org.bson.types.ObjectId;
import lombok.RequiredArgsConstructor;
import java.time.LocalDateTime;
import java.util.Optional;

/**
* 댓글 명령(수정, 삭제)을 처리하는 커스텀 레포지토리 구현 클래스.
Expand All @@ -30,15 +30,18 @@ public class CustomCommentCommandRepositoryImpl implements CustomCommentCommandR
* @return 업데이트된 댓글의 수
*/
@Override
public long update(ObjectId commentId, Long userId, String content) {
public Optional<CommentDocument> update(ObjectId commentId, Long userId, String content) {
Query query = new Query(Criteria.where("_id").is(commentId).and("user_id").is(userId));

Update update = new Update()
.set("content", content)
.set("modify_date", LocalDateTime.now());

UpdateResult result = mongoTemplate.updateFirst(query, update, CommentDocument.class);
return result.getModifiedCount();
FindAndModifyOptions options = FindAndModifyOptions.options()
.returnNew(true)
.upsert(false);

return Optional.ofNullable(mongoTemplate.findAndModify(query, update, options, CommentDocument.class));
}

/**
Expand All @@ -50,11 +53,15 @@ public long update(ObjectId commentId, Long userId, String content) {
* @return 업데이트된 댓글의 수
*/
@Override
public long update(ObjectId commentId, Long userId, boolean status) {
public Optional<CommentDocument> update(ObjectId commentId, Long userId, boolean status) {
Query query = new Query(Criteria.where("_id").is(commentId).and("user_id").is(userId));
Update update = new Update().set("is_public", status);
UpdateResult result = mongoTemplate.updateFirst(query, update, CommentDocument.class);
return result.getModifiedCount();

FindAndModifyOptions options = FindAndModifyOptions.options()
.returnNew(true)
.upsert(false);

return Optional.ofNullable(mongoTemplate.findAndModify(query, update, options, CommentDocument.class));
}

}
Loading

0 comments on commit 6ab7ac1

Please sign in to comment.