Skip to content

Commit

Permalink
refactor: 알림 삭제 & 환율요청 api 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ny2060 committed Jun 10, 2024
1 parent 0e7eca3 commit 5fa8da1
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, "허용되지 않은 메소드입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,4 +35,11 @@ public ResponseEntity<BaseResponse> handleBadRequestException(BadRequestExceptio
BaseResponse response = BaseResponse.res(ResponseStatus.BAD_REQUEST, ex.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(AlarmNotFoundException.class)
public ResponseEntity<BaseResponse> handleAlarmNotFoundException() {
BaseResponse response = BaseResponse.res(ResponseStatus.ALARM_NOT_FOUND,ResponseStatus.ALARM_NOT_FOUND.getMessage());
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public BaseResponse<List<ExchangeRateAlarmResponseDto>> 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());
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface ExchangeRateAlarmRepository extends JpaRepository<ExchangeRateAlarm,Long> {

List<ExchangeRateAlarm> findExchangeRatesAlarmByMember_MemberIdx(Long memberIdx);

ExchangeRateAlarm findExchangeRateAlarmByMember_MemberIdx(Long memberIdx);

Optional<ExchangeRateAlarm> findByMember_MemberIdxAndAlarmIdx(Long memberIdx, Long alarmIdx);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hanaro.triptogether.exchangeRate.exception;

public class AlarmNotFoundException extends RuntimeException{
public AlarmNotFoundException(){
super();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

}

}

0 comments on commit 5fa8da1

Please sign in to comment.