Skip to content
Open
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
7,945 changes: 7,945 additions & 0 deletions logDetails.log

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;

@SpringBootApplication
@EnableRetry
public class CmCodingChallengeApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.crewmeister.cmcodingchallenge;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

@Value("${rest.template.connection.timeout}")
private int connectionTimeout;

@Value("${rest.template.read.timeout}")
private int readTimeout;

@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(connectionTimeout); // in milliseconds
factory.setReadTimeout(readTimeout); // in milliseconds
RestTemplate restTemplate = new RestTemplate(factory);
restTemplate.getMessageConverters().add(new MappingJackson2XmlHttpMessageConverter());
return restTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.crewmeister.cmcodingchallenge.currency;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotBlank;

@Entity
public class Currency {

@Id
@GeneratedValue
private Long currencyId;

@NotBlank(message = "Currency name must not be blank")
private String currencyName;

public Currency() {
}

public Currency(Long currencyId, String currencyName) {
this.currencyId = currencyId;
this.currencyName = currencyName;
}

public Long getCurrencyId() {
return currencyId;
}

public void setCurrencyId(Long currencyId) {
this.currencyId = currencyId;
}

public String getCurrencyName() {
return currencyName;
}

public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.crewmeister.cmcodingchallenge.currency;

public enum CurrencyConstants {
AUD,
BGN,
BRL,
CAD,
CHF,
CNY,
CYP,
CZK,
DKK,
EEK,
GBP,
GRD,
HKD,
HRK,
HUF,
IDR,
ILS,
INR,
ISK,
JPY,
KRW,
LTL,
LVL,
MTL,
MXN,
MYR,
NOK,
NZD,
PHP,
PLN,
ROL,
RON,
RUB,
SEK,
SGD,
SIT,
SKK,
THB,
TRL,
TRY,
USD,
ZAR

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.crewmeister.cmcodingchallenge.currency;

import javax.validation.Valid;
import java.util.List;

public class CurrencyWrapper {

@Valid
private List<Currency> currencies;

public List<Currency> getCurrencies() {
return currencies;
}

public void setCurrencies(List<Currency> currencies) {
this.currencies = currencies;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.crewmeister.cmcodingchallenge.currencycontroller;

import com.crewmeister.cmcodingchallenge.currency.Currency;
import com.crewmeister.cmcodingchallenge.currency.CurrencyConstants;
import com.crewmeister.cmcodingchallenge.currency.CurrencyWrapper;
import com.crewmeister.cmcodingchallenge.currencydto.FXRequestDto;
import com.crewmeister.cmcodingchallenge.currencyservice.CurrencyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController()
@RequestMapping("/cmfxapi")
public class CurrencyController {

private static final Logger logger = LoggerFactory.getLogger(CurrencyController.class);
@Autowired
CurrencyService currencyService;

@PostMapping("/addCurrencies")
public List<Currency> addCurrency(@RequestBody @Valid CurrencyWrapper currency) {
logger.info("Adding the list of Currency");
if (currency.getCurrencies() == null || currency.getCurrencies().isEmpty()) {
throw new IllegalArgumentException("Currency list must not be null or empty");
}
return currencyService.addCurrency(currency.getCurrencies());
}

@GetMapping("/getAllAvailableCurrencies")
public ResponseEntity<List<String>> getListOfAvailableCurrencies() {
logger.info("Fetching the list of Currencies");
List<Currency> currencies = currencyService.getListOfAvailableCurrencies();
List<String> getCurrencyValues = new ArrayList<>();
if(!currencies.isEmpty())
getCurrencyValues =currencies.stream().map(x -> x.getCurrencyName())
.collect(Collectors.toList());
else { //if db doesnt have data , we have retrieved data from enum
for(CurrencyConstants ccy: CurrencyConstants.values())
getCurrencyValues.add(ccy.name());
}
return new ResponseEntity<List<String>>(getCurrencyValues, HttpStatus.OK);
}

@GetMapping("/getAllFXRates")
public ResponseEntity<Map<String, Map<String,String>>> getAllFXRates(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date, @RequestParam(required = false) String currency) {
logger.info("Fetching the fx rates of Currencies");
Map<String, Map<String,String>> fxRates = currencyService.getFXRates(date != null ? date.toString() : null,currency);
return new ResponseEntity<Map<String, Map<String,String>>>(fxRates, HttpStatus.OK);
}

@GetMapping("/getFXAmount")
public ResponseEntity<Double> getFXAmount(@Valid FXRequestDto request) {
logger.info("Received FX request: {}", request);
double fxAmount = currencyService.getFXAmount(request.getDate().toString(),request.getCurrency().name(), request.getAmount());
return new ResponseEntity<Double>(fxAmount, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.crewmeister.cmcodingchallenge.currencydto;

import com.crewmeister.cmcodingchallenge.currency.CurrencyConstants;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import java.time.LocalDate;

public class FXRequestDto {

@NotNull(message = "Date is required")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate date;

@NotNull(message = "Currency is required")
private CurrencyConstants currency;

@NotNull
@Positive(message = "Amount must be a positive number")
private Double amount;

public LocalDate getDate() {
return date;
}

public void setDate(LocalDate date) {
this.date = date;
}

public CurrencyConstants getCurrency() {
return currency;
}

public void setCurrency(CurrencyConstants currency) {
this.currency = currency;
}

public double getAmount() {
return amount;
}

public void setAmount(double amount) {
this.amount = amount;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.crewmeister.cmcodingchallenge.currencyrepository;


import com.crewmeister.cmcodingchallenge.currency.Currency;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;


public interface CurrencyRepository extends JpaRepository<Currency,Long> {

@Query("SELECT c FROM Currency c WHERE c.currencyName = :currency")
Optional<Currency> findCurrencyName(@Param("currency") String currency);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.crewmeister.cmcodingchallenge.currencyservice;

import com.crewmeister.cmcodingchallenge.currency.Currency;

import java.util.List;
import java.util.Map;


public interface CurrencyService {

public List<Currency> getListOfAvailableCurrencies();

public List<Currency> addCurrency(List<Currency> currency);

public Map<String, Map<String,String>> getFXRates(String date,String currency);

public double getFXAmount(String date, String currency, double amount);
}
Loading