From 24312fa9d349e61596df8a973ad1dcb926472e6b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:18:30 +0900 Subject: [PATCH] =?UTF-8?q?feat-be:=20API=20=EC=BF=BC=EB=A6=AC=20=EC=B5=9C?= =?UTF-8?q?=EC=A0=81=ED=99=94=20(#707)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Kwoun Ki Ho <73146678+Chocochip101@users.noreply.github.com> Co-authored-by: 최가희 <60508828+cutehumanS2@users.noreply.github.com> Co-authored-by: Do Yeop Kim <113661364+Dobby-Kim@users.noreply.github.com> --- .../applicant/domain/dto/ApplicantCard.java | 25 +++ .../repository/ApplicantRepository.java | 30 +++- .../repository/EvaluationRepository.java | 4 +- .../applicant/facade/ApplicantFacade.java | 2 +- .../applicant/service/ApplicantService.java | 21 +-- .../applicant/service/EvaluationService.java | 16 +- .../repository/ApplyFormRepository.java | 22 ++- .../applyform/service/ApplyFormService.java | 5 + .../domain/DashboardApplyFormDto.java | 7 + .../repository/DashboardRepository.java | 3 - .../dashboard/facade/DashboardFacade.java | 30 +--- .../dashboard/service/DashboardService.java | 14 +- .../email/controller/EmailController.java | 2 +- .../java/com/cruru/email/domain/Email.java | 2 +- .../cruru/global/util/ExceptionLogger.java | 27 ++- .../cruru/process/facade/ProcessFacade.java | 59 +++--- .../cruru/process/service/ProcessService.java | 4 + .../domain/repository/AnswerRepository.java | 5 +- .../cruru/question/service/AnswerService.java | 4 +- .../repository/ApplicantRepositoryTest.java | 169 ++++++++++++++++++ .../service/EvaluationServiceTest.java | 29 --- .../repository/ApplyFormRepositoryTest.java | 36 ++++ .../applyform/facade/ApplyFormFacadeTest.java | 34 +++- .../service/ApplyFormServiceTest.java | 2 +- .../dashboard/facade/DashboardFacadeTest.java | 14 ++ .../service/DashboardServiceTest.java | 19 +- .../com/cruru/email/domain/EmailTest.java | 2 +- .../repository/EmailRepositoryTest.java | 2 +- .../cruru/email/facade/EmailFacadeTest.java | 6 +- .../controller/ProcessControllerTest.java | 2 +- .../process/facade/ProcessFacadeTest.java | 16 +- .../repository/AnswerRepositoryTest.java | 39 ++++ .../question/service/AnswerServiceTest.java | 6 +- .../test/java/com/cruru/util/ServiceTest.java | 5 +- .../cruru/util/fixture/ApplicantFixture.java | 4 + 35 files changed, 502 insertions(+), 165 deletions(-) create mode 100644 backend/src/main/java/com/cruru/applicant/domain/dto/ApplicantCard.java create mode 100644 backend/src/main/java/com/cruru/dashboard/domain/DashboardApplyFormDto.java diff --git a/backend/src/main/java/com/cruru/applicant/domain/dto/ApplicantCard.java b/backend/src/main/java/com/cruru/applicant/domain/dto/ApplicantCard.java new file mode 100644 index 000000000..d43338299 --- /dev/null +++ b/backend/src/main/java/com/cruru/applicant/domain/dto/ApplicantCard.java @@ -0,0 +1,25 @@ +package com.cruru.applicant.domain.dto; + +import com.cruru.applicant.controller.response.ApplicantCardResponse; +import java.time.LocalDateTime; + +public record ApplicantCard( + long id, + + String name, + + LocalDateTime createdAt, + + Boolean isRejected, + + long evaluationCount, + + double averageScore, + + long processId +) { + + public ApplicantCardResponse toResponse() { + return new ApplicantCardResponse(id, name, createdAt, isRejected, (int) evaluationCount, averageScore); + } +} diff --git a/backend/src/main/java/com/cruru/applicant/domain/repository/ApplicantRepository.java b/backend/src/main/java/com/cruru/applicant/domain/repository/ApplicantRepository.java index 0823c17e9..95279c4fc 100644 --- a/backend/src/main/java/com/cruru/applicant/domain/repository/ApplicantRepository.java +++ b/backend/src/main/java/com/cruru/applicant/domain/repository/ApplicantRepository.java @@ -1,10 +1,13 @@ package com.cruru.applicant.domain.repository; import com.cruru.applicant.domain.Applicant; +import com.cruru.applicant.domain.dto.ApplicantCard; +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.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface ApplicantRepository extends JpaRepository { @@ -12,5 +15,28 @@ public interface ApplicantRepository extends JpaRepository { long countByProcess(Process process); - Optional findByEmail(String email); + @Query(""" + SELECT new com.cruru.applicant.domain.dto.ApplicantCard( + a.id, a.name, a.createdDate, a.isRejected, COUNT(e), COALESCE(AVG(e.score), 0.00), a.process.id + ) + FROM Applicant a + LEFT JOIN Evaluation e ON e.applicant = a + WHERE a.process IN :processes + GROUP BY a.id, a.name, a.createdDate, a.isRejected, a.process.id + """) + List findApplicantCardsByProcesses(@Param("processes") List processes); + + @Query(""" + SELECT new com.cruru.applicant.domain.dto.ApplicantCard( + a.id, a.name, a.createdDate, a.isRejected, COUNT(e), COALESCE(AVG(e.score), 0.00), a.process.id + ) + FROM Applicant a + LEFT JOIN Evaluation e ON e.applicant = a + WHERE a.process = :process + GROUP BY a.id, a.name, a.createdDate, a.isRejected + """) + List findApplicantCardsByProcess(@Param("process") Process process); + + @Query("SELECT a FROM Applicant a JOIN FETCH a.process p JOIN FETCH p.dashboard d WHERE d = :dashboard") + List findAllByDashboard(@Param("dashboard") Dashboard dashboard); } diff --git a/backend/src/main/java/com/cruru/applicant/domain/repository/EvaluationRepository.java b/backend/src/main/java/com/cruru/applicant/domain/repository/EvaluationRepository.java index e90a85c1b..0674fabd4 100644 --- a/backend/src/main/java/com/cruru/applicant/domain/repository/EvaluationRepository.java +++ b/backend/src/main/java/com/cruru/applicant/domain/repository/EvaluationRepository.java @@ -8,9 +8,7 @@ public interface EvaluationRepository extends JpaRepository { - int countByApplicantAndProcess(Applicant applicant, Process process); - List findAllByProcessAndApplicant(Process process, Applicant applicant); - void deleteByProcess(Process process); + void deleteByProcessId(long processId); } diff --git a/backend/src/main/java/com/cruru/applicant/facade/ApplicantFacade.java b/backend/src/main/java/com/cruru/applicant/facade/ApplicantFacade.java index 571e4c963..0cafd8962 100644 --- a/backend/src/main/java/com/cruru/applicant/facade/ApplicantFacade.java +++ b/backend/src/main/java/com/cruru/applicant/facade/ApplicantFacade.java @@ -40,7 +40,7 @@ private ApplicantBasicResponse toApplicantBasicResponse(Applicant applicant) { public ApplicantAnswerResponses readDetailById(long applicantId) { Applicant applicant = applicantService.findById(applicantId); - List answers = answerService.findAllByApplicant(applicant); + List answers = answerService.findAllByApplicantWithQuestions(applicant); List answerResponses = answerService.toAnswerResponses(answers); return new ApplicantAnswerResponses(answerResponses); } diff --git a/backend/src/main/java/com/cruru/applicant/service/ApplicantService.java b/backend/src/main/java/com/cruru/applicant/service/ApplicantService.java index ae57924f2..0c68e9dff 100644 --- a/backend/src/main/java/com/cruru/applicant/service/ApplicantService.java +++ b/backend/src/main/java/com/cruru/applicant/service/ApplicantService.java @@ -3,9 +3,9 @@ import com.cruru.applicant.controller.request.ApplicantCreateRequest; import com.cruru.applicant.controller.request.ApplicantMoveRequest; import com.cruru.applicant.controller.request.ApplicantUpdateRequest; -import com.cruru.applicant.controller.response.ApplicantCardResponse; import com.cruru.applicant.controller.response.ApplicantResponse; import com.cruru.applicant.domain.Applicant; +import com.cruru.applicant.domain.dto.ApplicantCard; import com.cruru.applicant.domain.repository.ApplicantRepository; import com.cruru.applicant.exception.ApplicantNotFoundException; import com.cruru.applicant.exception.badrequest.ApplicantRejectException; @@ -95,18 +95,11 @@ public ApplicantResponse toApplicantResponse(Applicant applicant) { ); } - public ApplicantCardResponse toApplicantCardResponse( - Applicant applicant, - int evaluationCount, - double averageScore - ) { - return new ApplicantCardResponse( - applicant.getId(), - applicant.getName(), - applicant.getCreatedDate(), - applicant.isRejected(), - evaluationCount, - averageScore - ); + public List findApplicantCards(List processes) { + return applicantRepository.findApplicantCardsByProcesses(processes); + } + + public List findApplicantCards(Process process) { + return applicantRepository.findApplicantCardsByProcess(process); } } diff --git a/backend/src/main/java/com/cruru/applicant/service/EvaluationService.java b/backend/src/main/java/com/cruru/applicant/service/EvaluationService.java index 8a7794994..b02b5f24f 100644 --- a/backend/src/main/java/com/cruru/applicant/service/EvaluationService.java +++ b/backend/src/main/java/com/cruru/applicant/service/EvaluationService.java @@ -29,18 +29,6 @@ public void create(EvaluationCreateRequest request, Process process, Applicant a evaluationRepository.save(new Evaluation(request.score(), request.content(), process, applicant)); } - public int count(Process process, Applicant applicant) { - return evaluationRepository.countByApplicantAndProcess(applicant, process); - } - - public double calculateAverageScore(Process process, Applicant applicant) { - List evaluations = findAllByProcessAndApplicant(process, applicant); - return evaluations.stream() - .mapToDouble(Evaluation::getScore) - .average() - .orElse(0.0); - } - public List findAllByProcessAndApplicant(Process process, Applicant applicant) { return evaluationRepository.findAllByProcessAndApplicant(process, applicant); } @@ -64,7 +52,7 @@ private boolean changeExists(EvaluationUpdateRequest request, Evaluation evaluat return !(evaluation.getContent().equals(request.content()) && evaluation.getScore().equals(request.score())); } - public void deleteByProcess(Process process) { - evaluationRepository.deleteByProcess(process); + public void deleteByProcess(long processId) { + evaluationRepository.deleteByProcessId(processId); } } diff --git a/backend/src/main/java/com/cruru/applyform/domain/repository/ApplyFormRepository.java b/backend/src/main/java/com/cruru/applyform/domain/repository/ApplyFormRepository.java index 995a2dace..ec6bfc72d 100644 --- a/backend/src/main/java/com/cruru/applyform/domain/repository/ApplyFormRepository.java +++ b/backend/src/main/java/com/cruru/applyform/domain/repository/ApplyFormRepository.java @@ -2,10 +2,30 @@ import com.cruru.applyform.domain.ApplyForm; import com.cruru.dashboard.domain.Dashboard; +import com.cruru.dashboard.domain.DashboardApplyFormDto; +import java.util.List; import java.util.Optional; 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 { - Optional findByDashboard(Dashboard dashboardId); + Optional findByDashboard(Dashboard dashboard); + + @Query(""" + SELECT af FROM ApplyForm af + JOIN FETCH af.dashboard d + WHERE d.id = :dashboardId + """) + Optional findByDashboardId(long dashboardId); + + @Query(""" + SELECT new com.cruru.dashboard.domain.DashboardApplyFormDto(d, a) + FROM ApplyForm a + JOIN FETCH a.dashboard d + JOIN FETCH d.club c + WHERE c.id = :clubId + """) + List findAllByClub(@Param("clubId") Long clubId); } diff --git a/backend/src/main/java/com/cruru/applyform/service/ApplyFormService.java b/backend/src/main/java/com/cruru/applyform/service/ApplyFormService.java index 94c47f41d..156576b65 100644 --- a/backend/src/main/java/com/cruru/applyform/service/ApplyFormService.java +++ b/backend/src/main/java/com/cruru/applyform/service/ApplyFormService.java @@ -77,6 +77,11 @@ public ApplyForm findById(Long applyFormId) { .orElseThrow(ApplyFormNotFoundException::new); } + public ApplyForm findByDashboardId(Long dashboardId) { + return applyFormRepository.findByDashboardId(dashboardId) + .orElseThrow(ApplyFormNotFoundException::new); + } + public ApplyForm findByDashboard(Dashboard dashboard) { return applyFormRepository.findByDashboard(dashboard) .orElseThrow(ApplyFormNotFoundException::new); diff --git a/backend/src/main/java/com/cruru/dashboard/domain/DashboardApplyFormDto.java b/backend/src/main/java/com/cruru/dashboard/domain/DashboardApplyFormDto.java new file mode 100644 index 000000000..adc37ab87 --- /dev/null +++ b/backend/src/main/java/com/cruru/dashboard/domain/DashboardApplyFormDto.java @@ -0,0 +1,7 @@ +package com.cruru.dashboard.domain; + +import com.cruru.applyform.domain.ApplyForm; + +public record DashboardApplyFormDto(Dashboard dashboard, ApplyForm applyForm) { + +} diff --git a/backend/src/main/java/com/cruru/dashboard/domain/repository/DashboardRepository.java b/backend/src/main/java/com/cruru/dashboard/domain/repository/DashboardRepository.java index 42e15f4ae..92b8c02d6 100644 --- a/backend/src/main/java/com/cruru/dashboard/domain/repository/DashboardRepository.java +++ b/backend/src/main/java/com/cruru/dashboard/domain/repository/DashboardRepository.java @@ -1,11 +1,8 @@ package com.cruru.dashboard.domain.repository; -import com.cruru.club.domain.Club; import com.cruru.dashboard.domain.Dashboard; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface DashboardRepository extends JpaRepository { - List findAllByClub(Club club); } diff --git a/backend/src/main/java/com/cruru/dashboard/facade/DashboardFacade.java b/backend/src/main/java/com/cruru/dashboard/facade/DashboardFacade.java index 818039668..38301866d 100644 --- a/backend/src/main/java/com/cruru/dashboard/facade/DashboardFacade.java +++ b/backend/src/main/java/com/cruru/dashboard/facade/DashboardFacade.java @@ -1,7 +1,6 @@ package com.cruru.dashboard.facade; import com.cruru.applicant.domain.Applicant; -import com.cruru.applicant.service.ApplicantService; import com.cruru.applyform.controller.request.ApplyFormWriteRequest; import com.cruru.applyform.domain.ApplyForm; import com.cruru.applyform.service.ApplyFormService; @@ -13,10 +12,8 @@ import com.cruru.dashboard.controller.response.DashboardsOfClubResponse; import com.cruru.dashboard.controller.response.StatsResponse; import com.cruru.dashboard.domain.Dashboard; +import com.cruru.dashboard.domain.DashboardApplyFormDto; import com.cruru.dashboard.service.DashboardService; -import com.cruru.member.service.MemberService; -import com.cruru.process.domain.Process; -import com.cruru.process.service.ProcessService; import com.cruru.question.controller.request.QuestionCreateRequest; import com.cruru.question.service.QuestionService; import java.time.Clock; @@ -33,13 +30,10 @@ @RequiredArgsConstructor public class DashboardFacade { - private final MemberService memberService; private final ClubService clubService; private final DashboardService dashboardService; private final ApplyFormService applyFormService; private final QuestionService questionService; - private final ProcessService processService; - private final ApplicantService applicantService; private final Clock clock; @Transactional @@ -64,9 +58,7 @@ private ApplyFormWriteRequest toApplyFormWriteRequest(DashboardCreateRequest req } public DashboardsOfClubResponse findAllDashboardsByClubId(long clubId) { - Club club = clubService.findById(clubId); - - List dashboards = dashboardService.findAllByClub(club); + List dashboards = dashboardService.findAllByClub(clubId); String clubName = clubService.findById(clubId).getName(); LocalDateTime now = LocalDateTime.now(clock); @@ -79,10 +71,12 @@ public DashboardsOfClubResponse findAllDashboardsByClubId(long clubId) { return new DashboardsOfClubResponse(clubName, sortedDashboardPreviews); } - private DashboardPreviewResponse createDashboardPreviewResponse(Dashboard dashboard) { - ApplyForm applyForm = applyFormService.findByDashboard(dashboard); - List allApplicants = getAllApplicantsByDashboardId(dashboard); - StatsResponse stats = calculateStats(allApplicants); + private DashboardPreviewResponse createDashboardPreviewResponse(DashboardApplyFormDto dashboardApplyformDto) { + Dashboard dashboard = dashboardApplyformDto.dashboard(); + ApplyForm applyForm = dashboardApplyformDto.applyForm(); + + List applicants = dashboardService.findAllApplicants(dashboard); + StatsResponse stats = calculateStats(applicants); return new DashboardPreviewResponse( dashboard.getId(), applyForm.getId(), @@ -115,14 +109,6 @@ private List sortDashboardPreviews( return sortedDashboards; } - private List getAllApplicantsByDashboardId(Dashboard dashboard) { - List processes = processService.findAllByDashboard(dashboard); - return processes.stream() - .flatMap(process -> applicantService.findAllByProcess(process) - .stream()) - .toList(); - } - private StatsResponse calculateStats(List allApplicants) { int totalApplicants = allApplicants.size(); int totalFails = (int) allApplicants.stream() diff --git a/backend/src/main/java/com/cruru/dashboard/service/DashboardService.java b/backend/src/main/java/com/cruru/dashboard/service/DashboardService.java index ea9881ff4..65694bc08 100644 --- a/backend/src/main/java/com/cruru/dashboard/service/DashboardService.java +++ b/backend/src/main/java/com/cruru/dashboard/service/DashboardService.java @@ -1,7 +1,11 @@ package com.cruru.dashboard.service; +import com.cruru.applicant.domain.Applicant; +import com.cruru.applicant.domain.repository.ApplicantRepository; +import com.cruru.applyform.domain.repository.ApplyFormRepository; import com.cruru.club.domain.Club; import com.cruru.dashboard.domain.Dashboard; +import com.cruru.dashboard.domain.DashboardApplyFormDto; import com.cruru.dashboard.domain.repository.DashboardRepository; import com.cruru.dashboard.exception.DashboardNotFoundException; import com.cruru.process.domain.Process; @@ -19,6 +23,8 @@ public class DashboardService { private final DashboardRepository dashboardRepository; private final ProcessRepository processRepository; + private final ApplicantRepository applicantRepository; + private final ApplyFormRepository applyFormRepository; @Transactional public Dashboard create(Club club) { @@ -35,7 +41,11 @@ public Dashboard findById(Long id) { .orElseThrow(DashboardNotFoundException::new); } - public List findAllByClub(Club club) { - return dashboardRepository.findAllByClub(club); + public List findAllByClub(long clubId) { + return applyFormRepository.findAllByClub(clubId); + } + + public List findAllApplicants(Dashboard dashboard) { + return applicantRepository.findAllByDashboard(dashboard); } } diff --git a/backend/src/main/java/com/cruru/email/controller/EmailController.java b/backend/src/main/java/com/cruru/email/controller/EmailController.java index c2413d1d3..181f0931b 100644 --- a/backend/src/main/java/com/cruru/email/controller/EmailController.java +++ b/backend/src/main/java/com/cruru/email/controller/EmailController.java @@ -1,7 +1,7 @@ package com.cruru.email.controller; -import com.cruru.email.facade.EmailFacade; import com.cruru.email.controller.dto.EmailRequest; +import com.cruru.email.facade.EmailFacade; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; diff --git a/backend/src/main/java/com/cruru/email/domain/Email.java b/backend/src/main/java/com/cruru/email/domain/Email.java index 22c7229d7..e91405ca7 100644 --- a/backend/src/main/java/com/cruru/email/domain/Email.java +++ b/backend/src/main/java/com/cruru/email/domain/Email.java @@ -3,8 +3,8 @@ import com.cruru.BaseEntity; import com.cruru.applicant.domain.Applicant; import com.cruru.club.domain.Club; -import com.cruru.email.exception.EmailSubjectLengthException; import com.cruru.email.exception.EmailContentLengthException; +import com.cruru.email.exception.EmailSubjectLengthException; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; diff --git a/backend/src/main/java/com/cruru/global/util/ExceptionLogger.java b/backend/src/main/java/com/cruru/global/util/ExceptionLogger.java index b9241d26d..ac1a78725 100644 --- a/backend/src/main/java/com/cruru/global/util/ExceptionLogger.java +++ b/backend/src/main/java/com/cruru/global/util/ExceptionLogger.java @@ -9,7 +9,6 @@ import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC; -import org.springframework.http.HttpStatus; import org.springframework.http.ProblemDetail; @Slf4j @@ -35,6 +34,10 @@ private static void setMDC(HttpServletRequest request, CruruCustomException exce MDC.put("exceptionMessage", exception.getMessage()); } + private static void clearMDC() { + MDC.clear(); + } + // MDC 초기화 public static void info(ProblemDetail problemDetail) { setMDC(problemDetail); @@ -42,6 +45,15 @@ public static void info(ProblemDetail problemDetail) { clearMDC(); } + private static void setMDC(ProblemDetail problemDetail) { + Map details = problemDetail.getProperties(); + Map map = new HashMap<>(); + for (Entry stringObjectEntry : details.entrySet()) { + map.put(stringObjectEntry.getKey(), java.lang.String.valueOf(stringObjectEntry.getValue())); + } + MDC.setContextMap(map); + } + public static void warn(ProblemDetail problemDetail) { setMDC(problemDetail); log.warn("handle warn level exception"); @@ -53,17 +65,4 @@ public static void error(ProblemDetail problemDetail) { log.error("handle error level exception"); clearMDC(); } - - private static void setMDC(ProblemDetail problemDetail) { - Map details = problemDetail.getProperties(); - Map map = new HashMap<>(); - for (Entry stringObjectEntry : details.entrySet()) { - map.put(stringObjectEntry.getKey(), java.lang.String.valueOf(stringObjectEntry.getValue())); - } - MDC.setContextMap(map); - } - - private static void clearMDC() { - MDC.clear(); - } } diff --git a/backend/src/main/java/com/cruru/process/facade/ProcessFacade.java b/backend/src/main/java/com/cruru/process/facade/ProcessFacade.java index b6fe90dd7..f4a107237 100644 --- a/backend/src/main/java/com/cruru/process/facade/ProcessFacade.java +++ b/backend/src/main/java/com/cruru/process/facade/ProcessFacade.java @@ -1,7 +1,7 @@ package com.cruru.process.facade; import com.cruru.applicant.controller.response.ApplicantCardResponse; -import com.cruru.applicant.domain.Applicant; +import com.cruru.applicant.domain.dto.ApplicantCard; import com.cruru.applicant.service.ApplicantService; import com.cruru.applicant.service.EvaluationService; import com.cruru.applyform.domain.ApplyForm; @@ -33,28 +33,27 @@ public class ProcessFacade { @Transactional public void create(ProcessCreateRequest request, long dashboardId) { Dashboard dashboard = dashboardService.findById(dashboardId); - processService.create(request, dashboard); } public ProcessResponses readAllByDashboardId(long dashboardId) { - Dashboard dashboard = dashboardService.findById(dashboardId); + ApplyForm applyForm = applyFormService.findByDashboardId(dashboardId); + List processes = processService.findAllByDashboard(dashboardId); + List applicantCards = applicantService.findApplicantCards(processes); + + List processResponses = processes.stream() + .map(process -> toProcessResponse(process, applicantCards)) + .toList(); - ApplyForm applyForm = applyFormService.findByDashboard(dashboard); - List processes = processService.findAllByDashboard(dashboard); - List processResponses = toProcessResponses(processes); return new ProcessResponses(applyForm.getId(), processResponses, applyForm.getTitle()); } - private List toProcessResponses(List processes) { - return processes.stream() - .map(this::toProcessResponse) + private ProcessResponse toProcessResponse(Process process, List applicantCards) { + List applicantCardResponses = applicantCards.stream() + .filter(card -> card.processId() == process.getId()) + .map(ApplicantCard::toResponse) .toList(); - } - private ProcessResponse toProcessResponse(Process process) { - List applicantsOfProcess = applicantService.findAllByProcess(process); - List applicantCardResponses = toApplicantCardResponses(process, applicantsOfProcess); return new ProcessResponse( process.getId(), process.getSequence(), @@ -64,21 +63,6 @@ private ProcessResponse toProcessResponse(Process process) { ); } - private List toApplicantCardResponses( - Process process, - List applicantsOfProcess - ) { - return applicantsOfProcess.stream() - .map(applicant -> toApplicantCardResponse(process, applicant)) - .toList(); - } - - private ApplicantCardResponse toApplicantCardResponse(Process process, Applicant applicant) { - int evaluationCount = evaluationService.count(process, applicant); - double averageScore = evaluationService.calculateAverageScore(process, applicant); - return applicantService.toApplicantCardResponse(applicant, evaluationCount, averageScore); - } - @Transactional public ProcessResponse update(ProcessUpdateRequest request, long processId) { Process process = processService.findById(processId); @@ -87,11 +71,24 @@ public ProcessResponse update(ProcessUpdateRequest request, long processId) { return toProcessResponse(process); } + private ProcessResponse toProcessResponse(Process process) { + List applicantCardResponses = applicantService.findApplicantCards(process) + .stream() + .map(ApplicantCard::toResponse) + .toList(); + + return new ProcessResponse( + process.getId(), + process.getSequence(), + process.getName(), + process.getDescription(), + applicantCardResponses + ); + } + @Transactional public void delete(long processId) { - Process process = processService.findById(processId); - - evaluationService.deleteByProcess(process); + evaluationService.deleteByProcess(processId); processService.delete(processId); } } diff --git a/backend/src/main/java/com/cruru/process/service/ProcessService.java b/backend/src/main/java/com/cruru/process/service/ProcessService.java index c617fd6d8..23d1a8716 100644 --- a/backend/src/main/java/com/cruru/process/service/ProcessService.java +++ b/backend/src/main/java/com/cruru/process/service/ProcessService.java @@ -64,6 +64,10 @@ private Process toEvaluateProcess(ProcessCreateRequest request, Dashboard dashbo return new Process(request.sequence(), request.name(), request.description(), ProcessType.EVALUATE, dashboard); } + public List findAllByDashboard(Long dashboardId) { + return processRepository.findAllByDashboardId(dashboardId); + } + public Process findApplyProcessOnDashboard(Dashboard dashboard) { List processes = findAllByDashboard(dashboard); return processes.stream() diff --git a/backend/src/main/java/com/cruru/question/domain/repository/AnswerRepository.java b/backend/src/main/java/com/cruru/question/domain/repository/AnswerRepository.java index 23d4bb969..e0c608f8a 100644 --- a/backend/src/main/java/com/cruru/question/domain/repository/AnswerRepository.java +++ b/backend/src/main/java/com/cruru/question/domain/repository/AnswerRepository.java @@ -4,8 +4,11 @@ import com.cruru.question.domain.Answer; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public interface AnswerRepository extends JpaRepository { - List findAllByApplicant(Applicant applicant); + @Query("SELECT a FROM Answer a JOIN FETCH a.question WHERE a.applicant = :applicant") + List findAllByApplicantWithQuestions(@Param("applicant") Applicant applicant); } diff --git a/backend/src/main/java/com/cruru/question/service/AnswerService.java b/backend/src/main/java/com/cruru/question/service/AnswerService.java index 2dfe2c046..bb1e5780b 100644 --- a/backend/src/main/java/com/cruru/question/service/AnswerService.java +++ b/backend/src/main/java/com/cruru/question/service/AnswerService.java @@ -40,8 +40,8 @@ public void saveAnswerReplies(AnswerCreateRequest answerCreateRequest, Question } } - public List findAllByApplicant(Applicant applicant) { - return answerRepository.findAllByApplicant(applicant); + public List findAllByApplicantWithQuestions(Applicant applicant) { + return answerRepository.findAllByApplicantWithQuestions(applicant); } public List toAnswerResponses(List answers) { diff --git a/backend/src/test/java/com/cruru/applicant/domain/repository/ApplicantRepositoryTest.java b/backend/src/test/java/com/cruru/applicant/domain/repository/ApplicantRepositoryTest.java index f44ad8a8b..2727ab728 100644 --- a/backend/src/test/java/com/cruru/applicant/domain/repository/ApplicantRepositoryTest.java +++ b/backend/src/test/java/com/cruru/applicant/domain/repository/ApplicantRepositoryTest.java @@ -1,10 +1,25 @@ package com.cruru.applicant.domain.repository; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; import com.cruru.applicant.domain.Applicant; +import com.cruru.dashboard.domain.Dashboard; +import com.cruru.dashboard.domain.repository.DashboardRepository; +import com.cruru.process.domain.Process; +import com.cruru.process.domain.repository.ProcessRepository; +import com.cruru.applicant.domain.Evaluation; +import com.cruru.applicant.domain.dto.ApplicantCard; +import com.cruru.process.domain.Process; +import com.cruru.process.domain.repository.ProcessRepository; import com.cruru.util.RepositoryTest; import com.cruru.util.fixture.ApplicantFixture; +import com.cruru.util.fixture.DashboardFixture; +import com.cruru.util.fixture.ProcessFixture; +import java.util.List; +import com.cruru.util.fixture.EvaluationFixture; +import com.cruru.util.fixture.ProcessFixture; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -16,6 +31,15 @@ class ApplicantRepositoryTest extends RepositoryTest { @Autowired private ApplicantRepository applicantRepository; + @Autowired + private DashboardRepository dashboardRepository; + + @Autowired + private ProcessRepository processRepository; + + @Autowired + private EvaluationRepository evaluationRepository; + @BeforeEach void setUp() { applicantRepository.deleteAllInBatch(); @@ -53,4 +77,149 @@ void saveNoId() { //then assertThat(savedApplicant1.getId() + 1).isEqualTo(savedApplicant2.getId()); } + + @DisplayName("특정 Process들에 대한 ApplicantCard 목록을 반환한다.") + @Test + void findApplicantCardsByProcesses() { + // given + Process process = processRepository.save(ProcessFixture.applyType()); + + Applicant applicant1 = ApplicantFixture.pendingDobby(process); + Applicant applicant2 = ApplicantFixture.pendingRush(process); + applicantRepository.saveAll(List.of(applicant1, applicant2)); + + List evaluations = List.of( + EvaluationFixture.fivePoints(process, applicant1), + EvaluationFixture.fivePoints(process, applicant1), + EvaluationFixture.fivePoints(process, applicant2) + ); + evaluationRepository.saveAll(evaluations); + + // when + List applicantCards = applicantRepository.findApplicantCardsByProcesses(List.of(process)); + + // then + assertThat(applicantCards).hasSize(2); + + ApplicantCard applicantCard1 = applicantCards.get(0); + assertAll( + () -> assertThat(applicantCard1.id()).isEqualTo(applicant1.getId()), + () -> assertThat(applicantCard1.name()).isEqualTo(applicant1.getName()), + () -> assertThat(applicantCard1.evaluationCount()).isEqualTo(2), + () -> assertThat(applicantCard1.averageScore()).isEqualTo(5.0) + ); + + ApplicantCard applicantCard2 = applicantCards.get(1); + assertAll( + () -> assertThat(applicantCard2.id()).isEqualTo(applicant2.getId()), + () -> assertThat(applicantCard2.name()).isEqualTo(applicant2.getName()), + () -> assertThat(applicantCard2.evaluationCount()).isEqualTo(1), + () -> assertThat(applicantCard2.averageScore()).isEqualTo(5.0) + ); + } + + @DisplayName("평가가 없을 경우 ApplicantCard 목록에서 평균 점수는 0점이고 카운트는 0이다.") + @Test + void findApplicantCardsByProcesses_noEvaluations() { + // given + Process process = processRepository.save(ProcessFixture.applyType()); + Applicant applicant = applicantRepository.save(ApplicantFixture.pendingDobby(process)); + + // when + List applicantCards = applicantRepository.findApplicantCardsByProcesses(List.of(process)); + + // then + assertThat(applicantCards).hasSize(1); + ApplicantCard applicantCard = applicantCards.get(0); + + assertAll( + () -> assertThat(applicantCard.id()).isEqualTo(applicant.getId()), + () -> assertThat(applicantCard.name()).isEqualTo(applicant.getName()), + () -> assertThat(applicantCard.evaluationCount()).isZero(), + () -> assertThat(applicantCard.averageScore()).isZero() + ); + } + + @DisplayName("특정 Process에 대한 ApplicantCard 목록을 반환한다.") + @Test + void findApplicantCardsByProcess() { + // given + Process process = processRepository.save(ProcessFixture.applyType()); + + Applicant applicant1 = ApplicantFixture.pendingDobby(process); + Applicant applicant2 = ApplicantFixture.pendingRush(process); + applicantRepository.saveAll(List.of(applicant1, applicant2)); + + List evaluations = List.of( + EvaluationFixture.fivePoints(process, applicant1), + EvaluationFixture.fivePoints(process, applicant1), + EvaluationFixture.fivePoints(process, applicant2) + ); + evaluationRepository.saveAll(evaluations); + + // when + List applicantCards = applicantRepository.findApplicantCardsByProcess(process); + + // then + assertThat(applicantCards).hasSize(2); + + ApplicantCard applicantCard1 = applicantCards.get(0); + assertAll( + () -> assertThat(applicantCard1.id()).isEqualTo(applicant1.getId()), + () -> assertThat(applicantCard1.name()).isEqualTo(applicant1.getName()), + () -> assertThat(applicantCard1.evaluationCount()).isEqualTo(2), + () -> assertThat(applicantCard1.averageScore()).isEqualTo(5.0) + ); + + ApplicantCard applicantCard2 = applicantCards.get(1); + assertAll( + () -> assertThat(applicantCard2.id()).isEqualTo(applicant2.getId()), + () -> assertThat(applicantCard2.name()).isEqualTo(applicant2.getName()), + () -> assertThat(applicantCard2.evaluationCount()).isEqualTo(1), + () -> assertThat(applicantCard2.averageScore()).isEqualTo(5.0) + ); + } + + @DisplayName("평가가 없을 경우 ApplicantCard 목록에서 평균 점수는 0점이고 카운트는 0이다.") + @Test + void findApplicantCardsByProcess_noEvaluations() { + // given + Process process = processRepository.save(ProcessFixture.applyType()); + Applicant applicant = applicantRepository.save(ApplicantFixture.pendingDobby(process)); + + // when + List applicantCards = applicantRepository.findApplicantCardsByProcess(process); + + // then + assertThat(applicantCards).hasSize(1); + ApplicantCard applicantCard = applicantCards.get(0); + + assertAll( + () -> assertThat(applicantCard.id()).isEqualTo(applicant.getId()), + () -> assertThat(applicantCard.name()).isEqualTo(applicant.getName()), + () -> assertThat(applicantCard.evaluationCount()).isZero(), + () -> assertThat(applicantCard.averageScore()).isZero() + ); + } + + @DisplayName("특정 대시보드에 해당하는 지원자 목록을 반환한다.") + @Test + void findAllByDashboard() { + // given + Dashboard dashboard = dashboardRepository.save(DashboardFixture.backend()); + + Process process1 = processRepository.save(ProcessFixture.applyType(dashboard)); + Process process2 = processRepository.save(ProcessFixture.approveType(dashboard)); + + Applicant applicant1 = applicantRepository.save(ApplicantFixture.pendingDobby(process1)); + Applicant applicant2 = applicantRepository.save(ApplicantFixture.pendingDobby(process1)); + Applicant applicant3 = applicantRepository.save(ApplicantFixture.pendingDobby(process2)); + + // when + List applicants = applicantRepository.findAllByDashboard(dashboard); + + // then + assertThat(applicants).hasSize(3); + assertThat(applicants).containsExactlyInAnyOrder(applicant1, applicant2, applicant3); + } } diff --git a/backend/src/test/java/com/cruru/applicant/service/EvaluationServiceTest.java b/backend/src/test/java/com/cruru/applicant/service/EvaluationServiceTest.java index 1a21f9179..92e7a33d6 100644 --- a/backend/src/test/java/com/cruru/applicant/service/EvaluationServiceTest.java +++ b/backend/src/test/java/com/cruru/applicant/service/EvaluationServiceTest.java @@ -111,33 +111,4 @@ void update() { () -> assertThat(updatedEvaluation.get().getContent()).isEqualTo(content) ); } - - @DisplayName("평가에 대한 평균 점수를 계산한다.") - @Test - void calculateAverageScore() { - // given - List evaluations = List.of( - new Evaluation(1, null, process, applicant), - new Evaluation(2, null, process, applicant), - new Evaluation(3, null, process, applicant) - ); - evaluationRepository.saveAll(evaluations); - - // when - // 평균 점수 = (1 + 2 + 3) / 3 = 2 - double averageScore = evaluationService.calculateAverageScore(process, applicant); - - // then - assertThat(averageScore).isEqualTo(2.0); - } - - @DisplayName("평가가 없을 시 평균 점수는 0점이다.") - @Test - void calculateAverageScore_zero() { - // when - double averageScore = evaluationService.calculateAverageScore(process, applicant); - - // then - assertThat(averageScore).isZero(); - } } diff --git a/backend/src/test/java/com/cruru/applyform/domain/repository/ApplyFormRepositoryTest.java b/backend/src/test/java/com/cruru/applyform/domain/repository/ApplyFormRepositoryTest.java index 0ef2c691f..fe28c6d23 100644 --- a/backend/src/test/java/com/cruru/applyform/domain/repository/ApplyFormRepositoryTest.java +++ b/backend/src/test/java/com/cruru/applyform/domain/repository/ApplyFormRepositoryTest.java @@ -4,13 +4,18 @@ import static org.junit.jupiter.api.Assertions.assertAll; import com.cruru.applyform.domain.ApplyForm; +import com.cruru.club.domain.Club; +import com.cruru.club.domain.repository.ClubRepository; import com.cruru.dashboard.domain.Dashboard; +import com.cruru.dashboard.domain.DashboardApplyFormDto; import com.cruru.dashboard.domain.repository.DashboardRepository; import com.cruru.util.RepositoryTest; import com.cruru.util.fixture.ApplyFormFixture; +import com.cruru.util.fixture.ClubFixture; import com.cruru.util.fixture.DashboardFixture; import com.cruru.util.fixture.LocalDateFixture; import java.time.LocalDateTime; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -25,6 +30,9 @@ class ApplyFormRepositoryTest extends RepositoryTest { @Autowired private DashboardRepository dashboardRepository; + @Autowired + private ClubRepository clubRepository; + @BeforeEach void setUp() { applyFormRepository.deleteAllInBatch(); @@ -80,4 +88,32 @@ void save_NotSavedId() { //then assertThat(savedApplyForm1.getId() + 1).isEqualTo(savedApplyForm2.getId()); } + + @DisplayName("특정 동아리에 속하는 DashboardApplyForm 목록을 반환한다.") + @Test + void findAllByClub() { + // given + Club club = clubRepository.save(ClubFixture.create()); + Dashboard dashboard1 = dashboardRepository.save(DashboardFixture.frontend(club)); + Dashboard dashboard2 = dashboardRepository.save(DashboardFixture.backend(club)); + + ApplyForm applyForm1 = applyFormRepository.save(ApplyFormFixture.frontend(dashboard1)); + ApplyForm applyForm2 = applyFormRepository.save(ApplyFormFixture.backend(dashboard2)); + + // when + List dashboardApplyFormDtos = applyFormRepository.findAllByClub(club.getId()); + + // then + assertThat(dashboardApplyFormDtos).hasSize(2); + + DashboardApplyFormDto dto1 = dashboardApplyFormDtos.get(0); + DashboardApplyFormDto dto2 = dashboardApplyFormDtos.get(1); + + assertAll( + () -> assertThat(dto1.dashboard().getId()).isEqualTo(dashboard1.getId()), + () -> assertThat(dto1.applyForm().getId()).isEqualTo(applyForm1.getId()), + () -> assertThat(dto2.dashboard().getId()).isEqualTo(dashboard2.getId()), + () -> assertThat(dto2.applyForm().getId()).isEqualTo(applyForm2.getId()) + ); + } } diff --git a/backend/src/test/java/com/cruru/applyform/facade/ApplyFormFacadeTest.java b/backend/src/test/java/com/cruru/applyform/facade/ApplyFormFacadeTest.java index 80ec5e075..80bca4a48 100644 --- a/backend/src/test/java/com/cruru/applyform/facade/ApplyFormFacadeTest.java +++ b/backend/src/test/java/com/cruru/applyform/facade/ApplyFormFacadeTest.java @@ -10,6 +10,7 @@ import com.cruru.applyform.controller.request.AnswerCreateRequest; import com.cruru.applyform.controller.request.ApplyFormSubmitRequest; import com.cruru.applyform.controller.request.ApplyFormWriteRequest; +import com.cruru.applyform.controller.response.ApplyFormResponse; import com.cruru.applyform.domain.ApplyForm; import com.cruru.applyform.domain.repository.ApplyFormRepository; import com.cruru.applyform.exception.ApplyFormNotFoundException; @@ -19,8 +20,10 @@ import com.cruru.dashboard.domain.repository.DashboardRepository; import com.cruru.process.domain.Process; import com.cruru.process.domain.repository.ProcessRepository; +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; @@ -62,6 +65,7 @@ class ApplyFormFacadeTest extends ServiceTest { private Process firstProcess; private Process finalProcess; private ApplyForm applyForm; + private Question question1; private List answerCreateRequests; private ApplyFormSubmitRequest applyFormSubmitrequest; private ApplicantCreateRequest applicantCreateRequest; @@ -72,7 +76,7 @@ void setUp() { firstProcess = processRepository.save(ProcessFixture.applyType(dashboard)); finalProcess = processRepository.save(ProcessFixture.approveType(dashboard)); applyForm = applyFormRepository.save(ApplyFormFixture.backend(dashboard)); - Question question1 = questionRepository.save(QuestionFixture.longAnswerType(applyForm)); + question1 = questionRepository.save(QuestionFixture.longAnswerType(applyForm)); Question question2 = questionRepository.save(QuestionFixture.shortAnswerType(applyForm)); answerCreateRequests = List.of( @@ -138,10 +142,12 @@ void submit_invalidSubmitDate() { // given ApplyForm pastApplyForm = applyFormRepository.save(new ApplyForm( "지난 모집 공고", "description", - LocalDateFixture.oneWeekAgo(), LocalDateFixture.oneDayAgo(), null)); + LocalDateFixture.oneWeekAgo(), LocalDateFixture.oneDayAgo(), null + )); ApplyForm futureApplyForm = applyFormRepository.save(new ApplyForm( "미래의 모집 공고", "description", - LocalDateFixture.oneDayLater(), LocalDateFixture.oneWeekLater(), null)); + LocalDateFixture.oneDayLater(), LocalDateFixture.oneWeekLater(), null + )); // when&then assertAll( @@ -187,4 +193,26 @@ void update() { () -> assertThat(actual.getEndDate()).isEqualTo(toChangeEndDate) ); } + + @DisplayName("지원서 폼 조회에 성공한다.") + @Test + void readApplyFormById() { + // given&when + ApplyFormResponse applyFormResponse = applyFormFacade.readApplyFormById(applyForm.getId()); + + // then + assertAll( + () -> assertThat(applyFormResponse.title()).isEqualTo(applyForm.getTitle()), + () -> assertThat(applyFormResponse.startDate()).isEqualTo(applyForm.getStartDate()), + () -> assertThat(applyFormResponse.endDate()).isEqualTo(applyForm.getEndDate()), + () -> { + QuestionResponse questionResponse = applyFormResponse.questionResponses().get(0); + assertThat(questionResponse.id()).isEqualTo(question1.getId()); + assertThat(questionResponse.content()).isEqualTo(question1.getContent()); + assertThat(questionResponse.orderIndex()).isEqualTo(question1.getSequence()); + assertThat(questionResponse.required()).isEqualTo(question1.isRequired()); + assertThat(questionResponse.choiceResponses()).isEmpty(); + } + ); + } } diff --git a/backend/src/test/java/com/cruru/applyform/service/ApplyFormServiceTest.java b/backend/src/test/java/com/cruru/applyform/service/ApplyFormServiceTest.java index 4c339d6e1..36457dbc6 100644 --- a/backend/src/test/java/com/cruru/applyform/service/ApplyFormServiceTest.java +++ b/backend/src/test/java/com/cruru/applyform/service/ApplyFormServiceTest.java @@ -139,7 +139,7 @@ void update() { String toChangeDescription = "# 모집 공고 설명 #"; LocalDateTime toChangeStartDate = LocalDateFixture.oneDayLater(); LocalDateTime toChangeEndDate = LocalDateFixture.oneWeekLater(); - + ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.backend(dashboard)); ApplyFormWriteRequest request = new ApplyFormWriteRequest( toChangeTitle, toChangeDescription, toChangeStartDate, toChangeEndDate diff --git a/backend/src/test/java/com/cruru/dashboard/facade/DashboardFacadeTest.java b/backend/src/test/java/com/cruru/dashboard/facade/DashboardFacadeTest.java index 8363e9d38..80176537b 100644 --- a/backend/src/test/java/com/cruru/dashboard/facade/DashboardFacadeTest.java +++ b/backend/src/test/java/com/cruru/dashboard/facade/DashboardFacadeTest.java @@ -93,9 +93,13 @@ void create() { void findAllDashboardsByClubId() { // given Dashboard dashboard = dashboardRepository.save(DashboardFixture.backend(club)); + Dashboard dashboard1 = dashboardRepository.save(DashboardFixture.backend(club)); ApplyForm applyForm = applyFormRepository.save(ApplyFormFixture.backend(dashboard)); + ApplyForm applyForm1 = applyFormRepository.save(ApplyFormFixture.backend(dashboard1)); Process firstProcess = processRepository.save(ProcessFixture.applyType(dashboard)); Process lastProcess = processRepository.save(ProcessFixture.approveType(dashboard)); + Process firstProcess1 = processRepository.save(ProcessFixture.applyType(dashboard1)); + Process lastProcess1 = processRepository.save(ProcessFixture.approveType(dashboard1)); List applicants = List.of( // 마지막 프로세스에 있으면서 불합격 상태인 경우, 불합격 @@ -108,6 +112,16 @@ void findAllDashboardsByClubId() { ); applicantRepository.saveAll(applicants); + List applicants1 = List.of( + ApplicantFixture.rejectedRush(lastProcess1), + ApplicantFixture.rejectedRush(firstProcess1), + ApplicantFixture.pendingDobby(lastProcess1), + ApplicantFixture.pendingDobby(firstProcess1), + ApplicantFixture.pendingDobby(firstProcess1), + ApplicantFixture.pendingDobby(firstProcess1) + ); + applicantRepository.saveAll(applicants1); + // when DashboardsOfClubResponse dashboardsOfClubResponse = dashboardFacade.findAllDashboardsByClubId(club.getId()); diff --git a/backend/src/test/java/com/cruru/dashboard/service/DashboardServiceTest.java b/backend/src/test/java/com/cruru/dashboard/service/DashboardServiceTest.java index cdb80d889..f80aa79bb 100644 --- a/backend/src/test/java/com/cruru/dashboard/service/DashboardServiceTest.java +++ b/backend/src/test/java/com/cruru/dashboard/service/DashboardServiceTest.java @@ -4,13 +4,17 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import com.cruru.applyform.domain.ApplyForm; +import com.cruru.applyform.domain.repository.ApplyFormRepository; import com.cruru.club.domain.Club; import com.cruru.club.domain.repository.ClubRepository; import com.cruru.dashboard.domain.Dashboard; +import com.cruru.dashboard.domain.DashboardApplyFormDto; import com.cruru.dashboard.domain.repository.DashboardRepository; import com.cruru.process.domain.Process; import com.cruru.process.domain.repository.ProcessRepository; import com.cruru.util.ServiceTest; +import com.cruru.util.fixture.ApplyFormFixture; import com.cruru.util.fixture.ClubFixture; import com.cruru.util.fixture.DashboardFixture; import java.util.Comparator; @@ -34,6 +38,9 @@ class DashboardServiceTest extends ServiceTest { @Autowired private DashboardRepository dashboardRepository; + @Autowired + private ApplyFormRepository applyFormRepository; + @DisplayName("새로운 대시보드를 생성시, 기본 프로세스 2개가 생성된다.") @Test void create_createDefaultProcess() { @@ -51,7 +58,7 @@ void create_createDefaultProcess() { .toList(); assertAll( () -> assertThat(processes).hasSize(2), - () -> assertThat(processes.get(0).getSequence()).isEqualTo(0), + () -> assertThat(processes.get(0).getSequence()).isZero(), () -> assertThat(processes.get(1).getSequence()).isEqualTo(1) ); } @@ -75,11 +82,13 @@ void findAllByClub() { Club club = clubRepository.save(ClubFixture.create()); Dashboard backendDashboard = dashboardRepository.save(DashboardFixture.backend(club)); Dashboard frontendDashboard = dashboardRepository.save(DashboardFixture.frontend(club)); + ApplyForm backendApplyform = applyFormRepository.save(ApplyFormFixture.backend(backendDashboard)); + ApplyForm frontendApplyform = applyFormRepository.save(ApplyFormFixture.frontend(frontendDashboard)); - // when & then - assertThat(dashboardService.findAllByClub(club)).containsExactlyInAnyOrder( - backendDashboard, - frontendDashboard + // when&then + assertThat(dashboardService.findAllByClub(club.getId())).containsExactlyInAnyOrder( + new DashboardApplyFormDto(backendDashboard, backendApplyform), + new DashboardApplyFormDto(frontendDashboard, frontendApplyform) ); } } diff --git a/backend/src/test/java/com/cruru/email/domain/EmailTest.java b/backend/src/test/java/com/cruru/email/domain/EmailTest.java index ad66a11c3..c7de2853e 100644 --- a/backend/src/test/java/com/cruru/email/domain/EmailTest.java +++ b/backend/src/test/java/com/cruru/email/domain/EmailTest.java @@ -39,7 +39,7 @@ void invalidEmailContentLength(String content) { StringBuilder stringBuilder = new StringBuilder(content.length() * repeatCount); String thisContentLengthIs10000 = content.repeat(repeatCount); stringBuilder.append(thisContentLengthIs10000); - + String subject = EmailFixture.SUBJECT; String invalidContent = stringBuilder.append("!").toString(); diff --git a/backend/src/test/java/com/cruru/email/domain/repository/EmailRepositoryTest.java b/backend/src/test/java/com/cruru/email/domain/repository/EmailRepositoryTest.java index a6d0d74e7..fd39d6ca9 100644 --- a/backend/src/test/java/com/cruru/email/domain/repository/EmailRepositoryTest.java +++ b/backend/src/test/java/com/cruru/email/domain/repository/EmailRepositoryTest.java @@ -12,7 +12,7 @@ @DisplayName("발송 내역 레포지토리 테스트") class EmailRepositoryTest extends RepositoryTest { - + @Autowired private EmailRepository emailRepository; diff --git a/backend/src/test/java/com/cruru/email/facade/EmailFacadeTest.java b/backend/src/test/java/com/cruru/email/facade/EmailFacadeTest.java index 10c38f2e3..6c451593c 100644 --- a/backend/src/test/java/com/cruru/email/facade/EmailFacadeTest.java +++ b/backend/src/test/java/com/cruru/email/facade/EmailFacadeTest.java @@ -25,15 +25,13 @@ @DisplayName("발송 내역 파사드 테스트") class EmailFacadeTest extends ServiceTest { + @SpyBean + EmailService emailService; @Autowired private ApplicantRepository applicantRepository; - @Autowired private EmailFacade emailFacade; - @SpyBean - EmailService emailService; - @DisplayName("이메일을 비동기로 발송하고, 발송 내역을 저장한다.") @Test void sendAndSave() { diff --git a/backend/src/test/java/com/cruru/process/controller/ProcessControllerTest.java b/backend/src/test/java/com/cruru/process/controller/ProcessControllerTest.java index 486b47051..53dd0b21e 100644 --- a/backend/src/test/java/com/cruru/process/controller/ProcessControllerTest.java +++ b/backend/src/test/java/com/cruru/process/controller/ProcessControllerTest.java @@ -50,7 +50,7 @@ class ProcessControllerTest extends ControllerTest { fieldWithPath("isRejected").description("지원자의 불합격 여부"), fieldWithPath("evaluationCount").description("지원자의 평가 개수"), fieldWithPath("averageScore").description("지원자의 평가 평균 점수"), - }; + }; private static final FieldDescriptor[] PROCESS_CREATE_FIELD_DESCRIPTORS = { fieldWithPath("processName").description("프로세스명"), diff --git a/backend/src/test/java/com/cruru/process/facade/ProcessFacadeTest.java b/backend/src/test/java/com/cruru/process/facade/ProcessFacadeTest.java index 16eefac6d..a5afad632 100644 --- a/backend/src/test/java/com/cruru/process/facade/ProcessFacadeTest.java +++ b/backend/src/test/java/com/cruru/process/facade/ProcessFacadeTest.java @@ -74,13 +74,25 @@ void readAllByDashboardId() { // given applyFormRepository.save(ApplyFormFixture.backend(defaultDashboard)); Process process = processRepository.save(ProcessFixture.applyType(defaultDashboard)); + Process process1 = processRepository.save(ProcessFixture.interview(defaultDashboard)); Applicant applicant = applicantRepository.save(ApplicantFixture.pendingDobby(process)); + Applicant applicant1 = applicantRepository.save(ApplicantFixture.pendingDobby(process)); List evaluations = List.of( EvaluationFixture.fivePoints(process, applicant), + EvaluationFixture.fourPoints(process, applicant), + EvaluationFixture.fourPoints(process, applicant), EvaluationFixture.fourPoints(process, applicant) ); evaluationRepository.saveAll(evaluations); + List evaluations1 = List.of( + EvaluationFixture.fivePoints(process1, applicant1), + EvaluationFixture.fivePoints(process1, applicant1), + EvaluationFixture.fivePoints(process1, applicant1), + EvaluationFixture.fourPoints(process1, applicant1) + ); + evaluationRepository.saveAll(evaluations1); + // when ProcessResponses processResponses = processFacade.readAllByDashboardId(defaultDashboard.getId()); @@ -89,11 +101,11 @@ void readAllByDashboardId() { long processId = firstProcessResponse.id(); ApplicantCardResponse applicantCardResponse = firstProcessResponse.applicantCardResponses().get(0); assertAll( - () -> assertThat(processResponses.processResponses()).hasSize(1), + () -> assertThat(processResponses.processResponses()).hasSize(2), () -> assertThat(processId).isEqualTo(process.getId()), () -> assertThat(applicantCardResponse.id()).isEqualTo(applicant.getId()), () -> assertThat(applicantCardResponse.evaluationCount()).isEqualTo(evaluations.size()), - () -> assertThat(applicantCardResponse.averageScore()).isEqualTo(4.5) + () -> assertThat(applicantCardResponse.averageScore()).isEqualTo(4.25) ); } diff --git a/backend/src/test/java/com/cruru/question/domain/repository/AnswerRepositoryTest.java b/backend/src/test/java/com/cruru/question/domain/repository/AnswerRepositoryTest.java index 05d435e7d..860d17f19 100644 --- a/backend/src/test/java/com/cruru/question/domain/repository/AnswerRepositoryTest.java +++ b/backend/src/test/java/com/cruru/question/domain/repository/AnswerRepositoryTest.java @@ -1,11 +1,20 @@ package com.cruru.question.domain.repository; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import com.cruru.applicant.domain.Applicant; +import com.cruru.applicant.domain.repository.ApplicantRepository; +import com.cruru.process.domain.Process; +import com.cruru.process.domain.repository.ProcessRepository; import com.cruru.question.domain.Answer; import com.cruru.question.domain.Question; import com.cruru.util.RepositoryTest; +import com.cruru.util.fixture.AnswerFixture; +import com.cruru.util.fixture.ApplicantFixture; +import com.cruru.util.fixture.ProcessFixture; import com.cruru.util.fixture.QuestionFixture; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -17,6 +26,15 @@ class AnswerRepositoryTest extends RepositoryTest { @Autowired private AnswerRepository answerRepository; + @Autowired + private ProcessRepository processRepository; + + @Autowired + private ApplicantRepository applicantRepository; + + @Autowired + private QuestionRepository questionRepository; + @BeforeEach void setUp() { answerRepository.deleteAllInBatch(); @@ -54,4 +72,25 @@ void saveNoId() { //then assertThat(savedAnswer1.getId() + 1).isEqualTo(savedAnswer2.getId()); } + + @DisplayName("특정 지원자와 질문에 해당하는 답변 목록을 조회한다.") + @Test + void findAllByApplicantWithQuestions() { + // given + Process process = processRepository.save(ProcessFixture.applyType()); + Applicant applicant = applicantRepository.save(ApplicantFixture.pendingDobby(process)); + Question question = questionRepository.save(QuestionFixture.shortAnswerType(null)); + answerRepository.save(AnswerFixture.first(question, applicant)); + + // when + List actual = answerRepository.findAllByApplicantWithQuestions(applicant); + + // then + Answer actualAnswer = actual.get(0); + assertAll( + () -> assertThat(actual).hasSize(1), + () -> assertThat(actualAnswer.getQuestion()).isEqualTo(question), + () -> assertThat(actualAnswer.getApplicant()).isEqualTo(applicant) + ); + } } diff --git a/backend/src/test/java/com/cruru/question/service/AnswerServiceTest.java b/backend/src/test/java/com/cruru/question/service/AnswerServiceTest.java index c9b1a9d5c..4a8edc112 100644 --- a/backend/src/test/java/com/cruru/question/service/AnswerServiceTest.java +++ b/backend/src/test/java/com/cruru/question/service/AnswerServiceTest.java @@ -61,7 +61,7 @@ void savedAnswerReplies() { answerService.saveAnswerReplies(request, question1, applicant); // then - List actualAnswer = answerRepository.findAllByApplicant(applicant); + List actualAnswer = answerRepository.findAllByApplicantWithQuestions(applicant); String content = actualAnswer.get(0).getContent(); assertAll( () -> assertThat(actualAnswer).hasSize(1), @@ -81,7 +81,7 @@ void savedAnswerReplies_notRequiredReplyNotExists() { answerService.saveAnswerReplies(request, question, applicant); // then - List actualAnswer = answerRepository.findAllByApplicant(applicant); + List actualAnswer = answerRepository.findAllByApplicantWithQuestions(applicant); String content = actualAnswer.get(0).getContent(); assertAll( () -> assertThat(actualAnswer).hasSize(1), @@ -112,7 +112,7 @@ void findAllByApplicant() { )); // when - List actualAnswers = answerService.findAllByApplicant(applicant); + List actualAnswers = answerService.findAllByApplicantWithQuestions(applicant); // then assertThat(actualAnswers).hasSameElementsAs(expectedAnswers); diff --git a/backend/src/test/java/com/cruru/util/ServiceTest.java b/backend/src/test/java/com/cruru/util/ServiceTest.java index 516c57f21..2d7f16e12 100644 --- a/backend/src/test/java/com/cruru/util/ServiceTest.java +++ b/backend/src/test/java/com/cruru/util/ServiceTest.java @@ -38,7 +38,8 @@ public class ServiceTest { protected Club defaultClub; protected Dashboard defaultDashboard; protected LoginProfile loginProfile; - + @MockBean + protected JavaMailSender javaMailSender; @Autowired private DbCleaner dbCleaner; @Autowired @@ -49,8 +50,6 @@ public class ServiceTest { private DashboardRepository dashboardRepository; @SpyBean private Clock clock; - @MockBean - protected JavaMailSender javaMailSender; @BeforeEach void resetDb() { diff --git a/backend/src/test/java/com/cruru/util/fixture/ApplicantFixture.java b/backend/src/test/java/com/cruru/util/fixture/ApplicantFixture.java index 1b8f5348f..eb1ac7b57 100644 --- a/backend/src/test/java/com/cruru/util/fixture/ApplicantFixture.java +++ b/backend/src/test/java/com/cruru/util/fixture/ApplicantFixture.java @@ -17,6 +17,10 @@ public static Applicant pendingRush() { return new Applicant("러쉬", "RUSH@email.com", "01000000001", null); } + public static Applicant pendingRush(Process process) { + return new Applicant("러쉬", "RUSH@email.com", "01000000001", process); + } + public static Applicant rejectedRush() { Applicant applicant = new Applicant("러쉬", "RUSH@email.com", "01000000001", null); applicant.reject();