Skip to content

Commit

Permalink
Merge pull request #294 from TEAM-MODDY/fix/#293
Browse files Browse the repository at this point in the history
#293 [fix] 디자이너 메인뷰 페이징 정렬, total 계산 오류 해결
  • Loading branch information
hellozo0 authored Nov 12, 2024
2 parents 1c25997 + f0f4fba commit 4f934fa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ output/
*.ipr
*.iws


# OSX
.DS_Store
.AppleDouble
Expand All @@ -37,3 +36,5 @@ output/
*.log-*.gz
logs/

#Token
/src/main/java/com/moddy/server/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
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.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

Expand All @@ -19,4 +22,7 @@ public interface HairModelApplicationJpaRepository extends JpaRepository<HairMod

List<HairModelApplication> findAllByModelId(Long modelId);

@Query("SELECT COUNT(*) FROM HairModelApplication h WHERE h.createdAt >= :startDate AND h.createdAt < :endDate")
long countNonExpiredApplications(@Param("startDate") LocalDateTime startDate, @Param("endDate") LocalDateTime endDate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -50,7 +52,6 @@ public DesignerMainResponse getDesignerMainInfo(final Long designerId, final int

Page<HairModelApplication> applicationPage = findApplicationsByPaging(page, size);
long totalElements = applicationPage.getTotalElements();

List<HairModelApplicationResponse> applicationResponsesList = applicationPage.stream().map(this::getApplicationResponse).collect(Collectors.toList());

return new DesignerMainResponse(
Expand Down Expand Up @@ -104,10 +105,11 @@ public ApplicationDto getApplicationDetailInfo(final Long applicationId) {
hairModelApplication.getExpiredDate().format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
}

public ApplicationIdResponse checkValidApplicationStatus(final Long modelId){
public ApplicationIdResponse checkValidApplicationStatus(final Long modelId) {
HairModelApplication hairModelApplication = hairModelApplicationJpaRepository.findFirstByModelIdOrderByCreatedAtDesc(modelId).orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_APPLICATION_EXCEPTION));

if(hairModelApplication.isExpired()) throw new NotFoundException(ErrorCode.NOT_FOUND_VALID_APPLICATION_EXCEPTION);
if (hairModelApplication.isExpired())
throw new NotFoundException(ErrorCode.NOT_FOUND_VALID_APPLICATION_EXCEPTION);

return new ApplicationIdResponse(hairModelApplication.getId());
}
Expand All @@ -128,22 +130,24 @@ public DownloadUrlResponseDto getApplicationCaptureDownloadUrl(final Long applic
return new DownloadUrlResponseDto(applicationDownloadUrl);
}

public boolean getApplicationExpiredStatus(final Long applicationId){
public boolean getApplicationExpiredStatus(final Long applicationId) {
final HairModelApplication hairModelApplication = hairModelApplicationJpaRepository.findById(applicationId)
.orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_APPLICATION_EXCEPTION));
return hairModelApplication.isExpired();
}

private Page<HairModelApplication> findApplicationsByPaging(final int page, final int size) {
PageRequest pageRequest = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "id"));
PageRequest pageRequest = PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<HairModelApplication> applicationPage = hairModelApplicationJpaRepository.findAll(pageRequest);

long nonExpiredCount = hairModelApplicationJpaRepository.countNonExpiredApplications(LocalDate.now().minusDays(13).atStartOfDay(), LocalDate.now().plusDays(1).atStartOfDay());

Page<HairModelApplication> nonExpiredApplications = applicationPage
.stream()
.filter(application -> !application.isExpired())
.collect(Collectors.collectingAndThen(
Collectors.toList(),
list -> new PageImpl<>(list, pageRequest, list.size())
list -> new PageImpl<>(list, pageRequest, nonExpiredCount) // 전체 개수 사용
));

return nonExpiredApplications;
Expand Down

0 comments on commit 4f934fa

Please sign in to comment.