-
Notifications
You must be signed in to change notification settings - Fork 0
feat: load stations data at startup #18
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a94b343
build: Add webclient dependencie
SHACRAM 7032842
feat: add WebClient confifuration for external api vlib
SHACRAM 7dcf101
feat: add DTO classes in model folder for parsing API data
SHACRAM 705108b
feat: Create new migration file to add recordId column in station table
SHACRAM 37bb96a
feat: add station filter to verify if stations are already in db
SHACRAM b10ff53
fix: Apply changes of open conversation in pr
SHACRAM 7313bad
chore: make VelibService webClient private
DCodeProg 5f25c29
fix: add null check to duedate.toStringMethod
DCodeProg f2bab8a
chore: add timeout to webclient
DCodeProg 77f0cbc
refactor: use correct java naming convention for getStationCode
DCodeProg db46c46
chore: match parameter and and field name for consistency
DCodeProg 48e2216
fix: correct annotation usage and field naming for lastUpdate in Stat…
DCodeProg 0de530a
fix: setCity setter
DCodeProg fbcb2a1
fix: update station code field naming for consistency
DCodeProg c10f1ad
fix: update station code field naming for consistency
DCodeProg e0c9de1
fix: ensure baseUrl is not null in WebClient configuration
DCodeProg c7ef707
fix: remove unused import for CreationTimestamp in Station class
DCodeProg 75b7b17
fix: rename created_at field to createdAt for consistency
DCodeProg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| flyway.url=jdbc:postgresql://185.166.39.209:5432/vlib_db | ||
| flyway.user=vlibadmin | ||
| flyway.password=T5tUb8DTh2dtMaRymzMyRKwT5kg3xqamdZJjqkLr | ||
| flyway.locations=filesystem:src/main/resources/db/migration | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/fr/host_dcode/vlib/config/WebClientConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package fr.host_dcode.vlib.config; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
|
|
||
| @Configuration | ||
| public class WebClientConfig { | ||
| @Value("${velib.api.base-url}") | ||
| private String baseUrl; | ||
|
|
||
| @Bean | ||
| public WebClient webClient() { | ||
| return WebClient.builder() | ||
| .baseUrl(java.util.Objects.requireNonNull(baseUrl, "velib.api.base-url must not be null")) | ||
| .build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/fr/host_dcode/vlib/model/VelibApiResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package fr.host_dcode.vlib.model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class VelibApiResponse { | ||
|
|
||
| private List<VelibRecord> records; | ||
|
|
||
| public List<VelibRecord> getRecords() { | ||
| return records; | ||
| } | ||
|
|
||
| public void setRecords(List<VelibRecord> records) { | ||
| this.records = records; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package fr.host_dcode.vlib.model; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import java.time.ZonedDateTime; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public class VelibFields { | ||
|
|
||
| @JsonProperty("recordid") | ||
| private String recordId; | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private String name; | ||
| @JsonProperty("stationcode") | ||
| private String stationCode; | ||
| @JsonProperty("nom_arrondissement_communes") | ||
| private String city; | ||
| private List<Double> coordonnees_geo; | ||
| private ZonedDateTime duedate; | ||
|
|
||
|
|
||
| public String getName(){ | ||
| return this.name; | ||
| } | ||
| public String getStationCode(){ | ||
| return this.stationCode; | ||
| } | ||
|
|
||
| public String getDuedate(){ | ||
| return this.duedate != null ? this.duedate.toString() : null; | ||
| } | ||
|
|
||
| public List<Double> getCoordonnees_geo(){ | ||
| return this.coordonnees_geo; | ||
| } | ||
|
|
||
| public String getCity(){ | ||
| return this.city; | ||
| } | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package fr.host_dcode.vlib.model; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| public class VelibRecord { | ||
|
|
||
| private VelibFields fields; | ||
| @JsonProperty("recordid") | ||
| private String recordId; | ||
|
|
||
| public VelibFields getFields() { | ||
| return fields; | ||
| } | ||
|
|
||
| public void setFields(VelibFields fields) { | ||
| this.fields = fields; | ||
| } | ||
|
|
||
| public String getRecordId(){ | ||
| return this.recordId; | ||
| } | ||
|
|
||
| public void setRecordId(String recordId) { | ||
| this.recordId = recordId; | ||
| } | ||
|
|
||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/fr/host_dcode/vlib/repository/StationRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package fr.host_dcode.vlib.repository; | ||
|
|
||
| import fr.host_dcode.vlib.model.Station; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface StationRepository extends JpaRepository<Station, String> { | ||
|
|
||
| Station findByStationCode(String station_code); | ||
|
|
||
| } |
85 changes: 85 additions & 0 deletions
85
src/main/java/fr/host_dcode/vlib/service/VelibService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package fr.host_dcode.vlib.service; | ||
|
|
||
| import fr.host_dcode.vlib.model.Station; | ||
| import fr.host_dcode.vlib.model.VelibApiResponse; | ||
| import fr.host_dcode.vlib.model.VelibFields; | ||
| import fr.host_dcode.vlib.repository.StationRepository; | ||
| import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
| import org.springframework.context.event.EventListener; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.web.reactive.function.client.WebClient; | ||
|
|
||
| import java.util.List; | ||
|
|
||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Service | ||
| public class VelibService { | ||
| private final WebClient webClient; | ||
| private final StationRepository stationRepository; | ||
|
|
||
| public VelibService(WebClient webClient, StationRepository stationRepository) { | ||
| this.webClient = webClient; | ||
| this.stationRepository = stationRepository; | ||
| } | ||
|
|
||
| public void fetchAndSaveVelibData() { | ||
|
|
||
| String queryUri = "?dataset=velib-disponibilite-en-temps-reel&rows=50"; | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| VelibApiResponse apiResponse = webClient.get() | ||
| .uri(queryUri) | ||
| .retrieve() | ||
| .bodyToMono(VelibApiResponse.class) | ||
| .block(java.time.Duration.ofSeconds(30)); | ||
|
|
||
| if (apiResponse == null || apiResponse.getRecords() == null || apiResponse.getRecords().isEmpty()) { | ||
| System.out.println("Aucune donnée Velib reçue."); | ||
SHACRAM marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| apiResponse.getRecords().stream() | ||
| .map(record -> { | ||
| VelibFields fields = record.getFields(); | ||
|
|
||
| List<Double> coords = fields.getCoordonnees_geo(); | ||
| double latitude = (coords != null && coords.size() >= 1) ? coords.get(0) : 0.0; | ||
| double longitude = (coords != null && coords.size() >= 2) ? coords.get(1) : 0.0; | ||
|
|
||
| String description = fields.getName() + ". Dernière mise à jour: " + (fields.getDuedate() != null ? fields.getDuedate() : "inconnue"); | ||
|
|
||
| Station station = new Station( | ||
| fields.getName(), | ||
| record.getRecordId(), | ||
| fields.getStationCode(), | ||
| fields.getCity(), | ||
| latitude, | ||
| longitude, | ||
| description | ||
| ); | ||
|
|
||
| return station; | ||
| }) | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .forEach(station ->{ | ||
| Station existingStation = stationRepository.findByStationCode(station.getStationCode()); | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if(existingStation == null){ | ||
| stationRepository.save(station); | ||
| } else { | ||
| existingStation.setName(station.getName()); | ||
| //ADD ALL UPDATES | ||
| stationRepository.save(existingStation); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| @EventListener(ApplicationReadyEvent.class) | ||
| public void runAfterStartup() { | ||
| System.out.println("🚀 Démarrage de l'application détecté. Lancement de la récupération des données Velib..."); | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| fetchAndSaveVelibData(); | ||
| System.out.println("✅ Récupération et sauvegarde initiales terminées."); | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (Exception e) { | ||
| System.err.println("❌ Erreur critique lors de la récupération initiale des données Velib : " + e.getMessage()); | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
2 changes: 2 additions & 0 deletions
2
src/main/resources/db/migration/V2__Alter_table_station_to_add_recordId_column.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ALTER TABLE station | ||
| ADD COLUMN recordId VARCHAR(100) NOT NULL; | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
6 changes: 6 additions & 0 deletions
6
src/main/resources/db/migration/V3__Alter_tables_users_and_station_to_change_id_type.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ALTER TABLE users | ||
| ALTER COLUMN id TYPE VARCHAR(40) USING id::VARCHAR(40); | ||
|
|
||
|
|
||
| ALTER TABLE station | ||
| ALTER COLUMN id TYPE VARCHAR(40) USING id::VARCHAR(40); | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
2 changes: 2 additions & 0 deletions
2
src/main/resources/db/migration/V4__Delete_address_column_in_table_station.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ALTER TABLE station | ||
| DROP COLUMN address; | ||
DCodeProg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.