From 10c9c4287fbf90c64934b1df42050d82f7909b15 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Tue, 15 Aug 2023 23:54:11 +0200 Subject: [PATCH 1/3] Add weather model, controller, repository and service --- .../controllers/CurrentWeatherController.java | 26 +++++ .../fr/brde/api/entities/CurrentWeather.java | 96 +++++++++++++++++++ .../CurrentWeatherRepository.java | 11 +++ .../api/services/CurrentWeatherService.java | 29 ++++++ src/main/resources/application.properties | 4 +- 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/main/java/fr/brde/api/controllers/CurrentWeatherController.java create mode 100644 src/main/java/fr/brde/api/entities/CurrentWeather.java create mode 100644 src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java create mode 100644 src/main/java/fr/brde/api/services/CurrentWeatherService.java diff --git a/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java b/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java new file mode 100644 index 0000000..e46ebc3 --- /dev/null +++ b/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java @@ -0,0 +1,26 @@ +package fr.brde.api.controllers; + +import fr.brde.api.entities.CurrentWeather; +import fr.brde.api.services.CurrentWeatherService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/currentWeather") +public class CurrentWeatherController +{ + + @Autowired + CurrentWeatherService currentWeatherService; + + @GetMapping("") + public List list() + { + return this.currentWeatherService.listAllWeathers(); + } + +} diff --git a/src/main/java/fr/brde/api/entities/CurrentWeather.java b/src/main/java/fr/brde/api/entities/CurrentWeather.java new file mode 100644 index 0000000..f211bef --- /dev/null +++ b/src/main/java/fr/brde/api/entities/CurrentWeather.java @@ -0,0 +1,96 @@ +package fr.brde.api.entities; + +import jakarta.persistence.*; + +@Entity +@Table(name = "weather_current") +public class CurrentWeather { + + private int id; + private String time; + + private int temp; // °C + private String lastUpdated; + private String weatherCondition; + private int wind; // km/h + + public CurrentWeather() {} + + public CurrentWeather(int id, + String time, + int temp, + String lastUpdated, + String weatherCondition, + int wind) + { + this.id = id; + this.time = time; + this.temp = temp; + this.lastUpdated = lastUpdated; + this.weatherCondition = weatherCondition; + this.wind = wind; + } + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public int getId() + { + return this.id; + } + + public void setId(int id) + { + this.id = id; + } + + public String getTime() + { + return time; + } + + public void setTime(String time) + { + this.time = time; + } + + public int getTemp() + { + return temp; + } + + public void setTemp(int temp) + { + this.temp = temp; + } + + public String getLastUpdated() + { + return lastUpdated; + } + + public void setLastUpdated(String lastUpdated) + { + this.lastUpdated = lastUpdated; + } + + public String getWeatherCondition() + { + return weatherCondition; + } + + public void setWeatherCondition(String weatherCondition) + { + this.weatherCondition = weatherCondition; + } + + public int getWind() + { + return wind; + } + + public void setWind(int wind) + { + this.wind = wind; + } + +} diff --git a/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java b/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java new file mode 100644 index 0000000..f94cb2b --- /dev/null +++ b/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java @@ -0,0 +1,11 @@ +package fr.brde.api.repositories; + +import fr.brde.api.entities.CurrentWeather; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CurrentWeatherRepository extends JpaRepository +{ + +} diff --git a/src/main/java/fr/brde/api/services/CurrentWeatherService.java b/src/main/java/fr/brde/api/services/CurrentWeatherService.java new file mode 100644 index 0000000..29ad6d5 --- /dev/null +++ b/src/main/java/fr/brde/api/services/CurrentWeatherService.java @@ -0,0 +1,29 @@ +package fr.brde.api.services; + +import fr.brde.api.entities.CurrentWeather; +import fr.brde.api.repositories.CurrentWeatherRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@Transactional +public class CurrentWeatherService +{ + + @Autowired + private CurrentWeatherRepository currentWeatherRepository; + + public List listAllWeathers() + { + return this.currentWeatherRepository.findAll(); + } + + public CurrentWeather getLastWeather(int id) + { + // Change to the last inserted data in DB + return this.currentWeatherRepository.findById(id).get(); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..a347fbc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,3 @@ - +spring.datasource.url=jdbc:mysql://localhost:3306/weather +spring.datasource.username=username +spring.datasource.password=password \ No newline at end of file From 201e61b56ba034212f06c825d8e3ab36418e6325 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Mon, 21 Aug 2023 23:19:33 +0200 Subject: [PATCH 2/3] Add last current weather --- .../api/controllers/CurrentWeatherController.java | 6 ++++++ .../java/fr/brde/api/entities/CurrentWeather.java | 2 +- .../api/repositories/CurrentWeatherRepository.java | 2 +- .../fr/brde/api/services/CurrentWeatherService.java | 12 ++++++++++-- src/main/resources/application.properties | 3 ++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java b/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java index e46ebc3..6a9a8d6 100644 --- a/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java +++ b/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java @@ -23,4 +23,10 @@ public List list() return this.currentWeatherService.listAllWeathers(); } + @GetMapping("/last") + public CurrentWeather getLast() + { + return this.currentWeatherService.getLastWeather(); + } + } diff --git a/src/main/java/fr/brde/api/entities/CurrentWeather.java b/src/main/java/fr/brde/api/entities/CurrentWeather.java index f211bef..208bb03 100644 --- a/src/main/java/fr/brde/api/entities/CurrentWeather.java +++ b/src/main/java/fr/brde/api/entities/CurrentWeather.java @@ -3,7 +3,7 @@ import jakarta.persistence.*; @Entity -@Table(name = "weather_current") +@Table(name = "CurrentWeather") public class CurrentWeather { private int id; diff --git a/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java b/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java index f94cb2b..5ffcfbb 100644 --- a/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java +++ b/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java @@ -5,7 +5,7 @@ import org.springframework.stereotype.Repository; @Repository -public interface CurrentWeatherRepository extends JpaRepository +public interface CurrentWeatherRepository extends JpaRepository { } diff --git a/src/main/java/fr/brde/api/services/CurrentWeatherService.java b/src/main/java/fr/brde/api/services/CurrentWeatherService.java index 29ad6d5..e067baf 100644 --- a/src/main/java/fr/brde/api/services/CurrentWeatherService.java +++ b/src/main/java/fr/brde/api/services/CurrentWeatherService.java @@ -2,6 +2,9 @@ import fr.brde.api.entities.CurrentWeather; import fr.brde.api.repositories.CurrentWeatherRepository; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.persistence.TypedQuery; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -13,6 +16,9 @@ public class CurrentWeatherService { + @PersistenceContext + EntityManager entityManager; + @Autowired private CurrentWeatherRepository currentWeatherRepository; @@ -21,9 +27,11 @@ public List listAllWeathers() return this.currentWeatherRepository.findAll(); } - public CurrentWeather getLastWeather(int id) + public CurrentWeather getLastWeather() { + TypedQuery query = this.entityManager + .createQuery("SELECT a FROM CurrentWeather a ORDER BY id DESC LIMIT 1", CurrentWeather.class); // Change to the last inserted data in DB - return this.currentWeatherRepository.findById(id).get(); + return query.getSingleResult(); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a347fbc..d92fb8b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,4 @@ spring.datasource.url=jdbc:mysql://localhost:3306/weather spring.datasource.username=username -spring.datasource.password=password \ No newline at end of file +spring.datasource.password=password +spring.jpa.hibernate.ddl-auto = update \ No newline at end of file From dea09c7dc1712471db50fa61f60b74726e5f8f8c Mon Sep 17 00:00:00 2001 From: Alexandre Date: Mon, 4 Sep 2023 23:03:09 +0200 Subject: [PATCH 3/3] Add forecast weather --- .../controllers/CurrentWeatherController.java | 2 + .../ForecastWeatherController.java | 34 ++++ .../fr/brde/api/entities/CurrentWeather.java | 14 +- .../fr/brde/api/entities/ForecastWeather.java | 163 ++++++++++++++++++ .../ForecastWeatherRepository.java | 9 + .../api/services/ForecastWeatherService.java | 37 ++++ 6 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 src/main/java/fr/brde/api/controllers/ForecastWeatherController.java create mode 100644 src/main/java/fr/brde/api/entities/ForecastWeather.java create mode 100644 src/main/java/fr/brde/api/repositories/ForecastWeatherRepository.java create mode 100644 src/main/java/fr/brde/api/services/ForecastWeatherService.java diff --git a/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java b/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java index 6a9a8d6..16a57dc 100644 --- a/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java +++ b/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java @@ -3,6 +3,7 @@ import fr.brde.api.entities.CurrentWeather; import fr.brde.api.services.CurrentWeatherService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -23,6 +24,7 @@ public List list() return this.currentWeatherService.listAllWeathers(); } + @CrossOrigin(origins = "http://localhost:8100") @GetMapping("/last") public CurrentWeather getLast() { diff --git a/src/main/java/fr/brde/api/controllers/ForecastWeatherController.java b/src/main/java/fr/brde/api/controllers/ForecastWeatherController.java new file mode 100644 index 0000000..4ad912f --- /dev/null +++ b/src/main/java/fr/brde/api/controllers/ForecastWeatherController.java @@ -0,0 +1,34 @@ +package fr.brde.api.controllers; + +import fr.brde.api.entities.ForecastWeather; +import fr.brde.api.services.ForecastWeatherService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/forecastWeather") +public class ForecastWeatherController +{ + + @Autowired + ForecastWeatherService forecastWeatherService; + + @GetMapping("") + public List list() + { + return this.forecastWeatherService.listAllForecastWeathers(); + } + + @CrossOrigin(originPatterns = "http://localhost:8100") + @GetMapping("/last") + public ForecastWeather getLast() + { + return this.forecastWeatherService.getLastForecastWeather(); + } + +} diff --git a/src/main/java/fr/brde/api/entities/CurrentWeather.java b/src/main/java/fr/brde/api/entities/CurrentWeather.java index 208bb03..937f08c 100644 --- a/src/main/java/fr/brde/api/entities/CurrentWeather.java +++ b/src/main/java/fr/brde/api/entities/CurrentWeather.java @@ -13,6 +13,7 @@ public class CurrentWeather { private String lastUpdated; private String weatherCondition; private int wind; // km/h + private int feelslike; public CurrentWeather() {} @@ -21,7 +22,8 @@ public CurrentWeather(int id, int temp, String lastUpdated, String weatherCondition, - int wind) + int wind, + int feelslike) { this.id = id; this.time = time; @@ -29,6 +31,7 @@ public CurrentWeather(int id, this.lastUpdated = lastUpdated; this.weatherCondition = weatherCondition; this.wind = wind; + this.feelslike = feelslike; } @Id @@ -93,4 +96,13 @@ public void setWind(int wind) this.wind = wind; } + public int getFeelslike() + { + return feelslike; + } + + public void setFeelslike(int feelslike) + { + this.feelslike = feelslike; + } } diff --git a/src/main/java/fr/brde/api/entities/ForecastWeather.java b/src/main/java/fr/brde/api/entities/ForecastWeather.java new file mode 100644 index 0000000..9107777 --- /dev/null +++ b/src/main/java/fr/brde/api/entities/ForecastWeather.java @@ -0,0 +1,163 @@ +package fr.brde.api.entities; + +import jakarta.persistence.*; + +@Entity +@Table(name = "ForecastWeather") +public class ForecastWeather +{ + + private int id; + private String time; + + private int morningTemp; // °C + private int morningWind; // km/h + private String morningWeatherCondition; + + private int afternoonTemp; // °C + private int afternoonWind; // km/h + private String afternoonWeatherCondition; + + private int nightTemp; // °C + private int nightWind; // km/h + private String nightWeatherCondition; + + public ForecastWeather() {} + + public ForecastWeather(int id, + String time, + int morningTemp, + int morningWind, + String morningWeatherCondition, + int afternoonTemp, + int afternoonWind, + String afternoonWeatherCondition, + int nightTemp, + int nightWind, + String nightWeatherCondition) + { + this.id = id; + this.time = time; + this.morningTemp = morningTemp; + this.morningWind = morningWind; + this.morningWeatherCondition = morningWeatherCondition; + this.afternoonTemp = afternoonTemp; + this.afternoonWind = afternoonWind; + this.afternoonWeatherCondition = afternoonWeatherCondition; + this.nightTemp = nightTemp; + this.nightWind = nightWind; + this.nightWeatherCondition = nightWeatherCondition; + } + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public int getId() + { + return this.id; + } + + public void setId(int id) + { + this.id = id; + } + + public String getTime() + { + return time; + } + + public void setTime(String time) + { + this.time = time; + } + + public int getMorningTemp() + { + return morningTemp; + } + + public void setMorningTemp(int morningTemp) + { + this.morningTemp = morningTemp; + } + + public int getMorningWind() + { + return morningWind; + } + + public void setMorningWind(int morningWind) + { + this.morningWind = morningWind; + } + + public String getMorningWeatherCondition() + { + return morningWeatherCondition; + } + + public void setMorningWeatherCondition(String morningWeatherCondition) + { + this.morningWeatherCondition = morningWeatherCondition; + } + + public int getAfternoonTemp() + { + return afternoonTemp; + } + + public void setAfternoonTemp(int afternoonTemp) + { + this.afternoonTemp = afternoonTemp; + } + + public int getAfternoonWind() + { + return afternoonWind; + } + + public void setAfternoonWind(int afternoonWind) + { + this.afternoonWind = afternoonWind; + } + + public String getAfternoonWeatherCondition() + { + return afternoonWeatherCondition; + } + + public void setAfternoonWeatherCondition(String afternoonWeatherCondition) + { + this.afternoonWeatherCondition = afternoonWeatherCondition; + } + + public int getNightTemp() + { + return nightTemp; + } + + public void setNightTemp(int nightTemp) + { + this.nightTemp = nightTemp; + } + + public int getNightWind() + { + return nightWind; + } + + public void setNightWind(int nightWind) + { + this.nightWind = nightWind; + } + + public String getNightWeatherCondition() + { + return nightWeatherCondition; + } + + public void setNightWeatherCondition(String nightWeatherCondition) + { + this.nightWeatherCondition = nightWeatherCondition; + } +} diff --git a/src/main/java/fr/brde/api/repositories/ForecastWeatherRepository.java b/src/main/java/fr/brde/api/repositories/ForecastWeatherRepository.java new file mode 100644 index 0000000..f41792b --- /dev/null +++ b/src/main/java/fr/brde/api/repositories/ForecastWeatherRepository.java @@ -0,0 +1,9 @@ +package fr.brde.api.repositories; + +import fr.brde.api.entities.ForecastWeather; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ForecastWeatherRepository extends JpaRepository +{ } diff --git a/src/main/java/fr/brde/api/services/ForecastWeatherService.java b/src/main/java/fr/brde/api/services/ForecastWeatherService.java new file mode 100644 index 0000000..5d62a45 --- /dev/null +++ b/src/main/java/fr/brde/api/services/ForecastWeatherService.java @@ -0,0 +1,37 @@ +package fr.brde.api.services; + +import fr.brde.api.entities.ForecastWeather; +import fr.brde.api.repositories.ForecastWeatherRepository; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.persistence.TypedQuery; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@Transactional +public class ForecastWeatherService +{ + + @PersistenceContext + EntityManager entityManager; + + @Autowired + private ForecastWeatherRepository forecastWeatherRepository; + + public List listAllForecastWeathers() + { + return this.forecastWeatherRepository.findAll(); + } + + public ForecastWeather getLastForecastWeather() + { + TypedQuery query = this.entityManager + .createQuery("SELECT a FROM ForecastWeather a ORDER BY id DESC LIMIT 1", ForecastWeather.class); + return query.getSingleResult(); + } + +}