From 5fa8da10c95562186864cbac927aeb288c6f783c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=A1=E1=86=AB=E1=84=82=E1=85=A1=E1=84=8B?= =?UTF-8?q?=E1=85=A7=E1=86=BC?= Date: Mon, 10 Jun 2024 11:38:27 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=95=8C=EB=A6=BC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20&=20=ED=99=98=EC=9C=A8=EC=9A=94=EC=B2=AD=20api=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/response/ResponseStatus.java | 1 + .../triptogether/exception/ExceptionAdvice.java | 8 ++++++++ .../controller/ExchangeRateAlarmController.java | 6 +++--- .../exchangeRate/domain/entity/ExchangeRate.java | 10 +++++----- .../domain/entity/ExchangeRateAlarm.java | 2 -- .../repository/ExchangeRateAlarmRepository.java | 4 ++++ .../dto/request/ExchangeRateAlarmRequestDto.java | 2 -- .../dto/request/ExchangeRateResponse.java | 8 ++++---- .../exception/AlarmNotFoundException.java | 7 +++++++ .../exchangeRate/service/ExchangeService.java | 13 +++++++++---- 10 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/hanaro/triptogether/exchangeRate/exception/AlarmNotFoundException.java diff --git a/src/main/java/com/hanaro/triptogether/common/response/ResponseStatus.java b/src/main/java/com/hanaro/triptogether/common/response/ResponseStatus.java index 0d65986..e994dd8 100644 --- a/src/main/java/com/hanaro/triptogether/common/response/ResponseStatus.java +++ b/src/main/java/com/hanaro/triptogether/common/response/ResponseStatus.java @@ -19,6 +19,7 @@ public enum ResponseStatus { // 404 Not Found USER_NOT_FOUND(HttpStatus.NOT_FOUND, false, "사용자를 찾을 수 없습니다."), DUES_NOT_FOUND(HttpStatus.NOT_FOUND,false,"회비규칙을 찾을 수 없습니다."), + ALARM_NOT_FOUND(HttpStatus.NOT_FOUND,false,"알람을 찾을 수 없습니다."), // 405 Method Not Allowed METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, false, "허용되지 않은 메소드입니다."), diff --git a/src/main/java/com/hanaro/triptogether/exception/ExceptionAdvice.java b/src/main/java/com/hanaro/triptogether/exception/ExceptionAdvice.java index 3d4269c..b8c09ae 100644 --- a/src/main/java/com/hanaro/triptogether/exception/ExceptionAdvice.java +++ b/src/main/java/com/hanaro/triptogether/exception/ExceptionAdvice.java @@ -2,6 +2,7 @@ import com.hanaro.triptogether.common.response.BaseResponse; import com.hanaro.triptogether.common.response.ResponseStatus; +import com.hanaro.triptogether.exchangeRate.exception.AlarmNotFoundException; import org.apache.coyote.BadRequestException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -34,4 +35,11 @@ public ResponseEntity handleBadRequestException(BadRequestExceptio BaseResponse response = BaseResponse.res(ResponseStatus.BAD_REQUEST, ex.getMessage()); return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + + @ExceptionHandler(AlarmNotFoundException.class) + public ResponseEntity handleAlarmNotFoundException() { + BaseResponse response = BaseResponse.res(ResponseStatus.ALARM_NOT_FOUND,ResponseStatus.ALARM_NOT_FOUND.getMessage()); + return new ResponseEntity<>(response, HttpStatus.NOT_FOUND); + } + } \ No newline at end of file diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/controller/ExchangeRateAlarmController.java b/src/main/java/com/hanaro/triptogether/exchangeRate/controller/ExchangeRateAlarmController.java index abec303..3bc9487 100644 --- a/src/main/java/com/hanaro/triptogether/exchangeRate/controller/ExchangeRateAlarmController.java +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/controller/ExchangeRateAlarmController.java @@ -28,9 +28,9 @@ public BaseResponse> getExchangeRateAlarmList return BaseResponse.res(ResponseStatus.SUCCESS,ResponseStatus.SUCCESS.getMessage(),exchangeService.getExchangeRateAlarmList(memberIdx)); } - @DeleteMapping("/exchange-rate/{memberIdx}") - public BaseResponse deleteAlarm(@PathVariable("memberIdx") Long memberIdx){ - exchangeService.deleteAlarm(memberIdx); + @DeleteMapping("/exchange-rate/{memberIdx}/{alarmIdx}") + public BaseResponse deleteAlarm(@PathVariable("memberIdx") Long memberIdx,@PathVariable("alarmIdx")Long alarmIdx){ + exchangeService.deleteAlarm(memberIdx,alarmIdx); return BaseResponse.res(ResponseStatus.SUCCESS,ResponseStatus.SUCCESS.getMessage()); } diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRate.java b/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRate.java index 0e6a882..5438733 100644 --- a/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRate.java +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRate.java @@ -22,7 +22,7 @@ public class ExchangeRate { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long curIdx; - @Column(name = "curCode",nullable = false, length = 10) + @Column(nullable = false, length = 10) private String curCode; @Column(nullable = false, length = 10) @@ -55,10 +55,10 @@ public class ExchangeRate { public ExchangeRateResponse toDto(String cur_icon) { return ExchangeRateResponse.builder() - .cur_code(curCode) - .cur_name(curName) - .cur_icon(cur_icon) - .cur_rate(String.valueOf(curRate)) + .curCode(curCode) + .curName(curName) + .curIcon(cur_icon) + .curRate(String.valueOf(curRate)) .build(); } diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRateAlarm.java b/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRateAlarm.java index ff63014..5f6b578 100644 --- a/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRateAlarm.java +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/domain/entity/ExchangeRateAlarm.java @@ -36,8 +36,6 @@ public class ExchangeRateAlarm extends BaseEntity { @Column(name = "cur_rate", nullable = false) private BigDecimal curRate; - private String fcmToken; - private Boolean notified; @Enumerated(EnumType.STRING) diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/domain/repository/ExchangeRateAlarmRepository.java b/src/main/java/com/hanaro/triptogether/exchangeRate/domain/repository/ExchangeRateAlarmRepository.java index eb58236..047ce8e 100644 --- a/src/main/java/com/hanaro/triptogether/exchangeRate/domain/repository/ExchangeRateAlarmRepository.java +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/domain/repository/ExchangeRateAlarmRepository.java @@ -4,10 +4,14 @@ import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; +import java.util.Optional; public interface ExchangeRateAlarmRepository extends JpaRepository { List findExchangeRatesAlarmByMember_MemberIdx(Long memberIdx); ExchangeRateAlarm findExchangeRateAlarmByMember_MemberIdx(Long memberIdx); + + Optional findByMember_MemberIdxAndAlarmIdx(Long memberIdx, Long alarmIdx); + } diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateAlarmRequestDto.java b/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateAlarmRequestDto.java index 22eecab..b9d0281 100644 --- a/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateAlarmRequestDto.java +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateAlarmRequestDto.java @@ -19,12 +19,10 @@ public class ExchangeRateAlarmRequestDto { private String curCode; private String curRate; private ExchangeRateAlarmType rateAlarmType; - private String fcmToken; public ExchangeRateAlarm toEntity(Member member, ExchangeRate exchangeRate) { return ExchangeRateAlarm.builder() .member(member) - .fcmToken(fcmToken) .exchangeRate(exchangeRate) .rateType(rateAlarmType) .curRate(BigDecimalConverter.convertStringToBigDecimal(curRate)).build(); diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateResponse.java b/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateResponse.java index fae18bf..9329b7a 100644 --- a/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateResponse.java +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/dto/request/ExchangeRateResponse.java @@ -8,8 +8,8 @@ @Getter @Setter public class ExchangeRateResponse { - private String cur_code; - private String cur_name; - private String cur_icon; - private String cur_rate; + private String curCode; + private String curName; + private String curIcon; + private String curRate; } diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/exception/AlarmNotFoundException.java b/src/main/java/com/hanaro/triptogether/exchangeRate/exception/AlarmNotFoundException.java new file mode 100644 index 0000000..0ea7bc0 --- /dev/null +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/exception/AlarmNotFoundException.java @@ -0,0 +1,7 @@ +package com.hanaro.triptogether.exchangeRate.exception; + +public class AlarmNotFoundException extends RuntimeException{ + public AlarmNotFoundException(){ + super(); + } +} diff --git a/src/main/java/com/hanaro/triptogether/exchangeRate/service/ExchangeService.java b/src/main/java/com/hanaro/triptogether/exchangeRate/service/ExchangeService.java index 1ff5533..f0cd5c7 100644 --- a/src/main/java/com/hanaro/triptogether/exchangeRate/service/ExchangeService.java +++ b/src/main/java/com/hanaro/triptogether/exchangeRate/service/ExchangeService.java @@ -2,6 +2,7 @@ import com.hanaro.triptogether.common.BigDecimalConverter; import com.hanaro.triptogether.common.firebase.FirebaseFCMService; +import com.hanaro.triptogether.common.response.BaseResponse; import com.hanaro.triptogether.enumeration.ExchangeRateAlarmType; import com.hanaro.triptogether.exchangeRate.domain.entity.ExchangeRate; import com.hanaro.triptogether.exchangeRate.domain.entity.ExchangeRateAlarm; @@ -12,6 +13,7 @@ import com.hanaro.triptogether.exchangeRate.dto.request.ExchangeRateResponse; import com.hanaro.triptogether.exchangeRate.dto.request.FcmSendDto; import com.hanaro.triptogether.exchangeRate.dto.response.ExchangeRateAlarmResponseDto; +import com.hanaro.triptogether.exchangeRate.exception.AlarmNotFoundException; import com.hanaro.triptogether.exchangeRate.exception.EntityNotFoundException; import com.hanaro.triptogether.member.domain.Member; import com.hanaro.triptogether.member.domain.MemberRepository; @@ -97,7 +99,8 @@ public void checkNotifyAlarms() throws IOException { } if (notify) { - firebaseFCMService.sendMessageTo(FcmSendDto.builder().token(alarm.getFcmToken()).title("환율 알림").body("환율이 "+alarm.getCurRate()+" 에 도달했어요~!!.").build()); + Member member = memberRepository.findById(alarm.getMember().getMemberIdx()).orElseThrow(EntityNotFoundException::new); + firebaseFCMService.sendMessageTo(FcmSendDto.builder().token(member.getFcmToken()).title("환율 알림").body("환율이 "+alarm.getCurRate()+" 에 도달했어요~!!.").build()); alarm.setNotified(true); exchangeRateAlarmRepository.save(alarm); @@ -119,9 +122,11 @@ public void resetNotifiedFlags() { @Transactional - public void deleteAlarm(Long memberIdx) { - ExchangeRateAlarm exchangeRateAlarm = exchangeRateAlarmRepository.findExchangeRateAlarmByMember_MemberIdx(memberIdx); - exchangeRateAlarmRepository.delete(exchangeRateAlarm); + public void deleteAlarm(Long memberIdx, Long alarmIdx) { + ExchangeRateAlarm alarm = exchangeRateAlarmRepository.findByMember_MemberIdxAndAlarmIdx(memberIdx, alarmIdx) + .orElseThrow(AlarmNotFoundException::new); + exchangeRateAlarmRepository.delete(alarm); + } }