Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor : 마감순 정렬 로직 수정(#128) #129

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface JobDescriptionRepository {

fun findAllByUserIdAndSortByCreatedAt(userId: UUID, page: Int, size: Int, writeStatus: WriteStatus?): PageDomain<JobDescription>

fun findAllByUserIdAndSortByEndedAt(userId: UUID, page: Int, size: Int, writeStatus: WriteStatus?): PageDomain<JobDescription>

fun findAllByUserId(userId: UUID, page: Int, size: Int, writeStatus: WriteStatus?): PageDomain<JobDescription>

fun deleteById(jobDescriptionId: UUID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.bamyanggang.domainmodule.domain.jobDescription.aggregate.JobDescripti
import com.bamyanggang.domainmodule.domain.jobDescription.enums.SortType
import com.bamyanggang.domainmodule.domain.jobDescription.enums.WriteStatus
import com.bamyanggang.domainmodule.domain.jobDescription.repository.JobDescriptionRepository
import java.time.LocalDateTime
import java.time.temporal.ChronoUnit
import java.util.*

class JobDescriptionReader(
Expand All @@ -13,11 +15,24 @@ class JobDescriptionReader(
fun readJobDescriptionByUserIdAndSortType(userId: UUID, page: Int, size: Int, sortType: SortType?, writeStatus: WriteStatus?): PageDomain<JobDescription> {
return when(sortType) {
SortType.CREATED -> jobDescriptionRepository.findAllByUserIdAndSortByCreatedAt(userId, page, size, writeStatus)
SortType.ENDED -> jobDescriptionRepository.findAllByUserIdAndSortByEndedAt(userId, page, size, writeStatus)
SortType.ENDED -> sortWithEndedAt(jobDescriptionRepository.findAllByUserId(userId, page, size, writeStatus))
else -> jobDescriptionRepository.findAllByUserId(userId, page, size, writeStatus)
}
}

private fun sortWithEndedAt (pageDomain: PageDomain<JobDescription>): PageDomain<JobDescription> {
val now = LocalDateTime.now()
val futureList = pageDomain.content.filter { it.endedAt.isAfter(now) }
val pastList = pageDomain.content.filter { it.endedAt.isBefore(now) }

val sortedFutureList = futureList.sortedWith(compareBy { kotlin.math.abs(ChronoUnit.DAYS.between(it.endedAt, now)) })
val sortedPastList = pastList.sortedWith(compareByDescending { it.endedAt })

val sortedList = sortedFutureList + sortedPastList

return PageDomain(sortedList, pageDomain.pageNumber, pageDomain.pageSize, pageDomain.totalPage, pageDomain.hasNext)
}

fun readJobDescriptionById(jobDescriptionId: UUID): JobDescription {
return jobDescriptionRepository.findById(jobDescriptionId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void save(JobDescription jobDescription) {
jobDescriptionJpaRepository.save(jobDescriptionJpaEntity);
}

@Override
public JobDescription findById(UUID jobDescriptionId) {
return jobDescriptionJpaRepository.findById(jobDescriptionId)
.map(jobDescriptionMapper::toDomainEntity)
Expand All @@ -53,16 +54,6 @@ public PageDomain<JobDescription> findAllByUserId(UUID userId, int page, int siz
,jobDescriptionSlice.getTotalPages(), jobDescriptionSlice.hasNext());
}

@Override
public PageDomain<JobDescription> findAllByUserIdAndSortByEndedAt(UUID userId, int page, int size, WriteStatus writeStatus) {
Pageable pageable = PageRequest.of(page, size);
LocalDateTime now = LocalDateTime.now();
Page<JobDescriptionJpaEntity> jobDescriptionJpaEntities = jobDescriptionJpaRepository.findAllByUserIdAndWriteStatusAndTime(userId, writeStatus, now, pageable);
Page<JobDescription> jobDescriptionSlice = jobDescriptionJpaEntities.map(jobDescriptionMapper::toDomainEntity);
return new PageDomain<>(jobDescriptionSlice.getContent(), jobDescriptionSlice.getNumber(), jobDescriptionSlice.getSize()
,jobDescriptionSlice.getTotalPages(), jobDescriptionSlice.hasNext());
}

@Override
public void deleteById(UUID jobDescriptionId) {
jobDescriptionJpaRepository.deleteById(jobDescriptionId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public interface JobDescriptionJpaRepository extends JpaRepository<JobDescriptio
@Query("SELECT j FROM JobDescriptionJpaEntity j WHERE j.userId = :userId AND (:writeStatus is null or j.writeStatus = :writeStatus)")
Page<JobDescriptionJpaEntity> findAllByUserIdAndWriteStatus(@Param("userId") UUID userId, @Param("writeStatus") WriteStatus writeStatus, @Param("date") Pageable pageable);

@Query("SELECT j FROM JobDescriptionJpaEntity j WHERE j.userId = :userId AND (:writeStatus is null or j.writeStatus = :writeStatus) AND j.endedAt > :date ORDER BY j.endedAt ASC")
Page<JobDescriptionJpaEntity> findAllByUserIdAndWriteStatusAndTime(@Param("userId") UUID userId, @Param("writeStatus") WriteStatus writeStatus,
@Param("date") LocalDateTime now, Pageable pageable);
@Modifying
@Query("UPDATE JobDescriptionJpaEntity j SET j.writeStatus = 'CLOSED' WHERE j.endedAt < CURRENT_TIMESTAMP")
void changeWriteStatus();
Expand Down
Loading