diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/domain/AlarmType.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/domain/AlarmType.java index 956e938f..55e38216 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/domain/AlarmType.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/domain/AlarmType.java @@ -1,6 +1,19 @@ package com.wakeUpTogetUp.togetUp.api.alarm.domain; +import com.wakeUpTogetUp.togetUp.common.Status; +import com.wakeUpTogetUp.togetUp.exception.BaseException; +import java.util.Arrays; + public enum AlarmType { PERSONAL, GROUP; + + public static AlarmType getByName(String name) { + return Arrays.stream(values()) + .filter(value -> value.name().equals(name)) + .findFirst() + .orElseThrow( + () -> new BaseException(Status.ALARM_TYPE_NOT_FOUND) + ); + } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepository.java index e255c5ee..d5afc710 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepository.java @@ -1,10 +1,14 @@ package com.wakeUpTogetUp.togetUp.api.alarm.repository; import com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.AlarmSimpleRes; +import com.wakeUpTogetUp.togetUp.api.alarm.domain.AlarmType; +import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm; import java.time.LocalDateTime; import java.util.List; public interface AlarmQueryRepository { List findAllUserTodayActiveAlarmsAfterNow(Integer userId, LocalDateTime now); + + List findUserAlarmsByType(Integer userId, AlarmType type); } \ No newline at end of file diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepositoryImpl.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepositoryImpl.java index d85640d4..d9b9ea48 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepositoryImpl.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmQueryRepositoryImpl.java @@ -8,7 +8,11 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.AlarmSimpleRes; import com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.QAlarmSimpleRes; +import com.wakeUpTogetUp.togetUp.api.alarm.domain.AlarmType; +import com.wakeUpTogetUp.togetUp.api.alarm.model.Alarm; +import com.wakeUpTogetUp.togetUp.common.Status; import com.wakeUpTogetUp.togetUp.common.annotation.LogExecutionTime; +import com.wakeUpTogetUp.togetUp.exception.BaseException; import java.time.DayOfWeek; import java.time.LocalDateTime; import java.time.LocalTime; @@ -27,7 +31,7 @@ public List findAllUserTodayActiveAlarmsAfterNow(Integer userId, .select( new QAlarmSimpleRes( alarm.id, - alarm.missionObject.icon, + missionObject.icon, alarm.alarmTime, alarm.name, missionObject.kr, @@ -35,9 +39,12 @@ public List findAllUserTodayActiveAlarmsAfterNow(Integer userId, ) ) .from(alarm) - .innerJoin(missionObject).on(alarm.missionObject.id.eq(missionObject.id)) + .join(alarm.missionObject, missionObject) .where( alarm.user.id.eq(userId) + .and(alarm.isDeleted.eq(false)) + .and(alarm.isActivated.eq(true)) + .and(alarmTimeGoe(now.toLocalTime())) .and(isDayOrOneTimeAlarm(now)) ) .orderBy( @@ -47,6 +54,19 @@ public List findAllUserTodayActiveAlarmsAfterNow(Integer userId, .fetch(); } + @Override + public List findUserAlarmsByType(Integer userId, AlarmType type) { + return query.selectFrom(alarm) + .where( + alarm.user.id.eq(userId), + alarm.isDeleted.eq(false), + getAlarmTypeCondition(type) + ) + .orderBy( + alarm.alarmTime.asc() + ) + .fetch(); + } private BooleanExpression alarmTimeGoe(LocalTime now) { return alarm.alarmTime.goe(now); @@ -55,8 +75,7 @@ private BooleanExpression alarmTimeGoe(LocalTime now) { @LogExecutionTime private BooleanExpression isDayOrOneTimeAlarm(LocalDateTime now) { return isDayAlarm(now.getDayOfWeek()) - .or(isOneTimeAlarm()) - .and(isActiveAlarm(now.toLocalTime())); + .or(isOneTimeAlarm()); } private BooleanExpression isDayAlarm(DayOfWeek dayOfWeek) { @@ -90,9 +109,14 @@ private BooleanExpression isOneTimeAlarm() { .and(alarm.sunday.eq(false)); } - private BooleanExpression isActiveAlarm(LocalTime now) { - return alarm.isActivated.eq(true) - .and(alarm.isDeleted.eq(false)) - .and(alarmTimeGoe(now)); + private BooleanExpression getAlarmTypeCondition(AlarmType type) { + switch (type) { + case PERSONAL: + return alarm.room.isNull(); + case GROUP: + return alarm.room.isNotNull(); + default: + throw new BaseException(Status.BAD_REQUEST_PARAM); + } } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmRepository.java index 56599373..a45a89d5 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/repository/AlarmRepository.java @@ -23,29 +23,20 @@ public interface AlarmRepository extends JpaRepository, AlarmQue @Query("SELECT a.id FROM Alarm a WHERE a.user.id = :userId") Set findUserAlarmIds(@Param("userId") Integer userId); - List findAllByUser_IdAndRoom_IdIsNullOrderByAlarmTime(Integer userId); - - @Query("SELECT a " - + "FROM Alarm a " - + "WHERE a.user.id = :userId " - + "AND a.room.id != null " - + "ORDER BY a.alarmTime") - List findRoomAlarmByUserId(@Param("userId") Integer userId); - @LogExecutionTime @Query("SELECT new com.wakeUpTogetUp.togetUp.api.alarm.controller.dto.response.AlarmSimpleRes(" + "a.id, " - + "a.missionObject.icon, " + + "mo.icon, " + "ml.createdAt, " + "a.name, " + "mo.kr, " + "a.room.id) " + "FROM Alarm a " - + "JOIN MissionObject mo ON mo.id = a.missionObject.id " - + "JOIN MissionLog ml ON a.id = ml.alarm.id " + + "INNER JOIN MissionLog ml ON a.id = ml.alarm.id " + + "INNER JOIN MissionObject mo ON mo.id = a.missionObject.id " + "WHERE a.user.id = :userId " + "AND ml.createdAt >= :baseTime " - + "ORDER BY a.alarmTime ASC, a.id ASC") + + "ORDER BY ml.createdAt ASC, a.id ASC") List findAllUserAlarmsWithTodayLog(@Param("userId") Integer userId, @Param("baseTime") LocalDateTime baseTime); @Query("SELECT a.missionObject FROM Alarm a WHERE a.room.id = :roomId ") diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/service/AlarmQueryService.java b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/service/AlarmQueryService.java index 30eeeb65..b2b7482b 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/service/AlarmQueryService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/api/alarm/service/AlarmQueryService.java @@ -39,17 +39,8 @@ public AlarmDetailRes getAlarmById(Integer alarmId) { public List getAlarmsByUserIdOrderByDate(Integer userId, String type) { userValidationService.validateUserExist(userId); - List alarms; - switch (AlarmType.valueOf(type)) { - case PERSONAL: - alarms = alarmRepository.findAllByUser_IdAndRoom_IdIsNullOrderByAlarmTime(userId); - break; - case GROUP: - alarms = alarmRepository.findRoomAlarmByUserId(userId); - break; - default: - throw new BaseException(Status.BAD_REQUEST_PARAM); - } + AlarmType alarmType = AlarmType.getByName(type); + List alarms = alarmRepository.findUserAlarmsByType(userId, alarmType); return EntityDtoMapper.INSTANCE.toAlarmDetailResList(alarms); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java b/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java index 2dbbc5ff..c206df76 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/common/Status.java @@ -50,6 +50,7 @@ public enum Status { INVALID_FILE_CONTENT_TYPE_EXCEPTION(HttpStatus.BAD_REQUEST, "이미지 컨텐트 타입이 유효하지 않습니다."), APP_VERSION_HIGHER_THAN_LATEST_EXCEPTION(HttpStatus.BAD_REQUEST, "데이터 내의 최신 버전보다 높은 버전입니다."), TIME_ZONE_ID_NOT_EXIST(HttpStatus.BAD_REQUEST, "유효하지 않은 시간대입니다."), + INVALID_ALARM_TYPE(HttpStatus.BAD_REQUEST, "알람 타입이 유효하지 않습니다."), // FORBIDDEN USER_AVATAR_LOCKED(HttpStatus.FORBIDDEN, "유저가 보유하지 않은 아바타 ID 입니다."), @@ -66,6 +67,7 @@ public enum Status { FILE_NOT_FOUND(HttpStatus.NOT_FOUND, "파일을 찾을 수 없습니다."), ACCOUNT_DOESNT_EXISTS(HttpStatus.NOT_FOUND, "계정이 존재하지 않습니다."), ROOM_USER_NOT_FOUND(HttpStatus.NOT_FOUND, "그룹의 해당 멤버가 없습니다."), + ALARM_TYPE_NOT_FOUND(HttpStatus.BAD_REQUEST, "존재하지 않는 알람 타입입니다."), // CONFLICT USER_AVATAR_ALREADY_EXIST(HttpStatus.CONFLICT, "이미 보유한 아바타 입니다."), diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/EntityDtoMapper.java b/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/EntityDtoMapper.java index cc718149..a0d75ce9 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/EntityDtoMapper.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/utils/mapper/EntityDtoMapper.java @@ -44,7 +44,7 @@ public interface EntityDtoMapper { GetMissionWithObjectListRes toGetMissionRes(Mission mission); - // GetMissionLogRes + // MissionLog @Mapping(target = "userId", source = "user.id") @Mapping(target = "roomId", source = "room.id") GetMissionLogRes toMissionLogRes(MissionLog missionLog);