Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q&A 관련 인프라 리팩토링 #686

Merged
merged 17 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
Expand Up @@ -9,8 +9,8 @@
import com.ddang.ddang.qna.application.exception.QuestionNotFoundException;
import com.ddang.ddang.qna.domain.Answer;
import com.ddang.ddang.qna.domain.Question;
import com.ddang.ddang.qna.infrastructure.JpaAnswerRepository;
import com.ddang.ddang.qna.infrastructure.JpaQuestionRepository;
import com.ddang.ddang.qna.domain.repository.AnswerRepository;
import com.ddang.ddang.qna.domain.repository.QuestionRepository;
import com.ddang.ddang.user.application.exception.UserNotFoundException;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.domain.repository.UserRepository;
Expand All @@ -26,8 +26,8 @@ public class AnswerService {

private final ApplicationEventPublisher answerEventPublisher;
private final UserRepository userRepository;
private final JpaQuestionRepository questionRepository;
private final JpaAnswerRepository answerRepository;
private final QuestionRepository questionRepository;
private final AnswerRepository answerRepository;

@Transactional
public Long create(final CreateAnswerDto answerDto, final String absoluteImageUrl) {
Expand Down Expand Up @@ -65,7 +65,7 @@ private void checkAlreadyAnswered(final Question question) {

@Transactional
public void deleteById(final Long answerId, final Long userId) {
final Answer answer = answerRepository.findByIdAndDeletedIsFalse(answerId)
final Answer answer = answerRepository.findById(answerId)
.orElseThrow(() -> new AnswerNotFoundException("해당 답변을 찾을 수 없습니다."));
final User user = userRepository.findById(userId)
.orElseThrow(() -> new UserNotFoundException("해당 사용자를 찾을 수 없습니다."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import com.ddang.ddang.qna.application.exception.InvalidQuestionerException;
import com.ddang.ddang.qna.application.exception.QuestionNotFoundException;
import com.ddang.ddang.qna.domain.Question;
import com.ddang.ddang.qna.infrastructure.JpaQuestionRepository;
import com.ddang.ddang.qna.domain.repository.QuestionRepository;
import com.ddang.ddang.user.application.exception.UserNotFoundException;
import com.ddang.ddang.user.domain.User;
import lombok.RequiredArgsConstructor;
import com.ddang.ddang.user.domain.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -31,7 +31,7 @@ public class QuestionService {
private final ApplicationEventPublisher questionEventPublisher;
private final AuctionRepository auctionRepository;
private final UserRepository userRepository;
private final JpaQuestionRepository questionRepository;
private final QuestionRepository questionRepository;

@Transactional
public Long create(final CreateQuestionDto questionDto, final String absoluteImageUrl) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ddang.ddang.qna.domain.repository;

import com.ddang.ddang.qna.domain.Answer;

import java.util.Optional;

public interface AnswerRepository {

Answer save(final Answer answer);

boolean existsByQuestionId(Long questionId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

필수

final이 누락되었습니다

Copy link
Member Author

@JJ503 JJ503 Oct 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가해두었습니다. 감사합니다.


Optional<Answer> findById(final Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.ddang.ddang.qna.domain.repository;

import com.ddang.ddang.qna.domain.Question;

import java.util.List;
import java.util.Optional;

public interface QuestionRepository {

Question save(final Question question);

Optional<Question> findById(final Long id);

List<Question> findAllByAuctionId(final Long auctionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.ddang.ddang.qna.infrastructure;

import com.ddang.ddang.qna.domain.Answer;
import com.ddang.ddang.qna.domain.repository.AnswerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class AnswerRepositoryImpl implements AnswerRepository {

private final JpaAnswerRepository jpaAnswerRepository;

@Override
public Answer save(final Answer answer) {
return jpaAnswerRepository.save(answer);
}

@Override
public boolean existsByQuestionId(final Long questionId) {
return jpaAnswerRepository.existsByQuestionId(questionId);
}

@Override
public Optional<Answer> findById(final Long id) {
return jpaAnswerRepository.findByIdAndDeletedIsFalse(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.ddang.ddang.qna.infrastructure;

import com.ddang.ddang.qna.domain.Answer;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface JpaAnswerRepository extends JpaRepository<Answer, Long> {

boolean existsByQuestionId(Long questionId);

@EntityGraph(attributePaths = {"question", "question.auction", "question.auction.seller"})
@Query("""
SELECT an
FROM Answer an
JOIN FETCH an.question q
JOIN FETCH q.auction a
JOIN FETCH a.seller
WHERE an.deleted = false AND a.id = :answerId
""")
Optional<Answer> findByIdAndDeletedIsFalse(final Long answerId);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.ddang.ddang.qna.infrastructure;

import com.ddang.ddang.qna.domain.Question;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;
Expand All @@ -11,6 +11,14 @@ public interface JpaQuestionRepository extends JpaRepository<Question, Long> {

Optional<Question> findByIdAndDeletedIsFalse(final Long id);

@EntityGraph(attributePaths = {"writer", "answer", "auction", "auction.seller"})
@Query("""
SELECT q
FROM Question q
JOIN FETCH q.writer
LEFT JOIN FETCH q.answer
JOIN FETCH q.auction a
JOIN FETCH a.seller
WHERE q.deleted = false AND a.id = :auctionId
""")
List<Question> findAllByAuctionId(final Long auctionId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ddang.ddang.qna.infrastructure;

import com.ddang.ddang.qna.domain.Question;
import com.ddang.ddang.qna.domain.repository.QuestionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class QuestionRepositoryImpl implements QuestionRepository {

private final JpaQuestionRepository jpaQuestionRepository;

@Override
public Question save(final Question question) {
return jpaQuestionRepository.save(question);
}

@Override
public Optional<Question> findById(final Long id) {
return jpaQuestionRepository.findByIdAndDeletedIsFalse(id);
}

@Override
public List<Question> findAllByAuctionId(final Long auctionId) {
return jpaQuestionRepository.findAllByAuctionId(auctionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ddang.ddang.qna.application.exception.AnswerNotFoundException;
import com.ddang.ddang.qna.domain.Answer;
import com.ddang.ddang.qna.infrastructure.JpaAnswerRepository;
import com.ddang.ddang.qna.domain.repository.AnswerRepository;
import com.ddang.ddang.report.application.dto.CreateAnswerReportDto;
import com.ddang.ddang.report.application.dto.ReadAnswerReportDto;
import com.ddang.ddang.report.application.exception.InvalidAnswererReportException;
Expand All @@ -22,13 +22,13 @@
@RequiredArgsConstructor
public class AnswerReportService {

private final JpaAnswerRepository answerRepository;
private final AnswerRepository answerRepository;
private final UserRepository userRepository;
private final JpaAnswerReportRepository answerReportRepository;

@Transactional
public Long create(final CreateAnswerReportDto answerReportDto) {
final Answer answer = answerRepository.findByIdAndDeletedIsFalse(answerReportDto.answerId())
final Answer answer = answerRepository.findById(answerReportDto.answerId())
.orElseThrow(() ->
new AnswerNotFoundException("해당 답변을 찾을 수 없습니다.")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.ddang.ddang.qna.application.exception.QuestionNotFoundException;
import com.ddang.ddang.qna.domain.Question;
import com.ddang.ddang.qna.infrastructure.JpaQuestionRepository;
import com.ddang.ddang.qna.domain.repository.QuestionRepository;
import com.ddang.ddang.report.application.dto.CreateQuestionReportDto;
import com.ddang.ddang.report.application.dto.ReadQuestionReportDto;
import com.ddang.ddang.report.application.exception.InvalidQuestionReportException;
Expand All @@ -22,13 +22,13 @@
@RequiredArgsConstructor
public class QuestionReportService {

private final JpaQuestionRepository questionRepository;
private final QuestionRepository questionRepository;
private final UserRepository userRepository;
private final JpaQuestionReportRepository questionReportRepository;

@Transactional
public Long create(final CreateQuestionReportDto questionReportDto) {
final Question question = questionRepository.findByIdAndDeletedIsFalse(questionReportDto.questionId())
final Question question = questionRepository.findById(questionReportDto.questionId())
.orElseThrow(() ->
new QuestionNotFoundException("해당 질문을 찾을 수 없습니다.")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import com.ddang.ddang.qna.application.dto.CreateAnswerDto;
import com.ddang.ddang.qna.domain.Answer;
import com.ddang.ddang.qna.domain.Question;
import com.ddang.ddang.qna.infrastructure.JpaAnswerRepository;
import com.ddang.ddang.qna.infrastructure.JpaQuestionRepository;
import com.ddang.ddang.qna.domain.repository.AnswerRepository;
import com.ddang.ddang.qna.domain.repository.QuestionRepository;
import com.ddang.ddang.user.domain.Reliability;
import com.ddang.ddang.user.domain.User;
import com.ddang.ddang.user.domain.repository.UserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@SuppressWarnings("NonAsciiCharacters")
public class AnswerServiceFixture {
Expand All @@ -29,10 +28,10 @@ public class AnswerServiceFixture {
private UserRepository userRepository;

@Autowired
private JpaQuestionRepository questionRepository;
private QuestionRepository questionRepository;

@Autowired
private JpaAnswerRepository answerRepository;
private AnswerRepository answerRepository;

protected Long 존재하지_않는_답변_아이디 = -999L;
protected Long 존재하지_않는_사용자_아이디 = -999L;
Expand Down Expand Up @@ -87,7 +86,8 @@ void setUp() {
userRepository.save(판매자가_아닌_사용자);

auctionRepository.save(경매);
questionRepository.saveAll(List.of(질문, 답변한_질문));
questionRepository.save(질문);
questionRepository.save(답변한_질문);
answerRepository.save(답변);

답변_등록_요청_dto = new CreateAnswerDto(질문.getId(), "답변 드립니다.", 판매자.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.ddang.ddang.qna.application.dto.ReadUserInQnaDto;
import com.ddang.ddang.qna.domain.Answer;
import com.ddang.ddang.qna.domain.Question;
import com.ddang.ddang.qna.infrastructure.JpaAnswerRepository;
import com.ddang.ddang.qna.infrastructure.JpaQuestionRepository;
import com.ddang.ddang.qna.domain.repository.AnswerRepository;
import com.ddang.ddang.qna.domain.repository.QuestionRepository;
import com.ddang.ddang.region.domain.Region;
import com.ddang.ddang.region.domain.repository.RegionRepository;
import com.ddang.ddang.user.domain.Reliability;
Expand All @@ -24,7 +24,6 @@
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@SuppressWarnings("NonAsciiCharacters")
public class QuestionServiceFixture {
Expand All @@ -36,10 +35,10 @@ public class QuestionServiceFixture {
private AuctionRepository auctionRepository;

@Autowired
private JpaQuestionRepository questionRepository;
private QuestionRepository questionRepository;

@Autowired
private JpaAnswerRepository answerRepository;
private AnswerRepository answerRepository;

@Autowired
private RegionRepository regionRepository;
Expand Down Expand Up @@ -166,8 +165,11 @@ void setUp() {
auctionRepository.save(질문과_답변이_존재하는_경매);
auctionRepository.save(종료된_경매);
auctionRepository.save(삭제된_경매);
questionRepository.saveAll(List.of(질문, 질문2, 질문3));
answerRepository.saveAll(List.of(답변1, 답변2));
questionRepository.save(질문);
questionRepository.save(질문2);
questionRepository.save(질문3);
answerRepository.save(답변1);
answerRepository.save(답변2);

질문_3개_답변_2개가_존재하는_경매_아이디 = 질문과_답변이_존재하는_경매.getId();

Expand Down
Loading
Loading