-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TRAC-5] - Create Alert , Notification , Command /add /get monitoring…
… and BinanceApiClient
- Loading branch information
1 parent
c3cbc9d
commit 9016e13
Showing
21 changed files
with
539 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/api/BinanceApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.adaptors.api; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.api.DTO.BinancePriceResponse; | ||
import java.math.BigDecimal; | ||
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class BinanceApiClient { | ||
private final String BINANCE_URL = "https://api.binance.com/api/v3/ticker/price?symbol="; | ||
private final RestTemplate restTemplate = new RestTemplate(); | ||
private final String TICKER_CURRENCY = "USDT"; | ||
|
||
public boolean isCoinAvailable(String ticker) { | ||
String url = BINANCE_URL + ticker.toUpperCase() + TICKER_CURRENCY; | ||
try { | ||
restTemplate.getForEntity(url, String.class); | ||
return true; | ||
} catch (HttpClientErrorException.NotFound e) { | ||
log.warn("Coin not available: {}", ticker); | ||
return false; | ||
} catch (Exception e) { | ||
log.error("Error while checking coin availability", e); | ||
return false; | ||
} | ||
} | ||
|
||
public Optional<BigDecimal> getPrice(String ticker) { | ||
String url = BINANCE_URL + ticker; | ||
try { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
BinancePriceResponse response = restTemplate.getForObject(url, BinancePriceResponse.class); | ||
if (response != null && response.getPrice() != null) { | ||
return Optional.of(response.getPrice()); | ||
} else { | ||
log.warn("Price not found for ticker: {}", ticker); | ||
return Optional.empty(); | ||
} | ||
} catch (Exception e) { | ||
log.error("Failed to fetch price for ticker: {}. Error: {}", ticker, e.getMessage(), e); | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/api/DTO/BinancePriceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.adaptors.api.DTO; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.math.BigDecimal; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
public class BinancePriceResponse { | ||
private String symbol; | ||
private BigDecimal price; | ||
|
||
@JsonProperty("symbol") | ||
public String getSymbol() { | ||
return symbol; | ||
} | ||
|
||
@JsonProperty("price") | ||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
.../java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/MonitoringRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.adaptors.persistence; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.domain.Monitoring; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface MonitoringRepository extends JpaRepository<Monitoring, Long> { | ||
|
||
List<Monitoring> findByUserId(Long userId); | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.adaptors.persistence; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.domain.User; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
Optional<User> findByChatId(Long chatId); | ||
|
||
User findUserByChatId(Long chatId); | ||
} |
5 changes: 0 additions & 5 deletions
5
src/main/java/com/vladyslavpalamarchuk/trackmycoin/adaptors/persistence/package-info.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...com/vladyslavpalamarchuk/trackmycoin/command/processor/AddMonitoringCommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command.processor; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.api.BinanceApiClient; | ||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.telegram.TelegramBotClient; | ||
import com.vladyslavpalamarchuk.trackmycoin.command.Command; | ||
import com.vladyslavpalamarchuk.trackmycoin.service.MonitoringService; | ||
import java.math.BigDecimal; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class AddMonitoringCommandProcessor implements CommandProcessor { | ||
|
||
private final BinanceApiClient binanceApiClient; | ||
private final TelegramBotClient telegramBotClient; | ||
private final MonitoringService monitoringService; | ||
private final String ADD_COMMAND = "/add"; | ||
private final int PART_LENGTH = 2; | ||
|
||
@Override | ||
public void process(Update update) { | ||
Long chatId = update.getMessage().getChatId(); | ||
String userMessage = update.getMessage().getText(); | ||
if (userMessage.equalsIgnoreCase(ADD_COMMAND)) { | ||
telegramBotClient.sendMessage( | ||
chatId, "Please enter the coin ticker and its current price (for example, BTC 50000):"); | ||
return; | ||
} | ||
|
||
handleTickerInput(update); | ||
} | ||
|
||
private void handleTickerInput(Update update) { | ||
Long chatId = getChatId(update); | ||
|
||
String[] inputParts = extractAndValidateInput(update, chatId); | ||
|
||
if (inputParts != null) { | ||
processValidTicker(chatId, inputParts[0], inputParts[1]); | ||
} | ||
} | ||
|
||
private Long getChatId(Update update) { | ||
return update.getMessage().getChatId(); | ||
} | ||
|
||
private String[] extractAndValidateInput(Update update, Long chatId) { | ||
String[] parts = update.getMessage().getText().split(" "); | ||
if (parts.length != PART_LENGTH) { | ||
telegramBotClient.sendMessage( | ||
chatId, "Please enter two values: the coin ticker and its current price."); | ||
return null; | ||
} | ||
return parts; | ||
} | ||
|
||
private void processValidTicker(Long chatId, String ticker, String price) { | ||
if (isCoinAvailable(ticker) && isPriceAvailable(price)) { | ||
addTickerToMonitoring(ticker, price, chatId); | ||
} else { | ||
sendCoinNotFoundMessage(chatId); | ||
} | ||
} | ||
|
||
private boolean isCoinAvailable(String ticker) { | ||
return binanceApiClient.isCoinAvailable(ticker); | ||
} | ||
|
||
private boolean isPriceAvailable(String price) { | ||
if (price == null || price.trim().isEmpty()) { | ||
return false; | ||
} | ||
try { | ||
new BigDecimal(price); | ||
return true; | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
} | ||
|
||
private void addTickerToMonitoring(String ticker, String price, Long chatId) { | ||
monitoringService.add(ticker, price, chatId); | ||
} | ||
|
||
private void sendCoinNotFoundMessage(Long chatId) { | ||
telegramBotClient.sendMessage(chatId, "Coin not found. Please try again."); | ||
} | ||
|
||
@Override | ||
public Command getCommand() { | ||
return Command.ADDMONITOR; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...com/vladyslavpalamarchuk/trackmycoin/command/processor/GetMonitoringCommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.vladyslavpalamarchuk.trackmycoin.command.processor; | ||
|
||
import com.vladyslavpalamarchuk.trackmycoin.adaptors.persistence.UserRepository; | ||
import com.vladyslavpalamarchuk.trackmycoin.command.Command; | ||
import com.vladyslavpalamarchuk.trackmycoin.service.MonitoringService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.telegram.telegrambots.meta.api.objects.Update; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GetMonitoringCommandProcessor implements CommandProcessor { | ||
|
||
private final UserRepository userRepository; | ||
private final MonitoringService monitoringService; | ||
|
||
@Override | ||
public void process(Update update) { | ||
Long chatId = update.getMessage().getChatId(); | ||
|
||
userRepository | ||
.findByChatId(chatId) | ||
.ifPresent( | ||
user -> { | ||
monitoringService.get(user.getId()); | ||
}); | ||
} | ||
|
||
@Override | ||
public Command getCommand() { | ||
return Command.GETMONITOR; | ||
} | ||
} |
24 changes: 21 additions & 3 deletions
24
...in/java/com/vladyslavpalamarchuk/trackmycoin/command/processor/StartCommandProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.