Skip to content

Commit

Permalink
♻️ refactor: QueryDsl 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn committed Jan 28, 2024
1 parent 1da524c commit d3e9f8a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
3 changes: 3 additions & 0 deletions http/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,6 @@ Content-Type: application/json

### 보고서 조회
GET http://localhost:8080/api/v1/reports/0

### 공고 검색
GET http://localhost:8080/api/v1/announcements?search=무신사
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public ApiResponse<ProposeCreateResponse> createPropose(
return ApiResponse.onSuccess(proposeService.createPropose(authOrganization, request));
}

// TODO QueryDsl 변경 테스트 후 수정 필요
@GetMapping("/me")
public ApiResponse<List<ProposeSummaryGetResponse>> getMyProposes(
@AuthOrganization Organization authOrganization,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sponus.sponusbe.domain.propose.repository;

import java.util.List;

import com.sponus.sponusbe.domain.propose.entity.Propose;

public interface ProposeCustomRepository {

List<Propose> findSentPropose(Long id);

List<Propose> findReceivedProposeWithAnnouncementId(Long organizationId, Long announcementId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.sponus.sponusbe.domain.propose.repository;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.sponus.sponusbe.domain.propose.entity.Propose;
import com.sponus.sponusbe.domain.propose.entity.QPropose;

import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Repository
public class ProposeCustomRepositoryImpl implements ProposeCustomRepository {

private final EntityManager entityManager;

@Override

public List<Propose> findSentPropose(Long id) {
QPropose p = QPropose.propose;

JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

return queryFactory.selectFrom(p)
.where(p.proposingOrganization.id.eq(id))
.leftJoin(p.proposingOrganization).fetchJoin()
.fetch();
}

@Override
public List<Propose> findReceivedProposeWithAnnouncementId(Long organizationId, Long announcementId) {
QPropose p = QPropose.propose;

JPAQueryFactory queryFactory = new JPAQueryFactory(entityManager);

return queryFactory.selectFrom(p)
.where(p.proposedOrganization.id.eq(organizationId)
.and(p.announcement.id.eq(announcementId)))
.leftJoin(p.proposedOrganization).fetchJoin()
.leftJoin(p.announcement)
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.sponus.sponusbe.domain.propose.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.sponus.sponusbe.domain.propose.entity.Propose;

public interface ProposeRepository extends JpaRepository<Propose, Long> {
@Query("SELECT p FROM Propose p WHERE p.proposingOrganization.id = :id")
List<Propose> findSentPropose(Long id);

@Query("SELECT p FROM Propose p WHERE p.proposedOrganization.id = :organizationId AND p.announcement.id = :announcementId")
List<Propose> findReceivedProposeWithAnnouncementId(Long organizationId, Long announcementId);
// @Query("SELECT p FROM Propose p WHERE p.proposingOrganization.id = :id")
// List<Propose> findSentPropose(Long id);
//
// @Query("SELECT p FROM Propose p WHERE p.proposedOrganization.id = :organizationId AND p.announcement.id = :announcementId")
// List<Propose> findReceivedProposeWithAnnouncementId(Long organizationId, Long announcementId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.sponus.sponusbe.domain.propose.entity.Propose;
import com.sponus.sponusbe.domain.propose.exception.ProposeErrorCode;
import com.sponus.sponusbe.domain.propose.exception.ProposeException;
import com.sponus.sponusbe.domain.propose.repository.ProposeCustomRepository;
import com.sponus.sponusbe.domain.propose.repository.ProposeRepository;

import lombok.RequiredArgsConstructor;
Expand All @@ -23,20 +24,22 @@ public class ProposeQueryService {

private final ProposeRepository proposeRepository;

private final ProposeCustomRepository proposeCustomRepository;

public List<ProposeSummaryGetResponse> getProposes(Organization organization, ProposeGetCondition condition) {
// TODO : 추후에 QueryDSL 이용
List<ProposeSummaryGetResponse> response;
if (condition.isSentPropose()) {
// 내가 보낸 제안은 그냥 반환
response = proposeRepository.findSentPropose(organization.getId()).stream()
response = proposeCustomRepository.findSentPropose(organization.getId()).stream()
.map(ProposeSummaryGetResponse::from)
.toList();
} else {
// 내가 받은 제안은 공고 id 별로 보여줘야하고, 공고 id가 없으면 안됨
if (condition.announcementId() == null) {
throw new ProposeException(ProposeErrorCode.ANNOUNCEMENT_ID_IS_REQUIRED);
}
response = proposeRepository.findReceivedProposeWithAnnouncementId(
response = proposeCustomRepository.findReceivedProposeWithAnnouncementId(
organization.getId(),
condition.announcementId())
.stream()
Expand Down

0 comments on commit d3e9f8a

Please sign in to comment.