Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev): implement jooq integration #4

Open
wants to merge 3 commits into
base: feature/gradle-migration
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
тз
.idea
.idea
/transaction-service/transaction-model/src/main/java/com/example/transaction/model/jooq
/currency-service/currency-model/src/main/java/com/example/transaction/model/jooq
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ subprojects {

dependencies {
implementation("com.google.code.gson:gson:2.11.0")

compileOnly("org.projectlombok:lombok:1.18.24")
annotationProcessor("org.projectlombok:lombok:1.18.24")
annotationProcessor("org.projectlombok:lombok:1.18.30")

testImplementation("junit:junit:4.13.1")
testImplementation("org.springframework.boot:spring-boot-test:1.4.0.RELEASE")
}
}
4 changes: 2 additions & 2 deletions config-server/src/main/resources/config/currency-service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ spring:
url: jdbc:mysql://localhost:3306/${db.schema}
username: ${db.username}
password: ${db.password}
driver-class-name: com.mysql.jdbc.Driver
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
show-sql: true
hibernate.ddl-auto: create-drop
hibernate.ddl-auto: update
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ spring:
jpa:
database-platform: org.hibernate.dialect.MySQLDialect
show-sql: true
hibernate.ddl-auto: create-drop
hibernate.ddl-auto: update
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.example.currencyservice.model;
package com.example.currency.model.dto;

import jakarta.persistence.*;
import lombok.*;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.currencyservice.model;
package com.example.currency.model.entity;

import com.example.currencyservice.model.dto.CurrencyRequest;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -9,7 +8,7 @@
import java.math.BigDecimal;

@Component
@Entity
@Entity @Table(name = "currencies")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @ToString
public class Currency {
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package com.example.currencyservice.model.dto;
package com.example.currency.model.entity;

import com.example.currencyservice.model.Currency;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import java.util.List;
import lombok.*;
import org.hibernate.annotations.Cascade;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Component
@Entity
@Entity @Table(name = "currency_requests")
@Getter @Setter @NoArgsConstructor @AllArgsConstructor @ToString
public class CurrencyRequest {
@Id
Expand All @@ -30,6 +25,8 @@ public class CurrencyRequest {
cascade = CascadeType.ALL,
fetch = FetchType.EAGER
)
@JsonBackReference // Используется для обратной ссылки, не будет сериализоваться
/** Используется для обратной ссылки, не будет сериализоваться
* */
@JsonBackReference
private List<Currency> currencyList;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
description = "добавлю позже"

dependencies {
implementation project(":currency-service:currency-model")

implementation("org.springframework.boot:spring-boot-starter-actuator:3.3.3")
implementation("org.springframework.boot:spring-boot-starter-web:3.3.3")
implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:4.1.3")
Expand All @@ -10,4 +12,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter:3.3.3")
implementation("org.springframework.boot:spring-boot-starter-webflux:3.3.3")
implementation("org.springframework:spring-context:6.1.12")

//jooq
implementation("org.jooq:jooq:3.19.11")
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.example.currencyservice;
package com.example.currency;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
//@EnableDiscoveryClient
public class CurrencyServiceApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.currencyservice.config;
package com.example.currency.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.example.currencyservice.controller;
package com.example.currency.controller;

import com.example.currencyservice.model.Currency;
import com.example.currencyservice.model.CurrencyApiResponse;
import com.example.currencyservice.model.dto.CurrencyRequest;
import com.example.currencyservice.service.CurrencyService;
import com.example.currencyservice.service.implement.CurrencyServiceImplement;

import com.example.currency.model.entity.CurrencyRequest;
import com.example.currency.model.entity.Currency;
import com.example.currency.service.CurrencyService;
import com.example.currency.service.impl.CurrencyServiceImplement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;
Expand All @@ -15,15 +15,15 @@
@RestController
@RequestMapping("/currency-service/api/currencies")
public class CurrencyController {
private CurrencyService service;
private final CurrencyService service;

@Autowired
public CurrencyController(CurrencyServiceImplement service) {
this.service = service;
}

@GetMapping("/{dateTime}")
public Mono<List<Currency>> getCurrencyList(@PathVariable ZonedDateTime dateTime) {
public Mono<List<Currency>> getCurrencyList(@PathVariable("dateTime") ZonedDateTime dateTime) {
return service.getCurrencyList(dateTime);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.currency.repository;

import com.example.currency.model.entity.Currency;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CurrencyRepositoryJPA extends CrudRepository<Currency, Long> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.currency.repository;

import com.example.currency.model.entity.CurrencyRequest;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CurrencyRequestRepositoryJPA extends CrudRepository<CurrencyRequest, Long> {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.currencyservice.service;
package com.example.currency.service;

import com.example.currencyservice.model.Currency;
import com.example.currencyservice.model.CurrencyApiResponse;
import com.example.currencyservice.model.dto.CurrencyRequest;
import com.example.currency.model.entity.CurrencyRequest;
import com.example.currency.model.entity.Currency;
import com.example.currency.model.dto.CurrencyApiResponse;
import reactor.core.publisher.Mono;

import java.time.ZonedDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.example.currencyservice.service.implement;

import com.example.currencyservice.external.OpenExchangeRatesClient;
import com.example.currencyservice.model.CurrencyApiResponse;
import com.example.currencyservice.model.Currency;
import com.example.currencyservice.model.dto.CurrencyRequest;
import com.example.currencyservice.repository.CurrencyRepository;
import com.example.currencyservice.repository.CurrencyRequestRepository;
import com.example.currencyservice.service.CurrencyService;
import com.example.currencyservice.service.implement.utils.CurrencyServiceUtils;
package com.example.currency.service.impl;

import com.example.currency.repository.CurrencyRepositoryJOOQ;
import com.example.currency.repository.CurrencyRepositoryJPA;
import com.example.currency.repository.CurrencyRequestRepositoryJOOQ;
import com.example.currency.webclient.OpenExchangeRatesClient;
import com.example.currency.model.entity.CurrencyRequest;
import com.example.currency.model.entity.Currency;
import com.example.currency.model.dto.CurrencyApiResponse;
import com.example.currency.repository.CurrencyRequestRepositoryJPA;
import com.example.currency.service.CurrencyService;
import com.example.currency.service.impl.utils.CurrencyServiceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;
Expand All @@ -19,20 +20,28 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CurrencyServiceImplement implements CurrencyService {
private CurrencyRepository currencyRepository;
private CurrencyRequestRepository requestRepository;
private final CurrencyRepositoryJPA currencyRepositoryJPA;
private final CurrencyRepositoryJOOQ currencyRepositoryJOOQ;
private final CurrencyRequestRepositoryJPA requestRepositoryJPA;
private final CurrencyRequestRepositoryJOOQ requestRepositoryJOOQ;
private final OpenExchangeRatesClient openExchangeRatesClient;

private List<Currency> currencyList = new ArrayList<>();

@Autowired
public CurrencyServiceImplement(CurrencyRepository currencyRepository, CurrencyRequestRepository requestRepository, OpenExchangeRatesClient openExchangeRatesClient) {
this.currencyRepository = currencyRepository;
this.requestRepository = requestRepository;
public CurrencyServiceImplement(
CurrencyRepositoryJPA currencyRepositoryJPA,
CurrencyRepositoryJOOQ currencyRepositoryJOOQ,
CurrencyRequestRepositoryJPA requestRepositoryJPA, CurrencyRequestRepositoryJOOQ requestRepositoryJOOQ,
OpenExchangeRatesClient openExchangeRatesClient
) {
this.currencyRepositoryJPA = currencyRepositoryJPA;
this.currencyRepositoryJOOQ = currencyRepositoryJOOQ;
this.requestRepositoryJPA = requestRepositoryJPA;
this.requestRepositoryJOOQ = requestRepositoryJOOQ;
this.openExchangeRatesClient = openExchangeRatesClient;
}

Expand All @@ -43,12 +52,13 @@ public Mono<List<Currency>> getCurrencyList(ZonedDateTime transaction_dateTime)
LocalDate parsedCurrentDate = LocalDate.parse(currentDate_formatted);
LocalDate parsedTransactionDate = LocalDate.parse(transactionDate_formatted);

CurrencyRequest pastCurrencyRequest = requestRepository.findByFormatted_timestamp(transactionDate_formatted);
CurrencyRequest pastCurrencyRequest =
requestRepositoryJOOQ.findByFormatted_timestamp(transactionDate_formatted);
if (pastCurrencyRequest != null) {
System.out.println("Get data from local db!!! (currencyList at:" + pastCurrencyRequest.getFormatted_timestamp() + ")");

// Список валют взят из БД (запрос в openexchangerates.org/api/ НЕ делается)
currencyList = currencyRepository.findAllByCurrencyRequestID(pastCurrencyRequest.getId());
currencyList = currencyRepositoryJOOQ.findAllByCurrencyRequestID(pastCurrencyRequest.getId());
return checkForUnavailableRate(transaction_dateTime, currencyList);
} else {
return Mono.defer(() -> {
Expand Down Expand Up @@ -79,7 +89,7 @@ public Mono<List<Currency>> getCurrencyList(ZonedDateTime transaction_dateTime)
public Mono<List<Currency>> createCurrenciesFromResponse(Mono<CurrencyApiResponse> responseMono) {
return responseMono.flatMap(response -> {
CurrencyRequest currencyRequest = getCurrencyRequest(response);
requestRepository.save(currencyRequest);
requestRepositoryJPA.save(currencyRequest);

// Получаем список валют и сохраняем их в бд
Map<String, BigDecimal> currencyRates = response.getRates();
Expand All @@ -91,7 +101,7 @@ public Mono<List<Currency>> createCurrenciesFromResponse(Mono<CurrencyApiRespons
currencyList.add(currency);
}
currencyList.forEach(currency -> currency.setCurrencyRequest(currencyRequest));
currencyRepository.saveAll(currencyList);
currencyRepositoryJPA.saveAll(currencyList);
return Mono.just(currencyList);
});
}
Expand Down Expand Up @@ -130,12 +140,12 @@ public Mono<List<Currency>> checkForUnavailableRate(ZonedDateTime transaction_da

@Override
public List<CurrencyRequest> getPastRequestList() {
return (List<CurrencyRequest>) requestRepository.findAll();
return (List<CurrencyRequest>) requestRepositoryJPA.findAll();
}

@Override
public List<Currency> getPastCurrencyList() {
return (List<Currency>) currencyRepository.findAll();
return (List<Currency>) currencyRepositoryJPA.findAll();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.currencyservice.service.implement.utils;
package com.example.currency.service.impl.utils;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.currencyservice.external;
package com.example.currency.webclient;

import com.example.currencyservice.model.CurrencyApiResponse;
import com.example.currency.model.dto.CurrencyApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.currency;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class CurrencyServiceApplicationTests {

@Test
public void contextLoads() {
}

}

This file was deleted.

This file was deleted.

This file was deleted.

Loading