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..16a57dc --- /dev/null +++ b/src/main/java/fr/brde/api/controllers/CurrentWeatherController.java @@ -0,0 +1,34 @@ +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.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("/currentWeather") +public class CurrentWeatherController +{ + + @Autowired + CurrentWeatherService currentWeatherService; + + @GetMapping("") + public List list() + { + return this.currentWeatherService.listAllWeathers(); + } + + @CrossOrigin(origins = "http://localhost:8100") + @GetMapping("/last") + public CurrentWeather getLast() + { + return this.currentWeatherService.getLastWeather(); + } + +} 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 new file mode 100644 index 0000000..937f08c --- /dev/null +++ b/src/main/java/fr/brde/api/entities/CurrentWeather.java @@ -0,0 +1,108 @@ +package fr.brde.api.entities; + +import jakarta.persistence.*; + +@Entity +@Table(name = "CurrentWeather") +public class CurrentWeather { + + private int id; + private String time; + + private int temp; // °C + private String lastUpdated; + private String weatherCondition; + private int wind; // km/h + private int feelslike; + + public CurrentWeather() {} + + public CurrentWeather(int id, + String time, + int temp, + String lastUpdated, + String weatherCondition, + int wind, + int feelslike) + { + this.id = id; + this.time = time; + this.temp = temp; + this.lastUpdated = lastUpdated; + this.weatherCondition = weatherCondition; + this.wind = wind; + this.feelslike = feelslike; + } + + @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; + } + + 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/CurrentWeatherRepository.java b/src/main/java/fr/brde/api/repositories/CurrentWeatherRepository.java new file mode 100644 index 0000000..5ffcfbb --- /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/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/CurrentWeatherService.java b/src/main/java/fr/brde/api/services/CurrentWeatherService.java new file mode 100644 index 0000000..e067baf --- /dev/null +++ b/src/main/java/fr/brde/api/services/CurrentWeatherService.java @@ -0,0 +1,37 @@ +package fr.brde.api.services; + +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; + +import java.util.List; + +@Service +@Transactional +public class CurrentWeatherService +{ + + @PersistenceContext + EntityManager entityManager; + + @Autowired + private CurrentWeatherRepository currentWeatherRepository; + + public List listAllWeathers() + { + return this.currentWeatherRepository.findAll(); + } + + 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 query.getSingleResult(); + } +} 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(); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..d92fb8b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,4 @@ - +spring.datasource.url=jdbc:mysql://localhost:3306/weather +spring.datasource.username=username +spring.datasource.password=password +spring.jpa.hibernate.ddl-auto = update \ No newline at end of file