Skip to content
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 @@ -19,6 +19,11 @@ public interface AppHomeApi {
+ "열린피드백 작성 수, 사물함 배정 상태를 한 번에 조회한다. "
+ "대여와 신청한 행사는 미리보기 2건과 전체 수를 함께 준다. "
+ "신청 섹션은 운영진이 노출로 설정한 것 중 지금 신청할 수 있는 것만 담긴다 — "
+ "마감되거나 미게시된 대상은 누를 수 없는 '신청하기'가 되므로 뺀다.")
+ "마감되거나 미게시된 대상은 누를 수 없는 '신청하기'가 되므로 뺀다. "
+ "사물함 배정 상태는 신청이 시작된 가장 최근 회차 하나로 정한다 — 그 회차에 신청했으면 이용 기간 중 "
+ "ASSIGNED, 이용 기간이 끝나면 EXPIRED이고, 신청하지 않았으면 신청 기간 중 APPLICABLE, 그 밖에는 "
+ "NOT_APPLIED다. 회차가 없어도 NOT_APPLIED다. "
+ "단, 운영진이 신청 시작 전 회차에 미리 배정한 칸이 있으면 최근 회차가 ASSIGNED가 아닐 때 "
+ "그 칸으로 ASSIGNED를 준다.")
ApiResponse<HomeResponse> getHome(AppApiUser apiUser);
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package kr.ac.kookmin.stream.api.app.internal.home.response;

import kr.ac.kookmin.stream.event.domain.locker.domain.LockerApplicationStatus;
import kr.ac.kookmin.stream.event.domain.locker.domain.CurrentLockerStatus;

/**
* 홈에 보여줄 사물함 배정 상태.
* <p>
* 신청이 없으면 배정 자체가 없어 {@link LockerApplicationStatus}에 대응하는 값이 없다.
* 화면은 그 경우도 하나의 상태로 그리므로 {@link #NOT_APPLIED}를 여기서 만든다 — 신청이 없는 경우는
* 호출부가 먼저 거르므로 {@link #from}은 받지 않는다.
*/
public enum HomeLockerStatus {

NOT_APPLIED,
APPLICABLE,
ASSIGNED,
EXPIRED;

// switch 식이라 LockerApplicationStatus에 값이 늘면 컴파일이 막는다
public static HomeLockerStatus from(LockerApplicationStatus status) {
// switch 식이라 CurrentLockerStatus에 값이 늘면 컴파일이 막는다
public static HomeLockerStatus from(CurrentLockerStatus status) {
return switch (status) {
case NOT_APPLIED -> NOT_APPLIED;
case APPLICABLE -> APPLICABLE;
case ASSIGNED -> ASSIGNED;
case EXPIRED -> EXPIRED;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kr.ac.kookmin.stream.api.app.internal.home.usecase;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -17,8 +15,7 @@
import kr.ac.kookmin.stream.event.domain.event.domain.Event;
import kr.ac.kookmin.stream.event.domain.event.service.EventApplicationService;
import kr.ac.kookmin.stream.event.domain.event.service.EventService;
import kr.ac.kookmin.stream.event.domain.locker.domain.LockerApplicationResult;
import kr.ac.kookmin.stream.event.domain.locker.domain.LockerApplicationStatus;
import kr.ac.kookmin.stream.event.domain.locker.domain.CurrentLocker;
import kr.ac.kookmin.stream.event.domain.locker.domain.LockerPeriod;
import kr.ac.kookmin.stream.event.domain.locker.service.LockerApplicationService;
import kr.ac.kookmin.stream.event.domain.locker.service.LockerService;
Expand Down Expand Up @@ -64,19 +61,16 @@ public class AppHomeUseCase {
private final StudentTransferStatusService studentTransferStatusService;

public HomeResponse getHome(Long memberId) {
// 사물함 신청 내역은 배정 상태와 신청 섹션의 신청 여부가 함께 쓰므로 한 번만 읽는다
List<LockerApplicationResult> lockerApplications =
lockerApplicationService.getApplicationsByMemberId(memberId);
LocalDateTime now = LocalDateTime.now();

return new HomeResponse(
toBanners(),
toApplySections(memberId, lockerApplications),
toApplySections(memberId),
toRentals(memberId, now),
toAppliedEvents(memberId),
new HomeResponse.StudentFee(toFeeStatus(memberId)),
new HomeResponse.OpenFeedback(feedbackService.countByMemberId(memberId)),
toLocker(lockerApplications, now.toLocalDate())
toLocker(memberId, now)
);
}

Expand Down Expand Up @@ -142,10 +136,7 @@ private static List<Long> landingTargetIdsOf(List<HomeBanner> banners, HomeBanne
* 노출로 설정된 섹션 중 지금 신청할 수 있는 것만 설정 순서대로 남긴다. 마감되거나 미게시된 대상은
* 누를 수 없는 "신청하기"가 되므로 뺀다.
*/
private List<HomeResponse.ApplySection> toApplySections(
Long memberId,
List<LockerApplicationResult> lockerApplications
) {
private List<HomeResponse.ApplySection> toApplySections(Long memberId) {
List<HomeApplySection> sections = homeService.getExposedApplySections();
List<Long> eventIds = targetIdsOf(sections, HomeApplyTargetType.EVENT);
List<Long> periodIds = targetIdsOf(sections, HomeApplyTargetType.LOCKER);
Expand All @@ -156,9 +147,7 @@ private List<HomeResponse.ApplySection> toApplySections(
.collect(Collectors.toMap(LockerPeriod::getId, Function.identity()));

Set<Long> appliedEventIds = eventApplicationService.getAppliedEventIds(memberId, eventIds);
Set<Long> appliedPeriodIds = lockerApplications.stream()
.map(application -> application.period().getId())
.collect(Collectors.toSet());
Set<Long> appliedPeriodIds = lockerApplicationService.getAppliedPeriodIds(memberId, periodIds);

return sections.stream()
.map(section -> toApplySection(section, events, periods, appliedEventIds, appliedPeriodIds))
Expand Down Expand Up @@ -249,28 +238,10 @@ private HomeStudentFeeStatus toFeeStatus(Long memberId) {
.orElse(null));
}

/**
* 지금 쓰는 사물함이 있으면 그것을, 없으면 지난 배정이 있었는지만 알려준다.
* <p>
* 사용이 안 끝난 배정이 둘일 수 있다. 학생 신청은 신청 기간에만 생겨 회차가 겹치지 않지만, 운영진 배정
* ({@code AdminLockerServiceImpl.assignLocker})은 사용 종료만 보므로 다음 회차 칸을 미리 줄 수 있다.
* 그중 지금 쓰는 칸은 사용 시작이 가장 이른 것이다 — 신청 시각순으로 고르면 다음 학기 칸이 뜬다.
* <p>
* 배정·종료 판정은 "내 사물함" 화면과 같은 기준을 써야 하므로 {@link LockerApplicationStatus}에 맡긴다.
*/
private HomeResponse.Locker toLocker(List<LockerApplicationResult> applications, LocalDate today) {
if (applications.isEmpty()) {
return new HomeResponse.Locker(HomeLockerStatus.NOT_APPLIED, null);
}

return applications.stream()
.filter(application -> LockerApplicationStatus.from(application.period(), today)
== LockerApplicationStatus.ASSIGNED)
.min(Comparator.comparing(application -> application.period().getUsageStartAt()))
.map(application -> new HomeResponse.Locker(
HomeLockerStatus.from(LockerApplicationStatus.ASSIGNED),
application.locker().getLockerLabel()))
.orElseGet(() -> new HomeResponse.Locker(
HomeLockerStatus.from(LockerApplicationStatus.EXPIRED), null));
private HomeResponse.Locker toLocker(Long memberId, LocalDateTime now) {
CurrentLocker locker = lockerApplicationService.getCurrentLocker(memberId, now);
return new HomeResponse.Locker(
HomeLockerStatus.from(locker.status()),
locker.assignedLocker() == null ? null : locker.assignedLocker().getLockerLabel());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package kr.ac.kookmin.stream.event.domain.locker.domain;

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

/**
* @param assignedLocker {@link CurrentLockerStatus#ASSIGNED}일 때만 있다
*/
public record CurrentLocker(CurrentLockerStatus status, Locker assignedLocker) {

/**
* 회차는 겹치지 않아 최근 회차 신청이 시작됐으면 이전 회차 이용은 끝나 있으므로 최근 회차만 본다. 다만 운영진이 신청
* 시작 전 회차에 미리 배정한 칸은 알림·"내 사물함"이 이미 배정으로 보여 주므로 홈도 배정으로 본다.
*
* @param latestPeriod 없으면 null
*/
public static CurrentLocker of(LockerPeriod latestPeriod, List<LockerApplicationResult> applications,
LocalDateTime now) {
LocalDate today = now.toLocalDate();
Optional<LockerApplicationResult> latestApplication = applications.stream()
.filter(application -> latestPeriod != null && application.period().getId().equals(latestPeriod.getId()))
.findFirst();

Optional<LockerApplicationResult> assigned = latestApplication
.filter(application -> LockerApplicationStatus.from(application.period(), today)
== LockerApplicationStatus.ASSIGNED)
.or(() -> findPreAssigned(applications, now));
if (assigned.isPresent()) {
return new CurrentLocker(CurrentLockerStatus.ASSIGNED, assigned.get().locker());
}
if (latestApplication.isPresent()) {
return new CurrentLocker(CurrentLockerStatus.EXPIRED, null);
}
if (latestPeriod != null && latestPeriod.isApplyOpen(now)) {
return new CurrentLocker(CurrentLockerStatus.APPLICABLE, null);
}
return new CurrentLocker(CurrentLockerStatus.NOT_APPLIED, null);
}

private static Optional<LockerApplicationResult> findPreAssigned(List<LockerApplicationResult> applications,
LocalDateTime now) {
return applications.stream()
.filter(application -> application.period().calculateStatus(now) == LockerPeriodStatus.UPCOMING)
.min(Comparator.comparing(application -> application.period().getApplyStartAt()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kr.ac.kookmin.stream.event.domain.locker.domain;

public enum CurrentLockerStatus {

ASSIGNED,
EXPIRED,
APPLICABLE,
NOT_APPLIED
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public interface LockerApplicationRepository {
*/
List<LockerApplication> findByMemberId(Long memberId);

/**
* 주어진 운영 회차 중 회원이 신청한 회차 식별자. 취소된 신청은 뺀다.
*/
Set<Long> findAppliedPeriodIds(Long memberId, Collection<Long> lockerPeriodIds);

/**
* 신청 한 건. 취소된 신청도 돌려준다 — 운영진이 취소 여부를 보고 판단한다.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public interface LockerRepository {
*/
Optional<LockerPeriod> findCurrentPublishedPeriod(LocalDateTime at);

/** {@link #findCurrentPublishedPeriod}와 달리 신청이 끝난 회차도 잡고, 신청 시작 전인 회차는 뺀다. */
Optional<LockerPeriod> findLatestStartedPublishedPeriod(LocalDateTime at);

/**
* 게시되고 삭제되지 않은 운영 회차 중 신청 마감이 {@code at} 이후인 것. 순서는 보장하지 않는다.
* 신청이 끝난 회차는 다시 열리지 않으므로 여기서 자르고, 실제로 신청을 받는지는 도메인이 판정한다.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package kr.ac.kookmin.stream.event.domain.locker.service;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import kr.ac.kookmin.stream.event.domain.locker.domain.CurrentLocker;
import kr.ac.kookmin.stream.event.domain.locker.domain.LockerApplicationResult;
import kr.ac.kookmin.stream.event.domain.locker.domain.LockerApplyCommand;

Expand All @@ -16,4 +20,12 @@ public interface LockerApplicationService {
* 게시된 운영 회차에서 회원이 신청한 사물함 내역을 신청 일시 최신순으로 조회한다. 없으면 빈 목록이다.
*/
List<LockerApplicationResult> getApplicationsByMemberId(Long memberId);

/**
* 주어진 운영 회차 중 회원이 신청한 회차 식별자. 취소된 신청은 뺀다.
*/
Set<Long> getAppliedPeriodIds(Long memberId, Collection<Long> lockerPeriodIds);

/** 판정 기준은 {@link CurrentLocker#of}. */
CurrentLocker getCurrentLocker(Long memberId, LocalDateTime now);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package kr.ac.kookmin.stream.event.domain.locker.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import kr.ac.kookmin.stream.common.BusinessException;
import kr.ac.kookmin.stream.common.OutboxWriter;
import kr.ac.kookmin.stream.common.event.LockerAssigned;
import kr.ac.kookmin.stream.event.domain.locker.domain.CurrentLocker;
import kr.ac.kookmin.stream.event.domain.locker.domain.Locker;
import kr.ac.kookmin.stream.event.domain.locker.domain.LockerApplication;
import kr.ac.kookmin.stream.event.domain.locker.domain.LockerApplicationResult;
Expand Down Expand Up @@ -69,6 +72,19 @@ public List<LockerApplicationResult> getApplicationsByMemberId(Long memberId) {
.toList();
}

@Override
public Set<Long> getAppliedPeriodIds(Long memberId, Collection<Long> lockerPeriodIds) {
return lockerApplicationRepository.findAppliedPeriodIds(memberId, lockerPeriodIds);
}

@Override
public CurrentLocker getCurrentLocker(Long memberId, LocalDateTime now) {
return CurrentLocker.of(
lockerRepository.findLatestStartedPublishedPeriod(now).orElse(null),
getApplicationsByMemberId(memberId),
now);
}

/** 신청들이 속한 운영 회차 중 게시된 것. 식별자로 찾아 쓸 수 있게 묶는다. */
private Map<Long, LockerPeriod> getPublishedPeriods(List<LockerApplication> applications) {
List<Long> lockerPeriodIds = applications.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ public Optional<LockerPeriod> findCurrentPublishedPeriod(LocalDateTime at) {
throw new UnsupportedOperationException();
}

@Override
public Optional<LockerPeriod> findLatestStartedPublishedPeriod(LocalDateTime at) {
throw new UnsupportedOperationException();
}

@Override
public List<LockerPeriod> findPublishedPeriodsByIds(Collection<Long> lockerPeriodIds) {
batchLookups++;
Expand Down Expand Up @@ -514,6 +519,11 @@ public List<LockerApplication> findByMemberId(Long memberId) {
return memberApplications.stream().filter(value -> value.getMemberId().equals(memberId)).toList();
}

@Override
public Set<Long> findAppliedPeriodIds(Long memberId, Collection<Long> lockerPeriodIds) {
throw new UnsupportedOperationException();
}

// 아래는 구역·배치 조회(LockerService)용 메서드라 이 테스트에서는 쓰지 않는다

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ public Optional<LockerPeriod> findCurrentPublishedPeriod(LocalDateTime at) {
throw new UnsupportedOperationException();
}

@Override
public Optional<LockerPeriod> findLatestStartedPublishedPeriod(LocalDateTime at) {
throw new UnsupportedOperationException();
}

// 아래는 회원별 신청 내역 조회용 메서드라 이 테스트에서는 쓰지 않는다

@Override
Expand Down Expand Up @@ -425,5 +430,10 @@ public long countAppliedByPeriod(Long lockerPeriodId) {
public List<LockerApplication> findByMemberId(Long memberId) {
throw new UnsupportedOperationException();
}

@Override
public Set<Long> findAppliedPeriodIds(Long memberId, Collection<Long> lockerPeriodIds) {
throw new UnsupportedOperationException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public interface LockerApplicationJpaRepository extends JpaRepository<LockerAppl

List<LockerApplicationJpaEntity> findAllByMemberIdAndIsCanceledFalseOrderByAppliedAtDescIdDesc(Long memberId);

@Query("""
SELECT a.lockerPeriodId FROM LockerApplicationJpaEntity a
WHERE a.memberId = :memberId AND a.lockerPeriodId IN :lockerPeriodIds AND a.isCanceled = false
""")
List<Long> findLockerPeriodIdsByMemberId(
@Param("memberId") Long memberId,
@Param("lockerPeriodIds") Collection<Long> lockerPeriodIds
);

/**
* 해당 운영 회차에 신청된 사물함 식별자. 신청 여부만 필요하므로 신청 자체는 읽지 않는다.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ public List<LockerApplication> findByMemberId(Long memberId) {
.toList();
}

@Override
public Set<Long> findAppliedPeriodIds(Long memberId, Collection<Long> lockerPeriodIds) {
if (lockerPeriodIds.isEmpty()) {
return Set.of();
}
return Set.copyOf(lockerApplicationJpaRepository.findLockerPeriodIdsByMemberId(memberId, lockerPeriodIds));
}

@Override
public Optional<LockerApplication> findById(Long lockerApplicationId) {
return lockerApplicationJpaRepository.findById(lockerApplicationId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ public interface LockerPeriodJpaRepository extends JpaRepository<LockerPeriodJpa
ORDER BY p.applyStartAt ASC, p.id ASC
""")
List<LockerPeriodJpaEntity> findCurrentPublished(@Param("at") LocalDateTime at, Pageable pageable);

@Query("""
SELECT p FROM LockerPeriodJpaEntity p
WHERE p.isPublished = true
AND p.isDeleted = false
AND p.applyStartAt <= :at
ORDER BY p.applyStartAt DESC, p.id DESC
""")
List<LockerPeriodJpaEntity> findLatestStartedPublished(@Param("at") LocalDateTime at, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ public Optional<LockerPeriod> findCurrentPublishedPeriod(LocalDateTime at) {
.map(LockerPeriodJpaEntity::toDomain);
}

@Override
public Optional<LockerPeriod> findLatestStartedPublishedPeriod(LocalDateTime at) {
return lockerPeriodJpaRepository.findLatestStartedPublished(at, Pageable.ofSize(1)).stream()
.findFirst()
.map(LockerPeriodJpaEntity::toDomain);
}

@Override
public List<LockerPeriod> findPublishedPeriodsByIds(Collection<Long> lockerPeriodIds) {
if (lockerPeriodIds.isEmpty()) {
Expand Down