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

[#19] Transaction 발생 시 알림 구현 #20

Merged
merged 2 commits into from
Jan 29, 2025
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
2 changes: 1 addition & 1 deletion application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ spring:
jpa:
hibernate:
ddl-auto: update
show-sql: true
show-sql: false
generate-ddl: true
main:
web-application-type: servlet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class FilteringService {
public FilteringService(TransactionService transactionService) {
this.transactionService = transactionService;

volumeThresholdMap.put("BTC", 0.5);
volumeThresholdMap.put("ETH", 15.0);
volumeThresholdMap.put("SOL", 90.0);
volumeThresholdMap.put("BTC", 0.8);
volumeThresholdMap.put("ETH", 22.0);
volumeThresholdMap.put("SOL", 300.0);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import com.whalewatch.domain.AlertSetting;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface AlertRepository extends JpaRepository<AlertSetting, Integer> {
List<AlertSetting> findByCoin(String coin);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package com.whalewatch.service;

import com.whalewatch.domain.AlertSetting;
import com.whalewatch.domain.Transaction;
import com.whalewatch.domain.UserAlert;
import com.whalewatch.repository.AlertRepository;
import com.whalewatch.repository.TransactionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TransactionService {
private static final Logger log = LoggerFactory.getLogger(TransactionService.class);

private final TransactionRepository transactionRepository;
private final AlertRepository alertRepository;
private final UserAlertService userAlertService;

public TransactionService(TransactionRepository transactionRepository) {
public TransactionService(TransactionRepository transactionRepository,
AlertRepository alertRepository,
UserAlertService userAlertService) {
this.transactionRepository = transactionRepository;
this.alertRepository = alertRepository;
this.userAlertService = userAlertService;
}

public List<Transaction> getAllTransactions() {
Expand All @@ -23,6 +36,34 @@ public Transaction getTransactionById(int id) {
}

public Transaction createTransaction(Transaction tx) {
return transactionRepository.save(tx);
Transaction savedTx = transactionRepository.save(tx);

// 트랜잭션 저장 후 시점
long startAlertTime = System.currentTimeMillis();

// AlertSetting 조회
List<AlertSetting> settings = alertRepository.findByCoin(savedTx.getCoin());

// 임계값 비교
for (AlertSetting setting : settings) {
if (savedTx.getTradeVolume() >= setting.getThreshold()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 로직은 DB에서 데이터를 가져올 때, filtering을 할 수 있지 않을까요?

// 임계값 초과 UserAlert 생성
UserAlert userAlert = new UserAlert(
setting.getUserId(),
savedTx.getCoin(),
savedTx.getTradePrice(),
savedTx.getTradeVolume(),
savedTx.getTradeTimestamp()
);
userAlertService.createUserAlert(userAlert);
}
}
// Alert db 저장 후 시점
long endAlertTime = System.currentTimeMillis();

long alertInsertionTime = endAlertTime - startAlertTime;
log.info("Transaction save {}ms",alertInsertionTime);
return savedTx;
}

}
Loading