-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 패키지 구조 개선 및 QueryDsl 적용 (#363)
* refactor: 조직 검색 리팩토링 * refactor: 불필요한 테스트 코드 삭제 * chore: ignore 추가
- Loading branch information
Showing
16 changed files
with
217 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
*-secret.yml | ||
**/src/main/generated/ | ||
**/out/ | ||
**/firebase/*.json | ||
data/** | ||
db/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
api/src/test/java/com/sponus/sponusbe/domain/organization/OrganizationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.sponus.sponusbe.domain.organization; | ||
|
||
import java.util.List; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import com.sponus.coredomain.domain.organization.Organization; | ||
import com.sponus.coredomain.domain.organization.enums.OrganizationType; | ||
import com.sponus.coredomain.domain.organization.enums.ProfileStatus; | ||
import com.sponus.sponusbe.domain.organization.dto.request.PageCondition; | ||
import com.sponus.sponusbe.domain.organization.dto.response.OrganizationSearchResponse; | ||
import com.sponus.sponusbe.domain.organization.dto.response.PageResponse; | ||
import com.sponus.sponusbe.domain.organization.service.OrganizationService; | ||
|
||
import jakarta.persistence.EntityManager; | ||
|
||
@SpringBootTest | ||
@Transactional | ||
@ActiveProfiles("test") | ||
class OrganizationServiceTest { | ||
|
||
@Autowired | ||
OrganizationService organizationService; | ||
|
||
@Autowired | ||
EntityManager em; | ||
|
||
@BeforeEach | ||
public void init() { | ||
for (int i = 1; i <= 5; i++) { | ||
Organization organization = Organization.builder() | ||
.email("sponus_company" + i + "@gmail.com") | ||
.name("sponus_company" + i) | ||
.password("sponus_company1234#") | ||
.organizationType(OrganizationType.COMPANY) | ||
.profileStatus(ProfileStatus.ACTIVE) | ||
.build(); | ||
|
||
em.persist(organization); | ||
} | ||
em.flush(); | ||
em.clear(); | ||
} | ||
|
||
@Test | ||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD) | ||
void searchV1() { | ||
// given | ||
PageCondition pageCondition = new PageCondition(0, 10); | ||
|
||
// when | ||
PageResponse<OrganizationSearchResponse> searchOrganizations = organizationService.searchOrganizations( | ||
pageCondition, "sponus", null); | ||
|
||
// then | ||
List<String> expectedOrganizationNames = List.of( | ||
"sponus_company1", | ||
"sponus_company2", | ||
"sponus_company3", | ||
"sponus_company4", | ||
"sponus_company5"); | ||
List<String> actualOrganizationNames = searchOrganizations.content().stream() | ||
.map(OrganizationSearchResponse::name) | ||
.toList(); | ||
|
||
Assertions.assertThat(actualOrganizationNames).containsExactlyInAnyOrderElementsOf(expectedOrganizationNames); | ||
} | ||
|
||
@Test | ||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD) | ||
void searchV2() { | ||
// given | ||
PageCondition pageCondition = new PageCondition(0, 10); | ||
|
||
// when | ||
PageResponse<OrganizationSearchResponse> searchOrganizations = organizationService.searchOrganizationsV2( | ||
pageCondition, "sponus", null); | ||
|
||
// then | ||
List<String> expectedOrganizationNames = List.of( | ||
"sponus_company1", | ||
"sponus_company2", | ||
"sponus_company3", | ||
"sponus_company4", | ||
"sponus_company5"); | ||
List<String> actualOrganizationNames = searchOrganizations.content().stream() | ||
.map(OrganizationSearchResponse::name) | ||
.toList(); | ||
|
||
Assertions.assertThat(actualOrganizationNames).containsExactlyInAnyOrderElementsOf(expectedOrganizationNames); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...va/com/sponus/coredomain/domain/organization/repository/OrganizationRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.sponus.coredomain.domain.organization.repository; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import com.sponus.coredomain.domain.organization.Organization; | ||
import com.sponus.coredomain.domain.organization.repository.conditions.OrganizationSearchCondition; | ||
|
||
public interface OrganizationRepositoryCustom { | ||
|
||
Page<Organization> searchOrganizationV2(OrganizationSearchCondition condition, Pageable pageable); | ||
} |
66 changes: 66 additions & 0 deletions
66
...om/sponus/coredomain/domain/organization/repository/OrganizationRepositoryCustomImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.sponus.coredomain.domain.organization.repository; | ||
|
||
import static com.sponus.coredomain.domain.organization.QOrganization.*; | ||
import static org.springframework.util.StringUtils.*; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.support.PageableExecutionUtils; | ||
|
||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.sponus.coredomain.domain.organization.Organization; | ||
import com.sponus.coredomain.domain.organization.enums.ProfileStatus; | ||
import com.sponus.coredomain.domain.organization.repository.conditions.OrganizationSearchCondition; | ||
|
||
import jakarta.persistence.EntityManager; | ||
|
||
public class OrganizationRepositoryCustomImpl implements OrganizationRepositoryCustom { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
public OrganizationRepositoryCustomImpl(EntityManager em) { | ||
this.queryFactory = new JPAQueryFactory(em); | ||
} | ||
|
||
@Override | ||
public Page<Organization> searchOrganizationV2(OrganizationSearchCondition condition, Pageable pageable) { | ||
|
||
List<Organization> content = queryFactory | ||
.selectFrom(organization) | ||
.where( | ||
keywordContains(condition.keyword()), | ||
organizationIdNotEq(condition.organizationId()), | ||
isActive() | ||
) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
|
||
long count = queryFactory | ||
.selectFrom(organization) | ||
.where( | ||
keywordContains(condition.keyword()), | ||
organizationIdNotEq(condition.organizationId()), | ||
isActive() | ||
) | ||
.fetch() | ||
.size(); | ||
|
||
return PageableExecutionUtils.getPage(content, pageable, () -> count); | ||
} | ||
|
||
private BooleanExpression keywordContains(String keyword) { | ||
return hasText(keyword) ? organization.name.containsIgnoreCase(keyword) : null; | ||
} | ||
|
||
private BooleanExpression organizationIdNotEq(Long organizationId) { | ||
return organizationId != null ? organization.id.ne(organizationId) : null; | ||
} | ||
|
||
private BooleanExpression isActive() { | ||
return organization.profileStatus.eq(ProfileStatus.ACTIVE); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...nus/coredomain/domain/organization/repository/conditions/OrganizationSearchCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.sponus.coredomain.domain.organization.repository.conditions; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record OrganizationSearchCondition( | ||
String keyword, | ||
Long organizationId | ||
) { | ||
public static OrganizationSearchCondition of(String keyword, Long organizationId) { | ||
return OrganizationSearchCondition.builder() | ||
.keyword(keyword) | ||
.organizationId(organizationId) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 0 additions & 41 deletions
41
core/core-infra-db/out/production/resources/application-db.yml
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.