diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmController.java b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmController.java index 284bac21..1f6c7e98 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmController.java @@ -55,6 +55,7 @@ public BaseResponse createAlarm( @RequestBody @Valid AlarmReq alarmReq ){ //TODO : jwt 정보와 일치하는지 확인하기 + Integer createdAlarmId = alarmService.createAlarm(userId, alarmReq); return new BaseResponse(ResponseStatus.SUCCESS, createdAlarmId); @@ -80,5 +81,21 @@ public BaseResponse updateAlarm( return new BaseResponse<>(ResponseStatus.SUCCESS, patchAlarmRes); } -// 알람 삭제 + /** + * 알람 삭제 + * @param userId + * @param alarmId + * @return + */ + @DeleteMapping("{userId}/alarms/{alarmId}") + public BaseResponse deleteAlarm( + @PathVariable @Valid Integer userId, + @PathVariable @Valid Integer alarmId + ) { + // TODO : JWT + + alarmService.deleteAlarm(alarmId); + + return new BaseResponse(ResponseStatus.SUCCESS); + } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmProvider.java b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmProvider.java index 314e0cfe..98bce12d 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmProvider.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmProvider.java @@ -8,6 +8,8 @@ import com.wakeUpTogetUp.togetUp.routines.RoutineRepository; import com.wakeUpTogetUp.togetUp.routines.dto.response.RoutineRes; import com.wakeUpTogetUp.togetUp.routines.model.Routine; +import com.wakeUpTogetUp.togetUp.users.UserRepository; +import com.wakeUpTogetUp.togetUp.users.model.User; import com.wakeUpTogetUp.togetUp.utils.mappers.AlarmMapper; import com.wakeUpTogetUp.togetUp.utils.mappers.RoutineMapper; import lombok.RequiredArgsConstructor; @@ -19,6 +21,7 @@ @Service @RequiredArgsConstructor public class AlarmProvider { + private final UserRepository userRepository; private final AlarmRepository alarmRepository; private final RoutineRepository routineRepository; @@ -28,10 +31,12 @@ public class AlarmProvider { * @return */ public List getAlarmsByUserId(Integer userId) { - List alarmList = alarmRepository.findByUserId(userId) - .orElseThrow( - () -> new BaseException(ResponseStatus.ALARM_NOT_FOUND) - ); + // 유저 id 유무 확인 + User user = userRepository.findById(userId).orElseThrow( + () -> new BaseException(ResponseStatus.INVALID_USER_ID) + ); + + List alarmList = alarmRepository.findByUserId(userId); // dto 매핑 ArrayList alarmsResList = new ArrayList<>(); @@ -73,7 +78,7 @@ public List getRoutineResByAlarmId(Integer alarmId){ ArrayList routineResList = new ArrayList<>(); for(Routine routine : routineList) { - routineResList.add(RoutineMapper.INSTANCE.entityToGetRoutineRes(routine)); + routineResList.add(RoutineMapper.INSTANCE.toRoutineRes(routine)); } return routineResList; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmRepository.java index b4cdeb8a..63ba7332 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmRepository.java @@ -13,10 +13,9 @@ public interface AlarmRepository extends JpaRepository { @Override S save(S entity); - @Override Optional findById(Integer id); @Query("select a from Alarm a where a.user.id = :userId") - Optional> findByUserId(@Param(value = "userId") Integer userId); + List findByUserId(@Param(value = "userId") Integer userId); } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmService.java b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmService.java index 17e5fe73..4e77e536 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmService.java @@ -28,12 +28,7 @@ public class AlarmService { private final UserRepository userRepository; private final MappingAlarmRoutineRepository mappingAlarmRoutineRepository; - /** - * 알람 생성 - * @param userId - * @param postAlarmReq - * @return - */ + // 알람 생성 @Transactional public int createAlarm(Integer userId, AlarmReq postAlarmReq) { User user = userRepository.findById(userId) @@ -73,13 +68,7 @@ public int createAlarm(Integer userId, AlarmReq postAlarmReq) { return alarm.getId(); } - /** - * 알람 수정 - * @param userId - * @param alarmId - * @param patchAlarmReq - * @return - */ + // 알람 수정 @Transactional public AlarmRes updateAlarm(Integer userId, Integer alarmId, AlarmReq patchAlarmReq) { // 추가 값 설정 @@ -129,11 +118,7 @@ public AlarmRes updateAlarm(Integer userId, Integer alarmId, AlarmReq patchAlarm return alarmRes; } - /** - * 매핑 알람 루틴 리스트 생성하기 - * @param alarmReq - * @param alarm - */ + // 매핑 알람 루틴 리스트 생성하기 protected void createMappingAlarmRoutineList(AlarmReq alarmReq, Alarm alarm){ int i=1; @@ -154,4 +139,9 @@ protected void createMappingAlarmRoutineList(AlarmReq alarmReq, Alarm alarm){ i++; } } + + // 알람 삭제 + public void deleteAlarm(Integer alarmId) { + alarmRepository.deleteById(alarmId); + } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/dto/request/AlarmReq.java b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/dto/request/AlarmReq.java index 70138bf3..23ff61b5 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/dto/request/AlarmReq.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/dto/request/AlarmReq.java @@ -2,8 +2,8 @@ import lombok.*; -import javax.persistence.Access; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import javax.validation.constraints.Null; import java.util.List; @@ -36,6 +36,7 @@ public AlarmReq(String name, String icon, String sound, Integer volume, Boolean @Null private Integer id; + @NotNull(message = "사용자 아이디를 넣어주세요.") private Integer userId; @NotBlank(message = "알람 이름은 공백일 수 없습니다.") private String name; @@ -46,7 +47,9 @@ public AlarmReq(String name, String icon, String sound, Integer volume, Boolean private Boolean isRoutineOn; private Integer snoozeInterval; private Integer snoozeCnt; + @NotNull(message = "알람 시작시는 공백일 수 없습니다.") private Integer startHour; + @NotNull(message = "알람 시작분은 공백일 수 없습니다.") private Integer startMinute; private Boolean monday; private Boolean tuesday; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/model/Alarm.java b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/model/Alarm.java index beaa5f81..c0ea3264 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/alarms/model/Alarm.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/alarms/model/Alarm.java @@ -41,23 +41,23 @@ private Alarm(User user, String name, String icon, String sound, Integer volume, // TODO : 접근제어자를 public으로 두는게 맞나? public void modifyProperties(String name, String icon, String sound, Integer volume, Boolean isVibrate, Boolean isRoutineOn, Integer snoozeInterval, Integer snoozeCnt, Integer startHour, Integer startMinute, Boolean monday, Boolean tuesday, Boolean wednesday, Boolean thursday, Boolean friday, Boolean saturday, Boolean sunday) { - this.name = name; - this.icon = icon; - this.sound = sound; - this.volume = volume; - this.isVibrate = isVibrate; - this.isRoutineOn = isRoutineOn; - this.snoozeInterval = snoozeInterval; - this.snoozeCnt = snoozeCnt; - this.startHour = startHour; - this.startMinute = startMinute; - this.monday = monday; - this.tuesday = tuesday; - this.wednesday = wednesday; - this.thursday = thursday; - this.friday = friday; - this.saturday = saturday; - this.sunday = sunday; + setName(name); + setIcon(icon); + setSound(sound); + setVolume(volume); + setIsVibrate(isVibrate); + setIsRoutineOn(isRoutineOn); + setSnoozeInterval(snoozeInterval); + setSnoozeCnt(snoozeCnt); + setStartHour(startHour); + setId(startMinute); + setMonday(monday); + setTuesday(tuesday); + setWednesday(wednesday); + setThursday(thursday); + setFriday(friday); + setSaturday(saturday); + setSunday(sunday); } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "userId") diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineController.java b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineController.java index 4e3c1b1a..0694c306 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineController.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineController.java @@ -17,7 +17,12 @@ public class RoutineController { private final RoutineService routineService; private final RoutineProvider routineProvider; - // 루틴 1개 가져오기 + /** + * 루틴 1개 가져오기 + * @param userId + * @param routineId + * @return + */ @GetMapping("{userId}/routines/{routineId}") public BaseResponse getRoutine( @PathVariable("userId") Integer userId, @@ -28,7 +33,11 @@ public BaseResponse getRoutine( return new BaseResponse<>(ResponseStatus.SUCCESS, routineRes); } - // 루틴 목록 가져오기 + /** + * 루틴 목록 가져오기 + * @param userId + * @return + */ @GetMapping("{userId}/routines") public BaseResponse> getRoutinesByUserId(@PathVariable Integer userId) { List routineResList = routineProvider.getRoutinesByUserId(userId); @@ -36,22 +45,55 @@ public BaseResponse> getRoutinesByUserId(@PathVariable Integer return new BaseResponse<>(ResponseStatus.SUCCESS, routineResList); } - // 루틴 생성 + /** + * 루틴 생성 + * @param userId + * @param routineReq + * @return + */ @PostMapping("{userId}/routines") public BaseResponse createRoutine( @PathVariable("userId") Integer userId, @RequestBody @Valid RoutineReq routineReq ) { + // TODO : JWT + Integer createdRoutineId = routineService.createRoutine(userId, routineReq); return new BaseResponse(ResponseStatus.SUCCESS, createdRoutineId); } // 루틴 수정 + @PatchMapping("{userId}/routines/{routineId}") + public BaseResponse updateRoutine( + @PathVariable Integer userId, + @PathVariable Integer routineId, + @RequestBody @Valid RoutineReq patchRoutineReq + ) { + // TODO : JWT + RoutineRes patchRoutineRes = routineService.updateRoutine(routineId, patchRoutineReq); - // 루틴 삭제 - + return new BaseResponse<>(ResponseStatus.SUCCESS, patchRoutineRes); + } + + + /** + * 루틴 삭제 + * @param userId + * @param routineId + * @return + */ + @DeleteMapping("{userId}/routines/{routineId}") + public BaseResponse deleteAlarm( + @PathVariable @Valid Integer userId, + @PathVariable @Valid Integer routineId + ) { + // TODO : JWT + routineService.deleteRoutine(routineId); + + return new BaseResponse<>(ResponseStatus.SUCCESS); + } //test @GetMapping("/test") diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineProvider.java b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineProvider.java index 046a5ae5..19ffc218 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineProvider.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineProvider.java @@ -21,7 +21,7 @@ public RoutineRes getRoutine(Integer routineId) { () -> new BaseException(ResponseStatus.INVALID_ROUTINE_ID) ); - RoutineRes routineRes = RoutineMapper.INSTANCE.toGetRoutineRes(routine); + RoutineRes routineRes = RoutineMapper.INSTANCE.toRoutineRes(routine); return routineRes; } @@ -34,7 +34,7 @@ public List getRoutinesByUserId(Integer userId) { ArrayList routineResList = new ArrayList<>(); for(Routine routine : routineList) { - routineResList.add(RoutineMapper.INSTANCE.toGetRoutineRes(routine)); + routineResList.add(RoutineMapper.INSTANCE.toRoutineRes(routine)); } return routineResList; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineRepository.java b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineRepository.java index fde0041e..795b8baa 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineRepository.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineRepository.java @@ -11,6 +11,9 @@ @Repository public interface RoutineRepository extends JpaRepository { + @Override + S save(S entity); + @Override Optional findById(Integer id); diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineService.java b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineService.java index 65173dd3..97994402 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineService.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/routines/RoutineService.java @@ -5,9 +5,11 @@ import com.wakeUpTogetUp.togetUp.missions.MissionRepository; import com.wakeUpTogetUp.togetUp.missions.model.Mission; import com.wakeUpTogetUp.togetUp.routines.dto.request.RoutineReq; +import com.wakeUpTogetUp.togetUp.routines.dto.response.RoutineRes; import com.wakeUpTogetUp.togetUp.routines.model.Routine; import com.wakeUpTogetUp.togetUp.users.UserRepository; import com.wakeUpTogetUp.togetUp.users.model.User; +import com.wakeUpTogetUp.togetUp.utils.mappers.RoutineMapper; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -44,4 +46,34 @@ public int createRoutine(Integer userId, RoutineReq routineReq) { return routine.getId(); } + + // 루틴 수정 + public RoutineRes updateRoutine(Integer routineId, RoutineReq patchRoutineReq) { + patchRoutineReq.setId(routineId); + + Routine routine = routineRepository.findById(routineId).orElseThrow( + () -> new BaseException(ResponseStatus.INVALID_ROUTINE_ID) + ); + + routine.modifyProperties( + missionRepository.findById(patchRoutineReq.getMissionId()).orElseThrow( + () -> new BaseException(ResponseStatus.INVALID_MISSION_ID) + ), + patchRoutineReq.getName(), + patchRoutineReq.getEstimatedTime(), + patchRoutineReq.getIcon(), + patchRoutineReq.getColor() + ); + + Routine routineModified = routineRepository.save(routine); + + RoutineRes patchRoutineRes = RoutineMapper.INSTANCE.toRoutineRes(routineModified); + + return patchRoutineRes; + } + + // 루틴 삭제 + public void deleteRoutine(Integer routineId) { + routineRepository.deleteById(routineId); + } } diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/routines/dto/request/RoutineReq.java b/src/main/java/com/wakeUpTogetUp/togetUp/routines/dto/request/RoutineReq.java index 60509be5..f7b9edcf 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/routines/dto/request/RoutineReq.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/routines/dto/request/RoutineReq.java @@ -4,6 +4,8 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import javax.validation.constraints.Null; @Getter @@ -20,9 +22,13 @@ public RoutineReq(Integer userId, Integer missionId, String name, Integer estima } @Null private Integer id; + @NotNull(message = "userId는 null일 수 없습니다.") private Integer userId; + @NotNull(message = "missionId는 null일 수 없습니다.") private Integer missionId; + @NotBlank(message = "routine의 name은 공백일 수 없습니다.") private String name; + @NotNull(message = "estimatedTime은 null일 수 없습니다.") private Integer estimatedTime; private String icon; private String color; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/routines/model/Routine.java b/src/main/java/com/wakeUpTogetUp/togetUp/routines/model/Routine.java index 768ae181..d734b6da 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/routines/model/Routine.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/routines/model/Routine.java @@ -25,6 +25,14 @@ public Routine(User user, Mission mission, String name, int estimatedTime, Strin this.color = color; } + public void modifyProperties(Mission mission, String name, int estimatedTime, String icon, String color) { + setMission(mission); + setName(name); + setEstimatedTime(estimatedTime); + setIcon(icon); + setColor(color); + } + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; diff --git a/src/main/java/com/wakeUpTogetUp/togetUp/utils/mappers/RoutineMapper.java b/src/main/java/com/wakeUpTogetUp/togetUp/utils/mappers/RoutineMapper.java index 923c2dce..7cc9f030 100644 --- a/src/main/java/com/wakeUpTogetUp/togetUp/utils/mappers/RoutineMapper.java +++ b/src/main/java/com/wakeUpTogetUp/togetUp/utils/mappers/RoutineMapper.java @@ -12,5 +12,5 @@ public interface RoutineMapper { @Mapping(target = "userId", expression = "java(routine.getUser().getId())") @Mapping(target = "missionId", expression = "java(routine.getMission().getId())") - RoutineRes toGetRoutineRes(Routine routine); + RoutineRes toRoutineRes(Routine routine); }