Skip to content

Commit 9b38b03

Browse files
committed
Merge branch 'develop' into release
2 parents e6cfa9d + 9f2ca36 commit 9b38b03

4 files changed

Lines changed: 162 additions & 46 deletions

File tree

src/main/java/fr/polytech/codev/backend/BackendApplication.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,24 @@ public WalletServices walletServices() {
162162
}
163163

164164
@Bean
165-
public JavaMailSender javaMailSender() {
166-
final JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
167-
javaMailSender.setHost("smtp.gmail.com");
168-
javaMailSender.setPort(587);
169-
javaMailSender.setUsername("cryptowallet.alerts@gmail.com");
170-
javaMailSender.setPassword("ProjetCODEV");
171-
172-
final Properties properties = javaMailSender.getJavaMailProperties();
165+
public CoinMarketCapServices coinMarketCapServices() {
166+
return new CoinMarketCapServices();
167+
}
168+
169+
@Bean
170+
public JavaMailSender mailSender() {
171+
final JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
172+
mailSender.setHost("smtp.gmail.com");
173+
mailSender.setPort(587);
174+
mailSender.setUsername("cryptowallet.alerts@gmail.com");
175+
mailSender.setPassword("ProjetCODEV");
176+
177+
final Properties properties = mailSender.getJavaMailProperties();
173178
properties.put("mail.transport.protocol", "smtp");
174179
properties.put("mail.smtp.auth", "true");
175180
properties.put("mail.smtp.starttls.enable", "true");
176181
properties.put("mail.debug", "false");
177182

178-
return javaMailSender;
183+
return mailSender;
179184
}
180185
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package fr.polytech.codev.backend.controllers.coinmarketcap;
2+
3+
import fr.polytech.codev.backend.controllers.AbstractController;
4+
import fr.polytech.codev.backend.services.impl.CoinMarketCapServices;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.MediaType;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.*;
9+
10+
@CrossOrigin
11+
@RestController
12+
@RequestMapping("/api/cryptowallet/coinmarketcap")
13+
public class CoinMarketCapController extends AbstractController {
14+
15+
@Autowired
16+
private CoinMarketCapServices coinMarketCapServices;
17+
18+
@RequestMapping(value = "/graphs/{cryptocurrencyResourceUrl}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
19+
public ResponseEntity allPrices(@PathVariable String cryptocurrencyResourceUrl) {
20+
return serializeSuccessResponse(this.coinMarketCapServices.allPrices(cryptocurrencyResourceUrl));
21+
}
22+
23+
@RequestMapping(value = "/graphs/{cryptocurrencyResourceUrl}/{startDate}/{endDate}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
24+
public ResponseEntity allPricesBetween(@PathVariable String cryptocurrencyResourceUrl, @PathVariable String startDate, @PathVariable String endDate) {
25+
return serializeSuccessResponse(this.coinMarketCapServices.allPricesBetween(cryptocurrencyResourceUrl, startDate, endDate));
26+
}
27+
28+
@RequestMapping(value = "/ticker/{cryptocurrencyResourceUrl}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
29+
public ResponseEntity getCurrentPrice(@PathVariable String cryptocurrencyResourceUrl) {
30+
return serializeSuccessResponse(this.coinMarketCapServices.getCurrentPrice(cryptocurrencyResourceUrl));
31+
}
32+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package fr.polytech.codev.backend.services.impl;
2+
3+
import fr.polytech.codev.backend.requesters.JsonStringRestfulRequester;
4+
import fr.polytech.codev.backend.requesters.RestfulRequester;
5+
6+
import java.math.BigDecimal;
7+
import java.math.BigInteger;
8+
import java.util.List;
9+
10+
public class CoinMarketCapServices {
11+
12+
public static final String DEFAULT_COIN_MARKET_CAP_TICKER_BASE_URL = "https://api.coinmarketcap.com/v1/ticker/";
13+
14+
public static final String DEFAULT_COIN_MARKET_CAP_TICKER_RESOURCE_URL = "%s/";
15+
16+
public static final String DEFAULT_COIN_MARKET_CAP_GRAPHS_BASE_URL = "https://graphs2.coinmarketcap.com/currencies/";
17+
18+
public static final String DEFAULT_COIN_MARKET_CAP_GRAPHS_ALL_RESOURCE_URL = "%s/";
19+
20+
public static final String DEFAULT_COIN_MARKET_CAP_GRAPHS_ALL_BETWEEN_RESOURCE_URL = "%s/%s/%s/";
21+
22+
private final RestfulRequester coinMarketCapTickerRequester;
23+
24+
private final RestfulRequester coinMarketCapGraphsRequester;
25+
26+
public CoinMarketCapServices() {
27+
this.coinMarketCapTickerRequester = new JsonStringRestfulRequester(DEFAULT_COIN_MARKET_CAP_TICKER_BASE_URL);
28+
this.coinMarketCapGraphsRequester = new JsonStringRestfulRequester(DEFAULT_COIN_MARKET_CAP_GRAPHS_BASE_URL);
29+
}
30+
31+
public GraphsResponse allPrices(String cryptocurrencyResourceUrl) {
32+
return this.coinMarketCapGraphsRequester.get(String.format(DEFAULT_COIN_MARKET_CAP_GRAPHS_ALL_RESOURCE_URL, cryptocurrencyResourceUrl), GraphsResponse.class);
33+
}
34+
35+
public GraphsResponse allPricesBetween(String cryptocurrencyResourceUrl, String startDate, String endDate) {
36+
return this.coinMarketCapGraphsRequester.get(String.format(DEFAULT_COIN_MARKET_CAP_GRAPHS_ALL_BETWEEN_RESOURCE_URL, cryptocurrencyResourceUrl, startDate, endDate), GraphsResponse.class);
37+
}
38+
39+
public TickerResponse getCurrentPrice(String cryptocurrencyResourceUrl) {
40+
return this.coinMarketCapTickerRequester.get(String.format(DEFAULT_COIN_MARKET_CAP_TICKER_RESOURCE_URL, cryptocurrencyResourceUrl), TickerResponse[].class)[0];
41+
}
42+
43+
public static class TickerResponse {
44+
45+
private BigDecimal priceUsd;
46+
47+
private BigDecimal priceBtc;
48+
49+
public BigDecimal getPriceUsd() {
50+
return this.priceUsd;
51+
}
52+
53+
public void setPrice_usd(BigDecimal priceUsd) {
54+
this.priceUsd = priceUsd;
55+
}
56+
57+
public BigDecimal getPriceBtc() {
58+
return this.priceBtc;
59+
}
60+
61+
public void setPrice_btc(BigDecimal priceBtc) {
62+
this.priceBtc = priceBtc;
63+
}
64+
}
65+
66+
public static class GraphsResponse {
67+
68+
private List<List<BigInteger>> marketCapByAvailableSupply;
69+
70+
private List<List<BigInteger>> priceBtc;
71+
72+
private List<List<BigInteger>> priceUsd;
73+
74+
private List<List<BigInteger>> volumeUsd;
75+
76+
public List<List<BigInteger>> getMarketCapByAvailableSupply() {
77+
return this.marketCapByAvailableSupply;
78+
}
79+
80+
public void setMarket_cap_by_available_supply(List<List<BigInteger>> marketCapByAvailableSupply) {
81+
this.marketCapByAvailableSupply = marketCapByAvailableSupply;
82+
}
83+
84+
public List<List<BigInteger>> getPriceBtc() {
85+
return this.priceBtc;
86+
}
87+
88+
public void setPrice_btc(List<List<BigInteger>> priceBtc) {
89+
this.priceBtc = priceBtc;
90+
}
91+
92+
public List<List<BigInteger>> getPriceUsd() {
93+
return this.priceUsd;
94+
}
95+
96+
public void setPrice_usd(List<List<BigInteger>> priceUsd) {
97+
this.priceUsd = priceUsd;
98+
}
99+
100+
public List<List<BigInteger>> getVolumeUsd() {
101+
return this.volumeUsd;
102+
}
103+
104+
public void setVolume_usd(List<List<BigInteger>> volumeUsd) {
105+
this.volumeUsd = volumeUsd;
106+
}
107+
}
108+
}

src/main/java/fr/polytech/codev/backend/tasks/AlertTasks.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
import fr.polytech.codev.backend.entities.Alert;
44
import fr.polytech.codev.backend.forms.AlertForm;
5-
import fr.polytech.codev.backend.requesters.JsonStringRestfulRequester;
6-
import fr.polytech.codev.backend.requesters.RestfulRequester;
75
import fr.polytech.codev.backend.services.impl.AlertServices;
6+
import fr.polytech.codev.backend.services.impl.CoinMarketCapServices;
87
import org.slf4j.Logger;
98
import org.slf4j.LoggerFactory;
109
import org.springframework.beans.factory.annotation.Autowired;
@@ -20,31 +19,26 @@
2019
@Component
2120
public 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

Comments
 (0)