Skip to content

Commit

Permalink
Merge pull request #143 from TravelLaboratory/feature/application-lay…
Browse files Browse the repository at this point in the history
…er-test

Application/Service - Comment, Review 테스트 작성 및 comment, review의 update 시 id만 반환하는 걸 관련된 수정된 응답값들도 전부 보내도록 수정
  • Loading branch information
k9want authored Jul 25, 2024
2 parents 47c24d6 + 8264f15 commit 7187c6a
Show file tree
Hide file tree
Showing 43 changed files with 1,276 additions and 262 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.travellaboratory.be.article.application.port;

import java.util.List;
import java.util.Optional;
import site.travellaboratory.be.article.domain.Article;
import site.travellaboratory.be.article.domain.enums.ArticleStatus;

public interface ArticleRepository {

Optional<Article> findByIdAndStatusIn(final Long articleId, List<ArticleStatus> Status);
Article save(Article article);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.travellaboratory.be.article.application;
package site.travellaboratory.be.article.application.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.travellaboratory.be.article.application;
package site.travellaboratory.be.article.application.service;

import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.travellaboratory.be.article.application;
package site.travellaboratory.be.article.application.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.travellaboratory.be.article.application._schedule;
package site.travellaboratory.be.article.application.service._schedule;

import java.util.Comparator;
import java.util.LinkedHashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.travellaboratory.be.article.application._schedule;
package site.travellaboratory.be.article.application.service._schedule;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ public class Article {
private final LocalDate startAt;
private final LocalDate endAt;
private final String expense;
// todo: 체크한번
private final TravelCompanion travelCompanion;
// todo: 체크한번
@Builder.Default
private final List<TravelStyle> travelStyles = new ArrayList<>();
private final ArticleStatus status;
private final String coverImgUrl;
private final ArticleStatus status;

public static Article create(User user, ArticleRegisterRequest registerRequest) {
return Article.builder()
Expand All @@ -47,8 +45,8 @@ public static Article create(User user, ArticleRegisterRequest registerRequest)
.expense(registerRequest.expense())
.travelCompanion(TravelCompanion.from(registerRequest.travelCompanion()))
.travelStyles(TravelStyle.from(registerRequest.travelStyles()))
.status(ArticleStatus.ACTIVE) // 기본 상태를 ACTIVE로 설정
.coverImgUrl(null) // 여행 생성 시에는 coverImgUrl 받지 않음
.status(ArticleStatus.ACTIVE) // 기본 상태를 ACTIVE로 설정
.build();
}

Expand All @@ -65,8 +63,8 @@ public Article update(User user, ArticleUpdateRequest updateRequest) {
.expense(updateRequest.expense())
.travelCompanion(TravelCompanion.from(updateRequest.travelCompanion()))
.travelStyles(TravelStyle.from(updateRequest.travelStyles()))
.status(this.status) // 기본 상태를 ACTIVE로 설정
.coverImgUrl(this.coverImgUrl)
.status(this.status) // 기본 상태를 ACTIVE로 설정
.build();
}

Expand All @@ -83,8 +81,8 @@ public Article delete(User user) {
.expense(this.expense)
.travelCompanion(this.travelCompanion)
.travelStyles(this.travelStyles)
.status(ArticleStatus.INACTIVE)
.coverImgUrl(this.coverImgUrl)
.status(ArticleStatus.INACTIVE)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package site.travellaboratory.be.article.infrastructure.persistence.repository;

import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import site.travellaboratory.be.article.application.port.ArticleRepository;
import site.travellaboratory.be.article.domain.Article;
import site.travellaboratory.be.article.domain.enums.ArticleStatus;
import site.travellaboratory.be.article.infrastructure.persistence.entity.ArticleEntity;

@Repository
@RequiredArgsConstructor
public class ArticleRepositoryImpl implements ArticleRepository {

private final ArticleJpaRepository articleJpaRepository;

@Override
public Optional<Article> findByIdAndStatusIn(Long articleId, List<ArticleStatus> Status) {
return articleJpaRepository.findByIdAndStatusIn(articleId, Status)
.map(ArticleEntity::toModel);
}

@Override
public Article save(Article article) {
return articleJpaRepository.save(ArticleEntity.from(article)).toModel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.travellaboratory.be.article.application.ArticleLikeService;
import site.travellaboratory.be.article.application.service.ArticleLikeService;
import site.travellaboratory.be.common.annotation.UserId;
import site.travellaboratory.be.article.presentation.response.like.BookmarkSaveResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.travellaboratory.be.article.application.ArticleReaderService;
import site.travellaboratory.be.article.application.service.ArticleReaderService;
import site.travellaboratory.be.common.annotation.UserId;
import site.travellaboratory.be.article.presentation.response.like.BookmarkResponse;
import site.travellaboratory.be.article.presentation.response.reader.ArticleOneResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import site.travellaboratory.be.article.application.ArticleWriterService;
import site.travellaboratory.be.article.application.service.ArticleWriterService;
import site.travellaboratory.be.common.annotation.UserId;
import site.travellaboratory.be.article.domain.Article;
import site.travellaboratory.be.article.presentation.response.writer.ArticleDeleteResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.travellaboratory.be.article.application._schedule.ArticleScheduleReaderService;
import site.travellaboratory.be.article.application.service._schedule.ArticleScheduleReaderService;
import site.travellaboratory.be.common.annotation.UserId;
import site.travellaboratory.be.article.presentation.response._schedule.reader.ArticleScheduleReadPlacesResponse;
import site.travellaboratory.be.article.presentation.response._schedule.reader.ArticleScheduleReadDetailResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.travellaboratory.be.article.application._schedule.ArticleScheduleWriterService;
import site.travellaboratory.be.article.application.service._schedule.ArticleScheduleWriterService;
import site.travellaboratory.be.common.annotation.UserId;
import site.travellaboratory.be.article.presentation.response._schedule.writer.ArticleScheduleDeleteResponse;
import site.travellaboratory.be.article.domain._schedule.request.ArticleScheduleUpdateRequest;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.travellaboratory.be.comment.application.port;

import java.util.List;
import java.util.Optional;
import site.travellaboratory.be.comment.domain.Comment;
import site.travellaboratory.be.comment.domain.enums.CommentStatus;

public interface CommentRepository {
Optional<Comment> findByIdAndStatusIn(Long commentId, List<CommentStatus> status);

Comment save(Comment comment);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.travellaboratory.be.comment.application;
package site.travellaboratory.be.comment.application.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.travellaboratory.be.comment.application;
package site.travellaboratory.be.comment.application.service;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package site.travellaboratory.be.comment.application.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.travellaboratory.be.comment.application.port.CommentRepository;
import site.travellaboratory.be.comment.domain.Comment;
import site.travellaboratory.be.comment.domain.enums.CommentStatus;
import site.travellaboratory.be.comment.domain.request.CommentSaveRequest;
import site.travellaboratory.be.comment.domain.request.CommentUpdateRequest;
import site.travellaboratory.be.common.exception.BeApplicationException;
import site.travellaboratory.be.common.exception.ErrorCodes;
import site.travellaboratory.be.review.application.port.ReviewRepository;
import site.travellaboratory.be.review.domain.Review;
import site.travellaboratory.be.review.domain.enums.ReviewStatus;
import site.travellaboratory.be.user.application.port.UserRepository;
import site.travellaboratory.be.user.domain.User;
import site.travellaboratory.be.user.domain.enums.UserStatus;

@Service
@RequiredArgsConstructor
public class CommentWriterService {

private final CommentRepository commentRepository;
private final ReviewRepository reviewRepository;
private final UserRepository userRepository;

@Transactional
public Comment save(Long userId, CommentSaveRequest request) {
Review review = getReviewById(request.reviewId());
User user = getUserById(userId);

Comment saveComment = Comment.create(user, review, request);
return commentRepository.save(saveComment);
}

@Transactional
public Comment update(Long userId, Long commentId, CommentUpdateRequest request) {
Comment comment = getCommentById(commentId);
User user = getUserById(userId);

Comment updateComment = comment.withUpdatedReplyContent(user, request);
return commentRepository.save(updateComment);
}

@Transactional
public Comment delete(final Long userId, final Long commentId) {
Comment comment = getCommentById(commentId);
User user = getUserById(userId);

Comment deletedComment = comment.withInactiveStatus(user);
return commentRepository.save(deletedComment);
}

private Review getReviewById(Long reviewId) {
return reviewRepository.findByIdAndStatusIn(reviewId, List.of(ReviewStatus.ACTIVE, ReviewStatus.PRIVATE))
.orElseThrow(() -> new BeApplicationException(ErrorCodes.COMMENT_INVALID_REVIEW_ID,
HttpStatus.NOT_FOUND));
}

private User getUserById(Long userId) {
return userRepository.getByIdAndStatus(userId, UserStatus.ACTIVE);
}

private Comment getCommentById(Long commentId) {
return commentRepository.findByIdAndStatusIn(commentId,
List.of(CommentStatus.ACTIVE))
.orElseThrow(() -> new BeApplicationException(ErrorCodes.COMMENT_INVALID_COMMENT_ID,
HttpStatus.NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package site.travellaboratory.be.comment.infrastructure.persistence.repository;

import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import site.travellaboratory.be.comment.application.port.CommentRepository;
import site.travellaboratory.be.comment.domain.Comment;
import site.travellaboratory.be.comment.domain.enums.CommentStatus;
import site.travellaboratory.be.comment.infrastructure.persistence.entity.CommentEntity;

@Repository
@RequiredArgsConstructor
public class CommentRepositoryImpl implements CommentRepository {

private final CommentJpaRepository commentJpaRepository;

@Override
public Optional<Comment> findByIdAndStatusIn(Long commentId, List<CommentStatus> status) {
return commentJpaRepository.findByIdAndStatusIn(commentId, status).map(CommentEntity::toModel);
}

@Override
public Comment save(Comment comment) {
return commentJpaRepository.save(CommentEntity.from(comment)).toModel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.travellaboratory.be.comment.application.CommentLikeService;
import site.travellaboratory.be.comment.application.service.CommentLikeService;
import site.travellaboratory.be.common.annotation.UserId;
import site.travellaboratory.be.comment.domain.enums.CommentLikeStatus;
import site.travellaboratory.be.comment.presentation.response.like.CommentToggleLikeResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import site.travellaboratory.be.comment.application.CommentReaderService;
import site.travellaboratory.be.comment.application.service.CommentReaderService;
import site.travellaboratory.be.comment.presentation.response.reader.CommentReadPaginationResponse;
import site.travellaboratory.be.common.annotation.UserId;

Expand Down
Loading

0 comments on commit 7187c6a

Please sign in to comment.