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

refactor: 알림 삭제 & 환율요청 api 수정 #158

Merged
merged 1 commit into from
Jun 10, 2024
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
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);

}

}
Loading