Skip to content

Commit

Permalink
fix-be: OSIV 오류 해결 (#746)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Kwoun Ki Ho <[email protected]>
  • Loading branch information
2 people authored and Dobby-Kim committed Oct 5, 2024
1 parent 07d172e commit f766781
Show file tree
Hide file tree
Showing 22 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.cruru.applicant.controller.request.EvaluationCreateRequest;
import com.cruru.applicant.controller.request.EvaluationUpdateRequest;
import com.cruru.applicant.controller.response.EvaluationResponses;
import com.cruru.applicant.domain.Applicant;
import com.cruru.applicant.domain.Evaluation;
import com.cruru.applicant.facade.EvaluationFacade;
import com.cruru.auth.annotation.RequireAuthCheck;
import com.cruru.global.LoginProfile;
import com.cruru.process.domain.Process;
import jakarta.validation.Valid;
import java.net.URI;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,7 +28,7 @@ public class EvaluationController {

private final EvaluationFacade evaluationFacade;

@RequireAuthCheck(targetId = "applicantId", targetDomain = Applicant.class)
@RequireAuthCheck(targetId = "processId", targetDomain = Process.class)
@PostMapping
public ResponseEntity<Void> create(
@RequestBody @Valid EvaluationCreateRequest request,
Expand All @@ -41,7 +41,7 @@ public ResponseEntity<Void> create(
return ResponseEntity.created(URI.create(url)).build();
}

@RequireAuthCheck(targetId = "applicantId", targetDomain = Applicant.class)
@RequireAuthCheck(targetId = "processId", targetDomain = Process.class)
@GetMapping
public ResponseEntity<EvaluationResponses> read(
@RequestParam(name = "processId") Long processId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private boolean isOutOfRange(int score) {

@Override
public boolean isAuthorizedBy(Member member) {
return applicant.isAuthorizedBy(member);
return process.isAuthorizedBy(member);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.process.domain.Process;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -15,6 +17,10 @@ public interface ApplicantRepository extends JpaRepository<Applicant, Long> {

List<Applicant> findAllByProcess(Process process);

@EntityGraph(attributePaths = {"process.dashboard.club.member"})
@Query("SELECT a FROM Applicant a WHERE a.id = :id")
Optional<Applicant> findByIdFetchingMember(long id);

long countByProcess(Process process);

@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.cruru.applicant.domain.Evaluation;
import com.cruru.process.domain.Process;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -16,6 +18,10 @@ public interface EvaluationRepository extends JpaRepository<Evaluation, Long> {

void deleteByProcessId(long processId);

@EntityGraph(attributePaths = {"process.dashboard.club.member"})
@Query("SELECT e FROM Evaluation e WHERE e.id = :id")
Optional<Evaluation> findByIdFetchingMember(long id);

@Modifying(clearAutomatically = true, flushAutomatically = true)
@Transactional
@Query("DELETE FROM Evaluation e WHERE e.process IN :processes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public Applicant findById(Long applicantId) {
.orElseThrow(ApplicantNotFoundException::new);
}

public Applicant findByIdFetchingMember(Long applicantId) {
return applicantRepository.findByIdFetchingMember(applicantId)
.orElseThrow(ApplicantNotFoundException::new);
}

private boolean changeExists(ApplicantUpdateRequest request, Applicant applicant) {
return !(applicant.getName().equals(request.name())
&& applicant.getEmail().equals(request.email())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public Evaluation findById(Long evaluationId) {
.orElseThrow(EvaluationNotFoundException::new);
}

public Evaluation findByIdFetchingMember(Long evaluationId) {
return evaluationRepository.findByIdFetchingMember(evaluationId)
.orElseThrow(EvaluationNotFoundException::new);
}

@Transactional
public void create(EvaluationCreateRequest request, Process process, Applicant applicant) {
evaluationRepository.save(new Evaluation(request.score(), request.content(), process, applicant));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import com.cruru.dashboard.domain.DashboardApplyFormDto;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface ApplyFormRepository extends JpaRepository<ApplyForm, Long> {

@EntityGraph(attributePaths = {"dashboard.club.member"})
@Query("SELECT a FROM ApplyForm a WHERE a.id = :id")
Optional<ApplyForm> findByIdFetchingMember(long id);

Optional<ApplyForm> findByDashboard(Dashboard dashboard);

@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public ApplyForm findById(Long applyFormId) {
.orElseThrow(ApplyFormNotFoundException::new);
}

public ApplyForm findByIdFetchingMember(Long applyFormId) {
return applyFormRepository.findByIdFetchingMember(applyFormId)
.orElseThrow(ApplyFormNotFoundException::new);
}

public ApplyForm findByDashboardId(Long dashboardId) {
return applyFormRepository.findByDashboardId(dashboardId)
.orElseThrow(ApplyFormNotFoundException::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void checkAuthorizationForTarget(
Object service = applicationContext.getBean(serviceName);

// findById 메서드를 호출하여 해당 도메인 객체(SecureResource)를 가져옴
Method findByIdMethod = service.getClass().getMethod("findById", Long.class);
Method findByIdMethod = service.getClass().getMethod("findByIdFetchingMember", Long.class);
SecureResource secureResource = (SecureResource) findByIdMethod.invoke(service, targetId);

// Lazy Loading된 연관 엔티티를 강제 로딩함
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import com.cruru.club.domain.Club;
import com.cruru.member.domain.Member;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface ClubRepository extends JpaRepository<Club, Long> {

Optional<Club> findByMember(Member member);

@EntityGraph(attributePaths = "member")
@Query("SELECT c FROM Club c WHERE c.id = :id")
Optional<Club> findByIdFetchingMember(long id);
}
5 changes: 5 additions & 0 deletions backend/src/main/java/com/cruru/club/service/ClubService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public Club findByMember(Member member) {
return clubRepository.findByMember(member)
.orElseThrow(ClubNotFoundException::new);
}

public Club findByIdFetchingMember(Long id) {
return clubRepository.findByIdFetchingMember(id)
.orElseThrow(ClubNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.cruru.dashboard.domain.repository;

import com.cruru.dashboard.domain.Dashboard;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface DashboardRepository extends JpaRepository<Dashboard, Long> {

@EntityGraph(attributePaths = {"club.member"})
@Query("SELECT d FROM Dashboard d WHERE d.id = :id")
Optional<Dashboard> findByIdFetchingMember(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public Dashboard findById(Long id) {
.orElseThrow(DashboardNotFoundException::new);
}

public Dashboard findByIdFetchingMember(Long id) {
return dashboardRepository.findByIdFetchingMember(id)
.orElseThrow(DashboardNotFoundException::new);
}

public List<DashboardApplyFormDto> findAllByClub(long clubId) {
return applyFormRepository.findAllByClub(clubId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.cruru.dashboard.domain.Dashboard;
import com.cruru.process.domain.Process;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface ProcessRepository extends JpaRepository<Process, Long> {

Expand All @@ -12,4 +15,8 @@ public interface ProcessRepository extends JpaRepository<Process, Long> {
int countByDashboard(Dashboard dashboard);

List<Process> findAllByDashboard(Dashboard dashboard);

@EntityGraph(attributePaths = {"dashboard.club.member"})
@Query("SELECT p FROM Process p WHERE p.id = :id")
Optional<Process> findByIdFetchingMember(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public Process findById(Long processId) {
.orElseThrow(ProcessNotFoundException::new);
}

public Process findByIdFetchingMember(Long processId) {
return processRepository.findByIdFetchingMember(processId)
.orElseThrow(ProcessNotFoundException::new);
}

private boolean changeExists(ProcessUpdateRequest request, Process process) {
return !(request.name().equals(process.getName()) && request.description().equals(process.getDescription()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.cruru.applicant.domain.Applicant;
import com.cruru.question.domain.Answer;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -11,6 +13,10 @@

public interface AnswerRepository extends JpaRepository<Answer, Long> {

@EntityGraph(attributePaths = {"applicant.process.dashboard.club.member"})
@Query("SELECT a FROM Answer a WHERE a.id = :id")
Optional<Answer> findByIdFetchingMember(long id);

@Query("SELECT a FROM Answer a JOIN FETCH a.question WHERE a.applicant = :applicant")
List<Answer> findAllByApplicantWithQuestions(@Param("applicant") Applicant applicant);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import com.cruru.question.domain.Choice;
import com.cruru.question.domain.Question;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface ChoiceRepository extends JpaRepository<Choice, Long> {

@EntityGraph(attributePaths = {"question.applyForm.dashboard.club.member"})
@Query("SELECT c FROM Choice c WHERE c.id = :id")
Optional<Choice> findByIdFetchingMember(long id);

List<Choice> findAllByQuestion(Question question);

void deleteAllByQuestion(Question question);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import com.cruru.applyform.domain.ApplyForm;
import com.cruru.question.domain.Question;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface QuestionRepository extends JpaRepository<Question, Long> {

@EntityGraph(attributePaths = {"applyForm.dashboard.club.member"})
@Query("SELECT q FROM Question q WHERE q.id = :id")
Optional<Question> findByIdFetchingMember(long id);

List<Question> findAllByApplyForm(ApplyForm applyForm);
}
8 changes: 8 additions & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ spring:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
max_fetch_depth: 15
hibernate:
ddl-auto: create-drop
defer-datasource-initialization: false
open-in-view: false
mail:
host: smtp.gmail.com
port: 587
Expand Down Expand Up @@ -90,9 +92,11 @@ spring:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
max_fetch_depth: 15
hibernate:
ddl-auto: ${DDL_AUTO}
defer-datasource-initialization: false
open-in-view: false
mail:
host: smtp.gmail.com
port: 587
Expand Down Expand Up @@ -174,9 +178,11 @@ spring:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
max_fetch_depth: 15
hibernate:
ddl-auto: ${DDL_AUTO}
defer-datasource-initialization: false
open-in-view: false
mail:
host: smtp.gmail.com
port: 587
Expand Down Expand Up @@ -257,9 +263,11 @@ spring:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
max_fetch_depth: 15
hibernate:
ddl-auto: ${DDL_AUTO}
defer-datasource-initialization: false
open-in-view: false
mail:
host: smtp.gmail.com
port: 587
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.cruru.question.controller.response.QuestionResponse;
import com.cruru.question.domain.Question;
import com.cruru.question.domain.repository.AnswerRepository;
import com.cruru.question.domain.repository.ChoiceRepository;
import com.cruru.question.domain.repository.QuestionRepository;
import com.cruru.util.ServiceTest;
import com.cruru.util.fixture.ApplyFormFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ void findById() {

@DisplayName("지원서 폼 조회 시, 지원서 폼이 존재하지 않을 경우 예외가 발생한다.")
@Test
void findById_invalidApplyForm() {
void findById_FetchingMember_invalidApplyForm() {
// given
processRepository.save(ProcessFixture.applyType(dashboard));
ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.frontend(dashboard));
questionRepository.save(QuestionFixture.shortAnswerType(applyForm));

// when&then
assertThatThrownBy(() -> applyFormService.findById(-1L)).isInstanceOf(ApplyFormNotFoundException.class);
assertThatThrownBy(() -> applyFormService.findById(-1L))
.isInstanceOf(ApplyFormNotFoundException.class);
}

@DisplayName("대시보드 ID로 지원폼을 조회한다.")
Expand Down
2 changes: 2 additions & 0 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ spring:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
max_fetch_depth: 15
hibernate:
ddl-auto: create-drop
defer-datasource-initialization: false
open-in-view: false
mail:
host: smtp.gmail.com

Expand Down

0 comments on commit f766781

Please sign in to comment.