Skip to content

Commit

Permalink
Feature : update Alarm & ect (#9)
Browse files Browse the repository at this point in the history
패키지 구조를 바꿔서 나중에 충돌이 일어날 경우를 대비해서 미리 pr합니다.
아래 commit의 상세내역 확인을 부탁드립니다.
  • Loading branch information
hye-on authored May 5, 2023
2 parents efbbed1 + a12ee39 commit 7f320ac
Show file tree
Hide file tree
Showing 32 changed files with 486 additions and 358 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ dependencies {
implementation group: 'org.modelmapper', name: 'modelmapper', version: '3.0.0'

// model struct
implementation 'org.mapstruct:mapstruct:1.4.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.1.Final'
implementation 'org.mapstruct:mapstruct:1.5.4.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.4.Final'

// security
implementation('org.springframework.boot:spring-boot-starter-security')
Expand Down
69 changes: 51 additions & 18 deletions src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.wakeUpTogetUp.togetUp.alarms;

import com.wakeUpTogetUp.togetUp.alarms.model.GetAlarmRes;
import com.wakeUpTogetUp.togetUp.alarms.model.GetAlarmsRes;
import com.wakeUpTogetUp.togetUp.alarms.model.PostAlarmReq;
import com.wakeUpTogetUp.togetUp.common.BaseResponse;
import com.wakeUpTogetUp.togetUp.common.BaseResponseStatus;
import com.wakeUpTogetUp.togetUp.alarms.dto.response.AlarmRes;
import com.wakeUpTogetUp.togetUp.alarms.dto.response.AlarmsRes;
import com.wakeUpTogetUp.togetUp.alarms.dto.request.AlarmReq;
import com.wakeUpTogetUp.togetUp.common.dto.BaseResponse;
import com.wakeUpTogetUp.togetUp.common.ResponseStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand All @@ -18,34 +18,67 @@ public class AlarmController {
private final AlarmService alarmService;
private final AlarmProvider alarmProvider;

// 알람 1개 불러오기
/**
* 알람 1개 불러오기
* @param userId
* @param alarmId
* @return
*/
@GetMapping("{userId}/alarms/{alarmId}")
public BaseResponse<GetAlarmRes> GetAlarm(@PathVariable Integer userId, @PathVariable Integer alarmId){
GetAlarmRes getAlarmRes = alarmProvider.getAlarm(alarmId);
public BaseResponse<AlarmRes> GetAlarm(@PathVariable Integer userId, @PathVariable Integer alarmId){
AlarmRes alarmRes = alarmProvider.getAlarm(alarmId);

return new BaseResponse<>(BaseResponseStatus.SUCCESS, getAlarmRes);
return new BaseResponse<>(ResponseStatus.SUCCESS, alarmRes);
}

// 사용자 알람, 루틴 목록 불러오기
/**
* 사용자 알람, 루틴 목록 불러오기
* @param userId
* @return
*/
@GetMapping("{userId}/alarms")
public BaseResponse<List<GetAlarmsRes>> GetAlarmsByUserId(@PathVariable Integer userId){
List<GetAlarmsRes> getAlarmsResList = alarmProvider.getAlarmsByUserId(userId);
public BaseResponse<List<AlarmsRes>> GetAlarmsByUserId(@PathVariable Integer userId){
List<AlarmsRes> alarmsResList = alarmProvider.getAlarmsByUserId(userId);

return new BaseResponse<>(BaseResponseStatus.SUCCESS, getAlarmsResList);
return new BaseResponse<>(ResponseStatus.SUCCESS, alarmsResList);
}

// 알람 생성(알람, 알람-루틴 매핑)
/**
* 알람 생성(알람, 알람-루틴 매핑)
* @param userId
* @param alarmReq
* @return
*/
@PostMapping("/{userId}/alarms")
public BaseResponse createAlarm(
@PathVariable("userId") Integer userId,
@RequestBody @Valid PostAlarmReq postAlarmReq
@RequestBody @Valid AlarmReq alarmReq
){
//TODO : jwt 정보와 일치하는지 확인하기
int createdAlarmId = alarmService.createAlarm(userId, postAlarmReq);
Integer createdAlarmId = alarmService.createAlarm(userId, alarmReq);

return new BaseResponse(BaseResponseStatus.SUCCESS, createdAlarmId);
return new BaseResponse(ResponseStatus.SUCCESS, createdAlarmId);
}

/**
* 알람 수정
* @param userId
* @param alarmId
* @param patchAlarmReq
* @return
*/
@PatchMapping("{userId}/alarms/{alarmId}")
public BaseResponse<AlarmRes> updateAlarm(
@PathVariable Integer userId,
@PathVariable Integer alarmId,
@RequestBody @Valid AlarmReq patchAlarmReq
) {
// TODO : JWT

AlarmRes patchAlarmRes = alarmService.updateAlarm(userId, alarmId, patchAlarmReq);

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

// 알람 수정
// 알람 삭제
}
45 changes: 0 additions & 45 deletions src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmDao.java

This file was deleted.

68 changes: 45 additions & 23 deletions src/main/java/com/wakeUpTogetUp/togetUp/alarms/AlarmProvider.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.wakeUpTogetUp.togetUp.alarms;

import com.wakeUpTogetUp.togetUp.alarms.model.Alarm;
import com.wakeUpTogetUp.togetUp.alarms.model.GetAlarmRes;
import com.wakeUpTogetUp.togetUp.alarms.model.GetAlarmsRes;
import com.wakeUpTogetUp.togetUp.common.BaseException;
import com.wakeUpTogetUp.togetUp.common.BaseResponseStatus;
import com.wakeUpTogetUp.togetUp.routines.RoutineDao;
import com.wakeUpTogetUp.togetUp.routines.model.GetRoutineRes;
import com.wakeUpTogetUp.togetUp.alarms.dto.response.AlarmRes;
import com.wakeUpTogetUp.togetUp.alarms.dto.response.AlarmsRes;
import com.wakeUpTogetUp.togetUp.common.exception.BaseException;
import com.wakeUpTogetUp.togetUp.common.ResponseStatus;
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.utils.mappers.AlarmMapper;
import com.wakeUpTogetUp.togetUp.utils.mappers.RoutineMapper;
Expand All @@ -19,41 +19,63 @@
@Service
@RequiredArgsConstructor
public class AlarmProvider {
private final AlarmDao alarmDao;
private final RoutineDao routineDao;
private final AlarmRepository alarmRepository;
private final RoutineRepository routineRepository;

public List<GetAlarmsRes> getAlarmsByUserId(Integer userId) {
List<Alarm> alarmList = alarmDao.findByUserId(userId)
/**
* 유저 아이디로 알람 리스트 가져오기
* @param userId
* @return
*/
public List<AlarmsRes> getAlarmsByUserId(Integer userId) {
List<Alarm> alarmList = alarmRepository.findByUserId(userId)
.orElseThrow(
() -> new BaseException(BaseResponseStatus.ALARM_NOT_FOUND)
() -> new BaseException(ResponseStatus.ALARM_NOT_FOUND)
);

ArrayList<GetAlarmsRes> getAlarmsResList = new ArrayList<>();
// dto 매핑
ArrayList<AlarmsRes> alarmsResList = new ArrayList<>();
for(Alarm alarm : alarmList) {
getAlarmsResList.add(AlarmMapper.INSTANCE.entityToGetAlarmsRes(alarm));
alarmsResList.add(AlarmMapper.INSTANCE.toAlarmsRes(alarm));
}

return getAlarmsResList;
return alarmsResList;
}

public GetAlarmRes getAlarm(Integer alarmId) {
/**
* 알람 아이디로 알람 가져오기
* @param alarmId
* @return
*/
public AlarmRes getAlarm(Integer alarmId) {
// 알람 가져오기
Alarm alarm = alarmDao.findById(alarmId)
Alarm alarm = alarmRepository.findById(alarmId)
.orElseThrow(
() -> new BaseException(BaseResponseStatus.INVALID_ALARM_ID)
() -> new BaseException(ResponseStatus.INVALID_ALARM_ID)
);

// 루틴 리스트 가져오기
// 하나도 존재하지 않아도 됨
List<Routine> routineList = routineDao.findByAlarmId(alarmId).orElseGet(() -> null);
List<RoutineRes> routineResList = getRoutineResByAlarmId(alarmId);

ArrayList<GetRoutineRes> getRoutineResList = new ArrayList<>();
AlarmRes alarmRes = AlarmMapper.INSTANCE.toAlarmRes(alarm, routineResList);

return alarmRes;
}

/**
* alarmId로 routineRes 리스트 가져오기
* @param alarmId
* @return
*/
public List<RoutineRes> getRoutineResByAlarmId(Integer alarmId){
List<Routine> routineList = routineRepository.findByAlarmId(alarmId).orElseGet(() -> null);

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

GetAlarmRes getAlarmRes = AlarmMapper.INSTANCE.entityToGetAlarmRes(alarm, getRoutineResList);

return getAlarmRes;
return routineResList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.wakeUpTogetUp.togetUp.alarms;

import com.wakeUpTogetUp.togetUp.alarms.model.Alarm;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
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);
}
Loading

0 comments on commit 7f320ac

Please sign in to comment.