Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature : update Alarm & ect #9

Merged
merged 4 commits into from
May 5, 2023
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
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