Skip to content

Commit

Permalink
Merge pull request #292 from team9502/dev
Browse files Browse the repository at this point in the history
fix: 기업 전체조회 [배포]
  • Loading branch information
EUNCHAEv1006 authored Jul 2, 2024
2 parents 8988bf8 + b22b3ed commit 2ddbd06
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public class CompanyUser extends BaseTimeEntity implements CommonPoint {
private String cpPassword;

@Setter
@Column(nullable = true)
private Float averageRating;
@Column
private Float averageRating = 0.0f;

@Setter
private Integer reviewCount;
private Integer reviewCount = 0;

@Column(nullable = false)
private Integer viewCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -13,12 +15,12 @@
import team9502.sinchulgwinong.domain.scrap.entity.QCpUserScrap;

import java.util.List;
import java.util.Optional;

@Repository
public class CompanyUserRepositoryImpl implements CompanyUserRepositoryCustom {

private final JPAQueryFactory queryFactory;
private static final Logger logger = LoggerFactory.getLogger(CompanyUserRepositoryImpl.class);

public CompanyUserRepositoryImpl(EntityManager entityManager) {
this.queryFactory = new JPAQueryFactory(entityManager);
Expand Down Expand Up @@ -59,28 +61,31 @@ public Page<CompanyUser> findAllWithFilters(String sort, Float minRating, Float
case "createdAtDesc":
query.orderBy(companyUser.createdAt.desc());
break;
default:
query.orderBy(companyUser.cpUserId.asc()); // 기본 정렬 조건
}
} else {
query.orderBy(companyUser.cpUserId.asc()); // 기본 정렬 조건
}

// 총 개수 계산을 위한 쿼리 생성 및 실행, null 체크
Long countResult = queryFactory
.select(companyUser.count())
.from(companyUser)
.where(query.getMetadata().getWhere())
.fetchOne();
// 총 개수 계산을 위한 쿼리 생성 및 실행
long total = query.fetchCount();

long total = Optional.ofNullable(countResult).orElse(0L);
// 쿼리 디버깅을 위한 로그
logger.info("쿼리 실행 조건 - sort: {}, minRating: {}, maxRating: {}, pageable: {}, total: {}", sort, minRating, maxRating, pageable, total);

// 페이지 데이터 조회
List<CompanyUser> results = query
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

// 결과 디버깅을 위한 로그
logger.info("쿼리 결과 - results size: {}, pageable: {}", results.size(), pageable);

return new PageImpl<>(results, pageable, total);
}


@Override
public long countScrapsByCompanyUserId(Long cpUserId) {
QCpUserScrap scrap = QCpUserScrap.cpUserScrap;
Expand All @@ -93,5 +98,4 @@ public long countScrapsByCompanyUserId(Long cpUserId) {
// fetchOne() 결과가 null일 경우 0으로 대체
return count != null ? count : 0L;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package team9502.sinchulgwinong.domain.companyUser.service;

import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
Expand Down Expand Up @@ -36,7 +34,6 @@ public class CpUserService {
private final EmailVerificationService emailVerificationService;
private final PointService pointService;
private final JobBoardRepository jobBoardRepository;
private static final Logger logger = LoggerFactory.getLogger(CpUserService.class);

@Transactional
public CpUserProfileResponseDTO getCpUserProfile(Long cpUserId) {
Expand Down Expand Up @@ -126,11 +123,8 @@ public void updateCpUserPassword(Long cpUserId, CpUserPasswordUpdateRequestDTO r

@Transactional(readOnly = true)
public CpUserPageResponseDTO getAllCompanyUsers(String sort, Float minRating, Float maxRating, Pageable pageable) {

logger.info("기업회원 정렬 확인: {}, minRating: {}, maxRating: {}, pageable: {}", sort, minRating, maxRating, pageable);
Page<CompanyUser> companyUsers = companyUserRepository.findAllWithFilters(sort, minRating, maxRating, pageable);

logger.info("기업 회원 총합: {}", companyUsers.getTotalElements());
List<CpUserResponseDTO> content = companyUsers.getContent().stream()
.map(this::convertToDTO)
.collect(Collectors.toList());
Expand All @@ -145,15 +139,12 @@ public CpUserPageResponseDTO getAllCompanyUsers(String sort, Float minRating, Fl

@Transactional
public void usePointsForBanner(Long cpUserId) {

CompanyUser companyUser = companyUserRepository.findById(cpUserId)
.orElseThrow(() -> new ApiException(ErrorCode.COMPANY_USER_NOT_FOUND));

pointService.deductPoints(companyUser, UpType.BANNER);
}

private CpUserResponseDTO convertToDTO(CompanyUser companyUser) {

return new CpUserResponseDTO(
companyUser.getCpUserId(),
companyUser.getCpName(),
Expand All @@ -165,7 +156,6 @@ private CpUserResponseDTO convertToDTO(CompanyUser companyUser) {

@Transactional
public void deleteCpUser(Long cpUserId, CpUserDeleteRequestDTO requestDTO) {

CompanyUser companyUser = companyUserRepository.findById(cpUserId)
.orElseThrow(() -> new ApiException(ErrorCode.COMPANY_USER_NOT_FOUND));

Expand Down

0 comments on commit 2ddbd06

Please sign in to comment.