Skip to content

Commit

Permalink
[Feature] - [Company] 기업 검색 로직 리팩토링했습니다.
Browse files Browse the repository at this point in the history
[Feature] - [Company] 기업 검색 로직 리팩토링했습니다.
  • Loading branch information
fakerdeft authored Apr 24, 2024
2 parents 22335db + 6a68504 commit 602940a
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</p>

## 🚀 기술 스택
![image](https://github.com/COFLLL/CoverFlow-BE/assets/98208452/4d2492f0-36dc-4c61-8078-c1a5907b84b9)
![image](https://github.com/fakerdeft/CoverFlow-BE/assets/98208452/cc5dffb8-d84e-48c6-869a-8adc5c2c5baf)

## 🚀 프로젝트 아키텍처
![image](https://github.com/COFLLL/.github/assets/98208452/bdd1c678-3eef-4af7-a75e-661069930261)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import com.coverflow.company.dto.request.UpdateCompanyRequest;
import com.coverflow.company.dto.response.FindAllCompaniesResponse;
import com.coverflow.company.dto.response.FindCompanyResponse;
import com.coverflow.company.dto.response.SearchCompanyCountResponse;
import com.coverflow.company.dto.response.SearchCompanyResponse;
import com.coverflow.company.infrastructure.CompanyRepository;
import com.coverflow.question.application.QuestionService;
import com.coverflow.question.dto.CompanyAndQuestionDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -43,19 +45,25 @@ public SearchCompanyResponse search(
final int pageNo,
final String name
) {
Page<Company> companies = companyRepository.findByNameStartingWithAndCompanyStatus(generatePageAsc(pageNo, NORMAL_PAGE_SIZE, "name"), name)
Slice<Company> companies = companyRepository.findByNameStartingWith(generatePageAsc(pageNo, NORMAL_PAGE_SIZE, "name"), name)
.orElseThrow(() -> new CompanyNotFoundException(name));

return SearchCompanyResponse.of(
companies.getTotalPages(),
companies.getTotalElements(),
return SearchCompanyResponse.from(
companies.getContent()
.stream()
.map(CompanyDTO::from)
.toList()
);
}

@Transactional(readOnly = true)
public SearchCompanyCountResponse search(final String name) {
long totalElements = companyRepository.countByName(name);
int totalPages = (int) (totalElements / NORMAL_PAGE_SIZE);

return SearchCompanyCountResponse.of(totalPages, totalElements);
}

/**
* [특정 기업과 질문 조회 메서드]
* 특정 기업과 질문 리스트를 조회하는 메서드
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.coverflow.company.dto.response;

public record SearchCompanyCountResponse(
int totalPages,
long totalElements
) {
public static SearchCompanyCountResponse of(
final int totalPages,
final long totalElements
) {
return new SearchCompanyCountResponse(totalPages, totalElements);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
import java.util.List;

public record SearchCompanyResponse(
int totalPages,
long totalElements,

List<CompanyDTO> companyList
) {

public static SearchCompanyResponse of(
final int totalPages,
final long totalElements,
final List<CompanyDTO> companyList
) {
return new SearchCompanyResponse(totalPages, totalElements, companyList);
public static SearchCompanyResponse from(final List<CompanyDTO> companyList) {
return new SearchCompanyResponse(companyList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import com.coverflow.company.dto.request.FindCompanyAdminRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;

import java.util.Optional;

public interface CompanyCustomRepository {

Optional<Slice<Company>> findByNameStartingWith(final Pageable pageable, final String name);

Long countByName(final String name);

Optional<Page<Company>> findWithFilters(final Pageable pageable, final FindCompanyAdminRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;

import java.util.List;
Expand All @@ -24,6 +22,39 @@ public class CompanyCustomRepositoryImpl implements CompanyCustomRepository {

private final JPAQueryFactory jpaQueryFactory;

@Override
public Optional<Slice<Company>> findByNameStartingWith(
final Pageable pageable,
final String name
) {
List<Company> companies = jpaQueryFactory
.selectFrom(company)
.where(
company.name.startsWith(name),
company.companyStatus.eq(CompanyStatus.valueOf("REGISTRATION"))
)
.orderBy(makeOrderSpecifiers(company, pageable))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();


return Optional.of(new SliceImpl<>(companies));
}

@Override
public Long countByName(
final String name
) {
return jpaQueryFactory
.select(company.count())
.from(company)
.where(
company.name.startsWith(name)
)
.fetchOne();
}

@Override
public Optional<Page<Company>> findWithFilters(
final Pageable pageable,
Expand All @@ -41,9 +72,9 @@ public Optional<Page<Company>> findWithFilters(
toContainsDistrict(request.district()),
eqCompanyStatus(request.companyStatus())
)
.orderBy(makeOrderSpecifiers(company, pageable))
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(makeOrderSpecifiers(company, pageable))
.fetch()
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.coverflow.company.infrastructure;

import com.coverflow.company.domain.Company;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -13,17 +11,6 @@

public interface CompanyRepository extends JpaRepository<Company, Long>, CompanyCustomRepository {

@Query("""
SELECT c
FROM Company c
WHERE c.name LIKE :name%
AND c.companyStatus = 'REGISTRATION'
""")
Optional<Page<Company>> findByNameStartingWithAndCompanyStatus(
final Pageable pageable,
@Param("name") final String name
);

Optional<Company> findByName(final String name);

@Query("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.coverflow.company.dto.request.UpdateCompanyRequest;
import com.coverflow.company.dto.response.FindAllCompaniesResponse;
import com.coverflow.company.dto.response.FindCompanyResponse;
import com.coverflow.company.dto.response.SearchCompanyCountResponse;
import com.coverflow.company.dto.response.SearchCompanyResponse;
import com.coverflow.global.annotation.AdminAuthorize;
import com.coverflow.global.handler.ResponseHandler;
Expand All @@ -28,7 +29,7 @@ public class CompanyController {
@GetMapping
public ResponseEntity<ResponseHandler<SearchCompanyResponse>> search(
@RequestParam @PositiveOrZero final int pageNo,
@RequestParam(defaultValue = "name") final String name
@RequestParam final String name
) {
return ResponseEntity.ok()
.body(ResponseHandler.<SearchCompanyResponse>builder()
Expand All @@ -38,6 +39,16 @@ public ResponseEntity<ResponseHandler<SearchCompanyResponse>> search(
);
}

@GetMapping("/count")
public ResponseEntity<ResponseHandler<SearchCompanyCountResponse>> search(@RequestParam final String name) {
return ResponseEntity.ok()
.body(ResponseHandler.<SearchCompanyCountResponse>builder()
.statusCode(HttpStatus.OK)
.data(companyService.search(name))
.build()
);
}

@GetMapping("/{companyId}")
public ResponseEntity<ResponseHandler<FindCompanyResponse>> findByCompanyId(
@PathVariable @Positive final long companyId,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/coverflow/global/util/PageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class PageUtil {
public static Pageable generatePageAsc(
final int pageNo,
final int pageSize,
final String criterion
final String... criterion
) {
return PageRequest.of(pageNo, pageSize, Sort.by(criterion).ascending());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.UUID;

import static com.coverflow.company.exception.CompanyException.CompanyNotFoundException;
import static com.coverflow.global.constant.Constant.*;
import static com.coverflow.global.constant.Constant.LARGE_PAGE_SIZE;
import static com.coverflow.global.constant.Constant.NORMAL_PAGE_SIZE;
import static com.coverflow.global.util.PageUtil.generatePageDesc;
import static com.coverflow.question.exception.QuestionException.QuestionNotFoundException;

Expand Down Expand Up @@ -64,7 +65,7 @@ public FindMyQuestionsResponse findByMemberId(
final String criterion,
final UUID memberId
) {
Page<Question> questionList = questionRepository.findRegisteredQuestions(generatePageDesc(pageNo, SMALL_PAGE_SIZE, criterion), memberId)
Page<Question> questionList = questionRepository.findRegisteredQuestions(generatePageDesc(pageNo, NORMAL_PAGE_SIZE, criterion), memberId)
.orElseThrow(() -> new QuestionNotFoundException(memberId));

return FindMyQuestionsResponse.of(
Expand Down

0 comments on commit 602940a

Please sign in to comment.