22
33import fr .polytech .codev .backend .entities .Alert ;
44import fr .polytech .codev .backend .forms .AlertForm ;
5- import fr .polytech .codev .backend .requesters .JsonStringRestfulRequester ;
6- import fr .polytech .codev .backend .requesters .RestfulRequester ;
75import fr .polytech .codev .backend .services .impl .AlertServices ;
6+ import fr .polytech .codev .backend .services .impl .CoinMarketCapServices ;
87import org .slf4j .Logger ;
98import org .slf4j .LoggerFactory ;
109import org .springframework .beans .factory .annotation .Autowired ;
2019@ Component
2120public class AlertTasks {
2221
23- public static final String DEFAULT_TICKER_BASE_URL = "https://api.coinmarketcap.com/v1/ticker/" ;
24-
2522 public static final String DEFAULT_NOTIFICATION_EMAIL = "<table border=\" 0\" cellspacing=\" 0\" cellpadding=\" 0\" width=\" 600\" align=\" center\" style=\" border-radius:4px;border:1px solid rgb=(221,221,221);background:rgb(255,255,255)\" bgcolor=\" #ffffff\" > <tbody> <tr> <td style=\" color:rgb(102,102,102);font-size:16px;font=-family:'Open Sans','Helvetica Neue',Arial,sans-serif;line-height:1.5\" > <table border=\" 0\" cellspacing=\" 0\" cellpadding=\" 0\" width=\" 500\" align=\" center\" style=\" margin:42px\" > <tbody> <tr> <td width=\" 500\" cellpadding=\" 0\" align=\" center\" style=\" font-family:helvetica;font-size:24px;font-weight:300;color:rgb(85,85,85);line-height:1.5\" > <p style=\" color:rgb(102,102,102);font=-size:16px;font-family:'Open Sans','Helvetica Neue',Arial,sans-serif;line-height:1.5;margin:0px\" >Hello</p><p style=\" font-family:helvetica;font-=size:34px;font-weight:600;color:rgb(85,85,85);line-height:1.5;margin:0px 0px 42px\" >Alert on %s (%s)!</p></td></tr><tr> <td width=\" 500\" cellpadding=\" 0\" align=\" center\" style=\" font-family:helvetica;font-size:16px;font-weight:300;color:rgb(102,102,102);line-height:1.5\" > <p style=\" color:rgb(102,102,102);font=-size:16px;font-family:'Open Sans','Helvetica Neue',Arial,sans-serif;line-height:1.5;margin:0px 0px 21px\" >We send you this alert because the price of %s (%s) matches your defined constraints.</p><p style=\" color:rgb(102,102,102);font=-size:16px;font-family:'Open Sans','Helvetica Neue',Arial,sans-serif;line-height:1.5;margin:0px 0px 21px\" ><strong>Current value is %s BTC.</strong></p></td></tr></tbody> </table> </td></tr></tbody></table>" ;
2623
2724 private static Logger logger = LoggerFactory .getLogger (AlertTasks .class );
2825
2926 @ Autowired
30- private AlertServices alertServices ;
27+ private CoinMarketCapServices coinMarketCapServices ;
3128
3229 @ Autowired
33- private JavaMailSender javaMailSender ;
34-
35- private final RestfulRequester restfulRequester ;
30+ private AlertServices alertServices ;
3631
37- public AlertTasks () {
38- this .restfulRequester = new JsonStringRestfulRequester (DEFAULT_TICKER_BASE_URL );
39- }
32+ @ Autowired
33+ private JavaMailSender mailSender ;
4034
4135 @ Scheduled (fixedDelay = 60 * 60000 )
4236 public void processAlerts () throws Exception {
4337 this .alertServices .all ().parallelStream ().filter (alert -> alert .isActive ()).forEach (alert -> processAlert (alert ));
4438 }
4539
4640 private void processAlert (Alert alert ) {
47- final BigDecimal currentPrice = this .restfulRequester . get (alert .getCryptocurrency ().getResourceUrl (), TickerResponse []. class )[ 0 ] .getPriceBtc ();
41+ final BigDecimal currentPrice = this .coinMarketCapServices . getCurrentPrice (alert .getCryptocurrency ().getResourceUrl ()) .getPriceBtc ();
4842 if (equalityMatches (currentPrice , alert .getType ().getName (), alert .getThreshold ())) {
4943 try {
5044 sendNotification (alert , currentPrice );
@@ -90,36 +84,13 @@ private void sendNotification(Alert alert, BigDecimal currentPrice) throws Messa
9084 final String cryptocurrencyName = alert .getCryptocurrency ().getName ();
9185 final String cryptocurrencySymbol = alert .getCryptocurrency ().getSymbol ();
9286
93- final MimeMessage email = this .javaMailSender .createMimeMessage ();
87+ final MimeMessage email = this .mailSender .createMimeMessage ();
9488 final MimeMessageHelper emailHelper = new MimeMessageHelper (email );
9589
9690 emailHelper .setTo (alert .getUser ().getEmail ());
9791 emailHelper .setSubject ("CryptoWallet (" + alert .getName () + ")" );
9892 emailHelper .setText (String .format (DEFAULT_NOTIFICATION_EMAIL , cryptocurrencyName , cryptocurrencySymbol , cryptocurrencyName , cryptocurrencySymbol , currentPrice ), true );
9993
100- this .javaMailSender .send (email );
101- }
102-
103- static class TickerResponse {
104-
105- private BigDecimal priceUsd ;
106-
107- private BigDecimal priceBtc ;
108-
109- public BigDecimal getPriceUsd () {
110- return priceUsd ;
111- }
112-
113- public void setPrice_usd (final BigDecimal priceUsd ) {
114- this .priceUsd = priceUsd ;
115- }
116-
117- public BigDecimal getPriceBtc () {
118- return priceBtc ;
119- }
120-
121- public void setPrice_btc (final BigDecimal priceBtc ) {
122- this .priceBtc = priceBtc ;
123- }
94+ this .mailSender .send (email );
12495 }
12596}
0 commit comments