I have written a simple program that retrieves the exchange rate and some basic information about the currency using api(bank).
I would like to add a database to it so that the information is stored.
To do this, I have created a Currency
entity that references a DTO
and a CurrencyRepository
that I have injected into a CurrencyService
.
And at this point I am stuck, I don't really know how I can retrieve the values of code
, mid
, effectiveDate
and save to the database.
Could someone please suggest what I should do to go further?
DTO:CurrencyModelDto
import lombok.Builder;
import lombok.Getter;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDate;
@Getter
@Builder
public class CurrencyModelDto {
private String code;
private float mid;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate effectiveDate;
}
Entity:Currency
import lombok.Getter;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDate;
@Entity
@Getter
public class Currency {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String code;
private float mid;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate effectiveDate;
}
Repository:CurrencyRepository
import com.example.nbpmaster.repository.entity.Currency;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CurrencyRepository extends JpaRepository<Currency, Long> {
}
Service:CurrencyService
import com.example.nbpmaster.model.CurrencyModelDto;
import com.example.nbpmaster.repository.CurrencyRepository;
import com.example.nbpmaster.webclient.currency.CurrencyClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
@RequiredArgsConstructor
public class CurrencyService {
private final CurrencyClient currencyClient;
private final CurrencyRepository currencyRepository;
public CurrencyModelDto getCurrency() {
return currencyClient.getCurrencyType("usd");
}
}
Client:CurrencyClient
import com.example.nbpmaster.model.CurrencyModelDto;
import com.example.nbpmaster.webclient.dto.CurrencyDto;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class CurrencyClient {
public static final String URL_API_NBP = "http://api.nbp.pl/api/";
private final RestTemplate restTemplate = new RestTemplate();
public CurrencyModelDto getCurrencyType(String value) {
CurrencyDto currencyDto = callGetMethod("exchangerates/rates/a/{currency}/", CurrencyDto.class, value);
return CurrencyModelDto.builder()
.code(currencyDto.getCode())
.mid(currencyDto.getRates().stream().findFirst().get().getMid())
.effectiveDate(currencyDto.getRates().stream().findFirst().get().getEffectiveDate())
.build();
}
private <T> T callGetMethod(String url, Class<T> responseType, Object... objects) {
return restTemplate.getForObject(URL_API_NBP url,
responseType, objects);
}
}
CodePudding user response:
You need to map Dto objects with Entities. Use Mapstruct for the purpose. Next configure any Data base in your project and make connection to it. For ease it could be H2 data base. It's better to write set of CRUD REST controllers that will get all needed information from a site accordingly it's api specification and save it to DB. Then in main Spring Boot application class you code some logic applying to the REST controllers.
CodePudding user response:
You can use the ModelMapper
class to convert Dto to Model
and vice versa. ModelMapper
class has map()
which maps Model to Dto and vice versa.
Once you convert the Dto to Model you can call currencyRepository.save()
to save the Model to the database.
You can refer this link.