Skip to content

Commit

Permalink
Feature : alarm (#10)
Browse files Browse the repository at this point in the history
- Alarm, Routine 도메인 완성
  • Loading branch information
hye-on authored May 10, 2023
2 parents 7f320ac + 331da71 commit 9fc166e
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -80,5 +81,21 @@ public BaseResponse<AlarmRes> updateAlarm(
return new BaseResponse<>(ResponseStatus.SUCCESS, patchAlarmRes);
}

// 알람 삭제
/**
* 알람 삭제
* @param userId
* @param alarmId
* @return
*/
@DeleteMapping("{userId}/alarms/{alarmId}")
public BaseResponse<Integer> deleteAlarm(
@PathVariable @Valid Integer userId,
@PathVariable @Valid Integer alarmId
) {
// TODO : JWT

alarmService.deleteAlarm(alarmId);

return new BaseResponse<Integer>(ResponseStatus.SUCCESS);
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +21,7 @@
@Service
@RequiredArgsConstructor
public class AlarmProvider {
private final UserRepository userRepository;
private final AlarmRepository alarmRepository;
private final RoutineRepository routineRepository;

Expand All @@ -28,10 +31,12 @@ public class AlarmProvider {
* @return
*/
public List<AlarmsRes> getAlarmsByUserId(Integer userId) {
List<Alarm> 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<Alarm> alarmList = alarmRepository.findByUserId(userId);

// dto 매핑
ArrayList<AlarmsRes> alarmsResList = new ArrayList<>();
Expand Down Expand Up @@ -73,7 +78,7 @@ public List<RoutineRes> getRoutineResByAlarmId(Integer alarmId){

ArrayList<RoutineRes> routineResList = new ArrayList<>();
for(Routine routine : routineList) {
routineResList.add(RoutineMapper.INSTANCE.entityToGetRoutineRes(routine));
routineResList.add(RoutineMapper.INSTANCE.toRoutineRes(routine));
}

return routineResList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
public interface AlarmRepository extends JpaRepository<Alarm, Integer> {
@Override
<S extends Alarm> S save(S entity);

@Override
Optional<Alarm> findById(Integer id);

@Query("select a from Alarm a where a.user.id = :userId")
Optional<List<Alarm>> findByUserId(@Param(value = "userId") Integer userId);
List<Alarm> findByUserId(@Param(value = "userId") Integer userId);
}
26 changes: 8 additions & 18 deletions src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
// 추가 값 설정
Expand Down Expand Up @@ -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;

Expand All @@ -154,4 +139,9 @@ protected void createMappingAlarmRoutineList(AlarmReq alarmReq, Alarm alarm){
i++;
}
}

// 알람 삭제
public void deleteAlarm(Integer alarmId) {
alarmRepository.deleteById(alarmId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/wakeUpTogetUp/togetUp/alarms/model/Alarm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoutineRes> getRoutine(
@PathVariable("userId") Integer userId,
Expand All @@ -28,30 +33,67 @@ public BaseResponse<RoutineRes> getRoutine(
return new BaseResponse<>(ResponseStatus.SUCCESS, routineRes);
}

// 루틴 목록 가져오기
/**
* 루틴 목록 가져오기
* @param userId
* @return
*/
@GetMapping("{userId}/routines")
public BaseResponse<List<RoutineRes>> getRoutinesByUserId(@PathVariable Integer userId) {
List<RoutineRes> routineResList = routineProvider.getRoutinesByUserId(userId);

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<RoutineRes> 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<Integer> deleteAlarm(
@PathVariable @Valid Integer userId,
@PathVariable @Valid Integer routineId
) {
// TODO : JWT
routineService.deleteRoutine(routineId);

return new BaseResponse<>(ResponseStatus.SUCCESS);
}

//test
@GetMapping("/test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -34,7 +34,7 @@ public List<RoutineRes> getRoutinesByUserId(Integer userId) {

ArrayList<RoutineRes> routineResList = new ArrayList<>();
for(Routine routine : routineList) {
routineResList.add(RoutineMapper.INSTANCE.toGetRoutineRes(routine));
routineResList.add(RoutineMapper.INSTANCE.toRoutineRes(routine));
}

return routineResList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

@Repository
public interface RoutineRepository extends JpaRepository<Routine, Integer> {
@Override
<S extends Routine> S save(S entity);

@Override
Optional<Routine> findById(Integer id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 9fc166e

Please sign in to comment.