From 9a7b9b4ba7ac0dfd42758d7ea306982e5514c8a8 Mon Sep 17 00:00:00 2001 From: github-actions Date: Fri, 15 Nov 2024 20:57:34 +0100 Subject: [PATCH 1/2] [Spring boot config] -added group of folders -created basic examples (model, endpoints, db) -configuration of mysql Dodano modele, interfejsy i repo do spirnga Added models, interfaces and repo in spring. Added models, interfaces and repo in spring. --- .github/workflows/spring_ci.yml | 65 ++++++++ .github/workflows/spring_format_check.yml | 29 ---- .github/workflows/spring_mvn_test.yml | 29 ---- .gitignore | 2 + .vscode/launch.json | 14 ++ .vscode/settings.json | 3 + spring/README.md | 17 ++ spring/pom.xml | 151 ++++++++++-------- .../pogoda/weather/controller/Controller.java | 44 +++++ .../pogoda/weather/data/EspAlertsRepo.java | 45 ++++++ .../pogoda/weather/data/EspLanguagesRepo.java | 45 ++++++ .../weather/data/EspMeasureUnitsRepo.java | 45 ++++++ .../weather/data/EspMeasurementsRepo.java | 46 ++++++ .../weather/data/EspUserSettingsRepo.java | 36 +++++ .../weather/data/EspUsersAlertRepo.java | 36 +++++ .../com/pogoda/weather/data/EspUsersRepo.java | 32 ++++ .../weather/errors/IllegalParmException.java | 14 ++ .../weather/errors/NotFoundException.java | 16 ++ .../interfaces/IEspAlertsRepository.java | 16 ++ .../interfaces/IEspLanguagesRepository.java | 16 ++ .../IEspMeasureUnitsRepository.java | 16 ++ .../IEspMeasurementsReposytory.java | 25 +++ .../IEspUserSettingsRepository.java | 14 ++ .../interfaces/IEspUsersAlertRepository.java | 21 +++ .../interfaces/IEspUsersRepository.java | 12 ++ .../com/pogoda/weather/model/EspAlerts.java | 30 ++++ .../pogoda/weather/model/EspLanguages.java | 28 ++++ .../pogoda/weather/model/EspMeasureUnits.java | 27 ++++ .../pogoda/weather/model/EspMeasurements.java | 35 ++++ .../pogoda/weather/model/EspUserSettings.java | 34 ++++ .../com/pogoda/weather/model/EspUsers.java | 30 ++++ .../pogoda/weather/model/EspUsersAlert.java | 27 ++++ .../com/pogoda/weather/services/Service.java | 16 ++ .../src/main/resources/application.properties | 9 ++ 34 files changed, 904 insertions(+), 121 deletions(-) create mode 100644 .github/workflows/spring_ci.yml delete mode 100644 .github/workflows/spring_format_check.yml delete mode 100644 .github/workflows/spring_mvn_test.yml create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 spring/src/main/java/com/pogoda/weather/controller/Controller.java create mode 100644 spring/src/main/java/com/pogoda/weather/data/EspAlertsRepo.java create mode 100644 spring/src/main/java/com/pogoda/weather/data/EspLanguagesRepo.java create mode 100644 spring/src/main/java/com/pogoda/weather/data/EspMeasureUnitsRepo.java create mode 100644 spring/src/main/java/com/pogoda/weather/data/EspMeasurementsRepo.java create mode 100644 spring/src/main/java/com/pogoda/weather/data/EspUserSettingsRepo.java create mode 100644 spring/src/main/java/com/pogoda/weather/data/EspUsersAlertRepo.java create mode 100644 spring/src/main/java/com/pogoda/weather/data/EspUsersRepo.java create mode 100644 spring/src/main/java/com/pogoda/weather/errors/IllegalParmException.java create mode 100644 spring/src/main/java/com/pogoda/weather/errors/NotFoundException.java create mode 100644 spring/src/main/java/com/pogoda/weather/interfaces/IEspAlertsRepository.java create mode 100644 spring/src/main/java/com/pogoda/weather/interfaces/IEspLanguagesRepository.java create mode 100644 spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasureUnitsRepository.java create mode 100644 spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasurementsReposytory.java create mode 100644 spring/src/main/java/com/pogoda/weather/interfaces/IEspUserSettingsRepository.java create mode 100644 spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersAlertRepository.java create mode 100644 spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersRepository.java create mode 100644 spring/src/main/java/com/pogoda/weather/model/EspAlerts.java create mode 100644 spring/src/main/java/com/pogoda/weather/model/EspLanguages.java create mode 100644 spring/src/main/java/com/pogoda/weather/model/EspMeasureUnits.java create mode 100644 spring/src/main/java/com/pogoda/weather/model/EspMeasurements.java create mode 100644 spring/src/main/java/com/pogoda/weather/model/EspUserSettings.java create mode 100644 spring/src/main/java/com/pogoda/weather/model/EspUsers.java create mode 100644 spring/src/main/java/com/pogoda/weather/model/EspUsersAlert.java create mode 100644 spring/src/main/java/com/pogoda/weather/services/Service.java diff --git a/.github/workflows/spring_ci.yml b/.github/workflows/spring_ci.yml new file mode 100644 index 0000000..8b07649 --- /dev/null +++ b/.github/workflows/spring_ci.yml @@ -0,0 +1,65 @@ +name: Spring Boot CI/CD Pipeline + +on: + push: + branches: + - main + paths: + - spring/** + pull_request: + paths: + - spring/** + +jobs: + test: + runs-on: ubuntu-latest + + services: + mysql: + image: mysql:8.0 + env: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: Weather + ports: + - 3306:3306 + options: --health-cmd="mysqladmin ping --host 127.0.0.1 --silent" --health-interval=10s --health-timeout=5s --health-retries=3 + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up JDK 17 + uses: actions/setup-java@v2 + with: + java-version: '17' + distribution: 'temurin' + cache: maven + + - name: Cache Maven dependencies + uses: actions/cache@v2 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + + - name: Build with Maven + working-directory: ./spring + run: mvn clean install -DskipTests + + + - name: Run mvn tests + working-directory: ./spring + run: mvn test + env: + SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/Weather + SPRING_DATASOURCE_USERNAME: root + SPRING_DATASOURCE_PASSWORD: root + + - name: Spring format check + working-directory: ./spring + run: mvn formatter:validate + env: + SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/Weather + SPRING_DATASOURCE_USERNAME: root + SPRING_DATASOURCE_PASSWORD: root diff --git a/.github/workflows/spring_format_check.yml b/.github/workflows/spring_format_check.yml deleted file mode 100644 index f0c3765..0000000 --- a/.github/workflows/spring_format_check.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Spring format check pipeline - -on: - push: - branches: - - main - paths: - - spring/** - pull_request: - paths: - - spring/** - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - java-version: '17' - distribution: 'temurin' - cache: maven - - - name: Spring format check - working-directory: ./spring - run: mvn formatter:validate diff --git a/.github/workflows/spring_mvn_test.yml b/.github/workflows/spring_mvn_test.yml deleted file mode 100644 index 358e3fb..0000000 --- a/.github/workflows/spring_mvn_test.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: Spring mvn tests pipeline - -on: - push: - branches: - - main - paths: - - spring/** - pull_request: - paths: - - spring/** - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - java-version: '17' - distribution: 'temurin' - cache: maven - - - name: Run mvn tests - working-directory: ./spring - run: mvn test diff --git a/.gitignore b/.gitignore index 6b536b7..aac2728 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ spring/*.rar # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* + +.vscode diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d9b00f8 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "type": "java", + "name": "Spring Boot-WeatherApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.pogoda.weather.WeatherApplication", + "projectName": "weather", + "args": "", + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c5f3f6b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/spring/README.md b/spring/README.md index e69de29..a2838ff 100644 --- a/spring/README.md +++ b/spring/README.md @@ -0,0 +1,17 @@ +CREATE DATABASE Weather; +CREATE USER 'WeatherServer'@'localhost' IDENTIFIED BY 't4jn3h4sL0'; +GRANT ALL PRIVILEGES ON Weather.* TO 'WeatherServer'@'localhost'; +FLUSH PRIVILEGES; +SHOW GRANTS FOR 'WeatherServer'@'localhost'; + + + +odpalanie: mvn spring-boot:run + +dopisanie w controller wiecej getow (i wiecej metod w ) + +stworzenie w model nowych obiektow (tych co jest w projekcie bazy danych) +do nich wlasne interface +i wlasne repa w data + + diff --git a/spring/pom.xml b/spring/pom.xml index d8e1557..69b7742 100644 --- a/spring/pom.xml +++ b/spring/pom.xml @@ -1,64 +1,91 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 3.3.5 - - - com.pogoda - weather - 0.0.1-SNAPSHOT - weather - Backend for data from esp - - - - - - - - - - - - - - - 17 - - - - org.springframework.boot - spring-boot-starter-web - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.5 + + + com.pogoda + weather + 0.0.1-SNAPSHOT + weather + Backend for data from esp + + + 17 + + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + org.springframework.boot + spring-boot-starter-web + + - com.mysql - mysql-connector-j + mysql + mysql-connector-java + 8.0.33 runtime - - org.projectlombok - lombok - true - - - org.springframework.boot - spring-boot-starter-test - test - - - - + + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + + jakarta.persistence + jakarta.persistence-api + 3.1.0 + + + + + org.projectlombok + lombok + true + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + net.revelc.code.formatter formatter-maven-plugin 2.24.1 + UTF-8 @@ -69,19 +96,17 @@ - - org.springframework.boot - spring-boot-maven-plugin - - - - org.projectlombok - lombok - - - - - - + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + 17 + 17 + + + + diff --git a/spring/src/main/java/com/pogoda/weather/controller/Controller.java b/spring/src/main/java/com/pogoda/weather/controller/Controller.java new file mode 100644 index 0000000..557a4f1 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/controller/Controller.java @@ -0,0 +1,44 @@ +package com.pogoda.weather.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.pogoda.weather.data.EspMeasurementsRepo; +import com.pogoda.weather.model.EspMeasurements; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +@RestController +@RequestMapping("/weather") +public class Controller { + + @Autowired + EspMeasurementsRepo espDataRepo; + + @GetMapping("/test") + public String aja() { + return "cos tam"; + } + + @PostMapping("/test") + public void ajaa() { + System.out.println("DOSTALEM"); + } + + @PostMapping("/measurments") + public ResponseEntity zapis(@RequestBody EspMeasurements espMeasurements) { + return ResponseEntity.ok(espDataRepo.saveMeasurements(espMeasurements)); + } + + @GetMapping("/measurments/{id}") + public ResponseEntity odczyty(@PathVariable String id) { + return ResponseEntity.ok(espDataRepo.getMeasurements(id)); + } +} diff --git a/spring/src/main/java/com/pogoda/weather/data/EspAlertsRepo.java b/spring/src/main/java/com/pogoda/weather/data/EspAlertsRepo.java new file mode 100644 index 0000000..0eb1f29 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/data/EspAlertsRepo.java @@ -0,0 +1,45 @@ +package com.pogoda.weather.data; + +import org.springframework.stereotype.Repository; +import com.pogoda.weather.interfaces.IEspAlertsRepository; +import com.pogoda.weather.model.EspAlerts; + +@Repository +public class EspAlertsRepo { + + private final IEspAlertsRepository espAlertsRepository; + + public EspAlertsRepo(IEspAlertsRepository espAlertsRepository) { + this.espAlertsRepository = espAlertsRepository; + } + + public EspAlerts saveAlert(EspAlerts espAlerts) { + return espAlertsRepository.save(espAlerts); + } + + public EspAlerts getAlertByAlertId(String alertId) { + return espAlertsRepository.findByAlertId(alertId) + .orElseThrow(() -> new RuntimeException("Alert not found with alertId: " + alertId)); + } + + public EspAlerts getAlertByAlertType(String alertType) { + return espAlertsRepository.findByAlertType(alertType) + .orElseThrow(() -> new RuntimeException("Alert not found with alertType: " + alertType)); + } + + public boolean alertExistsByAlertId(String alertId) { + return espAlertsRepository.existsByAlertId(alertId); + } + + public boolean alertExistsByAlertType(String alertType) { + return espAlertsRepository.existsByAlertType(alertType); + } + + public void deleteAlertByAlertId(String alertId) { + espAlertsRepository.findByAlertId(alertId).ifPresent(espAlertsRepository::delete); + } + + public void deleteAlertByAlertType(String alertType) { + espAlertsRepository.findByAlertType(alertType).ifPresent(espAlertsRepository::delete); + } +} diff --git a/spring/src/main/java/com/pogoda/weather/data/EspLanguagesRepo.java b/spring/src/main/java/com/pogoda/weather/data/EspLanguagesRepo.java new file mode 100644 index 0000000..6b2a98e --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/data/EspLanguagesRepo.java @@ -0,0 +1,45 @@ +package com.pogoda.weather.data; + +import org.springframework.stereotype.Repository; +import com.pogoda.weather.interfaces.IEspLanguagesRepository; +import com.pogoda.weather.model.EspLanguages; + +@Repository +public class EspLanguagesRepo { + + private final IEspLanguagesRepository espLanguagesRepository; + + public EspLanguagesRepo(IEspLanguagesRepository espLanguagesRepository) { + this.espLanguagesRepository = espLanguagesRepository; + } + + public EspLanguages saveLanguage(EspLanguages espLanguages) { + return espLanguagesRepository.save(espLanguages); + } + + public EspLanguages getLanguageByLangId(String langId) { + return espLanguagesRepository.findByLangId(langId) + .orElseThrow(() -> new RuntimeException("Language not found with langId: " + langId)); + } + + public EspLanguages getLanguageByName(String name) { + return espLanguagesRepository.findByName(name) + .orElseThrow(() -> new RuntimeException("Language not found with name: " + name)); + } + + public boolean languageExistsByLangId(String langId) { + return espLanguagesRepository.existsByLangId(langId); + } + + public boolean languageExistsByName(String name) { + return espLanguagesRepository.existsByName(name); + } + + public void deleteLanguageByLangId(String langId) { + espLanguagesRepository.findByLangId(langId).ifPresent(espLanguagesRepository::delete); + } + + public void deleteLanguageByName(String name) { + espLanguagesRepository.findByName(name).ifPresent(espLanguagesRepository::delete); + } +} diff --git a/spring/src/main/java/com/pogoda/weather/data/EspMeasureUnitsRepo.java b/spring/src/main/java/com/pogoda/weather/data/EspMeasureUnitsRepo.java new file mode 100644 index 0000000..b27b0dc --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/data/EspMeasureUnitsRepo.java @@ -0,0 +1,45 @@ +package com.pogoda.weather.data; + +import org.springframework.stereotype.Repository; +import com.pogoda.weather.interfaces.IEspMeasureUnitsRepository; +import com.pogoda.weather.model.EspMeasureUnits; + +@Repository +public class EspMeasureUnitsRepo { + + private final IEspMeasureUnitsRepository espMeasureUnitsRepository; + + public EspMeasureUnitsRepo(IEspMeasureUnitsRepository espMeasureUnitsRepository) { + this.espMeasureUnitsRepository = espMeasureUnitsRepository; + } + + public EspMeasureUnits saveUnit(EspMeasureUnits espMeasureUnits) { + return espMeasureUnitsRepository.save(espMeasureUnits); + } + + public EspMeasureUnits getUnitByUnitId(String unitId) { + return espMeasureUnitsRepository.findByUnitId(unitId) + .orElseThrow(() -> new RuntimeException("Unit not found with unitId: " + unitId)); + } + + public EspMeasureUnits getUnitByName(String name) { + return espMeasureUnitsRepository.findByName(name) + .orElseThrow(() -> new RuntimeException("Unit not found with name: " + name)); + } + + public boolean unitExistsByUnitId(String unitId) { + return espMeasureUnitsRepository.existsByUnitId(unitId); + } + + public boolean unitExistsByName(String name) { + return espMeasureUnitsRepository.existsByName(name); + } + + public void deleteUnitByUnitId(String unitId) { + espMeasureUnitsRepository.findByUnitId(unitId).ifPresent(espMeasureUnitsRepository::delete); + } + + public void deleteUnitByName(String name) { + espMeasureUnitsRepository.findByName(name).ifPresent(espMeasureUnitsRepository::delete); + } +} diff --git a/spring/src/main/java/com/pogoda/weather/data/EspMeasurementsRepo.java b/spring/src/main/java/com/pogoda/weather/data/EspMeasurementsRepo.java new file mode 100644 index 0000000..4f88a28 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/data/EspMeasurementsRepo.java @@ -0,0 +1,46 @@ +package com.pogoda.weather.data; + +import org.springframework.stereotype.Repository; +import com.pogoda.weather.interfaces.IEspMeasurementsReposytory; +import com.pogoda.weather.model.EspMeasurements; + + +@Repository +public class EspMeasurementsRepo { + + private final IEspMeasurementsReposytory espMeasurementsReposytory; + + public EspMeasurementsRepo(IEspMeasurementsReposytory espMeasurementsReposytory) { + this.espMeasurementsReposytory = espMeasurementsReposytory; + } + + public EspMeasurements saveMeasurements(EspMeasurements espMeasurements) { + return espMeasurementsReposytory.save(espMeasurements); + } + + public EspMeasurements getMeasurements(String id) { + return espMeasurementsReposytory.findById(id) + .orElseThrow(() -> new RuntimeException("Measurement not found with id: " + id)); + } + + public Iterable getMeasurementsByPressure(int pressure) { + return espMeasurementsReposytory.findByPressure1(pressure); + } + + public Iterable getMeasurementsByTemperature(int temperature) { + return espMeasurementsReposytory.findByTemperature1(temperature); + } + + public void deleteMeasurement(String id) { + espMeasurementsReposytory.deleteById(id); + } + + public void deleteMeasurement(EspMeasurements espMeasurements) { + espMeasurementsReposytory.delete(espMeasurements); + } + + public void deleteAllMeasurements() { + espMeasurementsReposytory.deleteAll(); + } + +} diff --git a/spring/src/main/java/com/pogoda/weather/data/EspUserSettingsRepo.java b/spring/src/main/java/com/pogoda/weather/data/EspUserSettingsRepo.java new file mode 100644 index 0000000..fb6f4b3 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/data/EspUserSettingsRepo.java @@ -0,0 +1,36 @@ +package com.pogoda.weather.data; + +import org.springframework.stereotype.Repository; +import com.pogoda.weather.interfaces.IEspUserSettingsRepository; +import com.pogoda.weather.model.EspUserSettings; + +@Repository +public class EspUserSettingsRepo { + + private final IEspUserSettingsRepository espUserSettingsRepository; + + public EspUserSettingsRepo(IEspUserSettingsRepository espUserSettingsRepository) { + this.espUserSettingsRepository = espUserSettingsRepository; + } + + public EspUserSettings saveUserSettings(EspUserSettings espUserSettings) { + return espUserSettingsRepository.save(espUserSettings); + } + + public EspUserSettings getUserSettings(int userId) { + return espUserSettingsRepository.findByUserId(userId) + .orElseThrow(() -> new RuntimeException("User settings not found for userId: " + userId)); + } + + public void deleteUserSettings(int userId) { + espUserSettingsRepository.findByUserId(userId).ifPresent(espUserSettingsRepository::delete); + } + + public Iterable getUserSettingsByLngId(int lngId) { + return espUserSettingsRepository.findByLngId(lngId); + } + + public Iterable getUserSettingsByDarkModeOn(boolean darkModeOn) { + return espUserSettingsRepository.findByDarkModeOn(darkModeOn); + } +} diff --git a/spring/src/main/java/com/pogoda/weather/data/EspUsersAlertRepo.java b/spring/src/main/java/com/pogoda/weather/data/EspUsersAlertRepo.java new file mode 100644 index 0000000..4a7ec26 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/data/EspUsersAlertRepo.java @@ -0,0 +1,36 @@ +package com.pogoda.weather.data; + +import org.springframework.stereotype.Repository; +import com.pogoda.weather.interfaces.IEspUsersAlertRepository; +import com.pogoda.weather.model.EspUsersAlert; + +@Repository +public class EspUsersAlertRepo { + + private final IEspUsersAlertRepository espUsersAlertRepository; + + public EspUsersAlertRepo(IEspUsersAlertRepository espUsersAlertRepository) { + this.espUsersAlertRepository = espUsersAlertRepository; + } + + public EspUsersAlert saveUserAlert(EspUsersAlert espUsersAlert) { + return espUsersAlertRepository.save(espUsersAlert); + } + + public EspUsersAlert getUserAlert(String userId, String alertId) { + return espUsersAlertRepository.findByUserIdAndAlertId(userId, alertId).orElseThrow( + () -> new RuntimeException("Alert not found for userId: " + userId + " and alertId: " + alertId)); + } + + public boolean userAlertExists(String userId, String alertId) { + return espUsersAlertRepository.existsByUserIdAndAlertId(userId, alertId); + } + + public void deleteUserAlert(String userId, String alertId) { + espUsersAlertRepository.deleteByUserIdAndAlertId(userId, alertId); + } + + public Iterable getUserAlerts(String userId) { + return espUsersAlertRepository.findByUserId(userId); + } +} diff --git a/spring/src/main/java/com/pogoda/weather/data/EspUsersRepo.java b/spring/src/main/java/com/pogoda/weather/data/EspUsersRepo.java new file mode 100644 index 0000000..7c8170d --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/data/EspUsersRepo.java @@ -0,0 +1,32 @@ +package com.pogoda.weather.data; + +import org.springframework.stereotype.Repository; +import com.pogoda.weather.interfaces.IEspUsersRepository; +import com.pogoda.weather.model.EspUsers; + +@Repository +public class EspUsersRepo { + + private final IEspUsersRepository espUsersRepository; + + public EspUsersRepo(IEspUsersRepository espUsersRepository) { + this.espUsersRepository = espUsersRepository; + } + + public EspUsers saveUser(EspUsers espUsers) { + return espUsersRepository.save(espUsers); + } + + public EspUsers getUserByLogin(String login) { + return espUsersRepository.findByLogin(login) + .orElseThrow(() -> new RuntimeException("User not found with login: " + login)); + } + + public boolean userExists(String login) { + return espUsersRepository.existsByLogin(login); + } + + public void deleteUserByLogin(String login) { + espUsersRepository.findByLogin(login).ifPresent(espUsersRepository::delete); + } +} diff --git a/spring/src/main/java/com/pogoda/weather/errors/IllegalParmException.java b/spring/src/main/java/com/pogoda/weather/errors/IllegalParmException.java new file mode 100644 index 0000000..1b59f1c --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/errors/IllegalParmException.java @@ -0,0 +1,14 @@ +package com.pogoda.weather.errors; + +public class IllegalParmException extends Exception { + private final String message; + + public IllegalParmException(String message) { + this.message = message; + } + + @Override + public String getMessage() { + return message; + } +} diff --git a/spring/src/main/java/com/pogoda/weather/errors/NotFoundException.java b/spring/src/main/java/com/pogoda/weather/errors/NotFoundException.java new file mode 100644 index 0000000..ad1e034 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/errors/NotFoundException.java @@ -0,0 +1,16 @@ +package com.pogoda.weather.errors; + +public class NotFoundException extends Exception { + + private String message; + + public NotFoundException(String message) { + this.message = message; + } + + @Override + public String getMessage() { + return message; + } + +} diff --git a/spring/src/main/java/com/pogoda/weather/interfaces/IEspAlertsRepository.java b/spring/src/main/java/com/pogoda/weather/interfaces/IEspAlertsRepository.java new file mode 100644 index 0000000..e4728be --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/interfaces/IEspAlertsRepository.java @@ -0,0 +1,16 @@ +package com.pogoda.weather.interfaces; + +import org.springframework.data.repository.CrudRepository; +import com.pogoda.weather.model.EspAlerts; +import java.util.Optional; + +public interface IEspAlertsRepository extends CrudRepository { + + Optional findByAlertId(String alertId); + + Optional findByAlertType(String alertType); + + boolean existsByAlertId(String alertId); + + boolean existsByAlertType(String alertType); +} diff --git a/spring/src/main/java/com/pogoda/weather/interfaces/IEspLanguagesRepository.java b/spring/src/main/java/com/pogoda/weather/interfaces/IEspLanguagesRepository.java new file mode 100644 index 0000000..039d6bf --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/interfaces/IEspLanguagesRepository.java @@ -0,0 +1,16 @@ +package com.pogoda.weather.interfaces; + +import org.springframework.data.repository.CrudRepository; +import com.pogoda.weather.model.EspLanguages; +import java.util.Optional; + +public interface IEspLanguagesRepository extends CrudRepository { + + Optional findByLangId(String langId); + + Optional findByName(String name); + + boolean existsByLangId(String langId); + + boolean existsByName(String name); +} diff --git a/spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasureUnitsRepository.java b/spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasureUnitsRepository.java new file mode 100644 index 0000000..4ab654c --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasureUnitsRepository.java @@ -0,0 +1,16 @@ +package com.pogoda.weather.interfaces; + +import org.springframework.data.repository.CrudRepository; +import com.pogoda.weather.model.EspMeasureUnits; +import java.util.Optional; + +public interface IEspMeasureUnitsRepository extends CrudRepository { + + Optional findByUnitId(String unitId); + + Optional findByName(String name); + + boolean existsByUnitId(String unitId); + + boolean existsByName(String name); +} diff --git a/spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasurementsReposytory.java b/spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasurementsReposytory.java new file mode 100644 index 0000000..27ad473 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/interfaces/IEspMeasurementsReposytory.java @@ -0,0 +1,25 @@ +package com.pogoda.weather.interfaces; + +import org.springframework.data.repository.CrudRepository; +import com.pogoda.weather.model.EspMeasurements; +import java.util.Optional; + +public interface IEspMeasurementsReposytory extends CrudRepository { + + @SuppressWarnings("unchecked") + EspMeasurements save(EspMeasurements espMeasurements); + + + + Iterable findByPressure1(int pressure); + + Iterable findByTemperature1(int temperature); + + Optional findById(String id); + + void deleteById(String id); + + void delete(EspMeasurements entity); + + void deleteAll(); +} diff --git a/spring/src/main/java/com/pogoda/weather/interfaces/IEspUserSettingsRepository.java b/spring/src/main/java/com/pogoda/weather/interfaces/IEspUserSettingsRepository.java new file mode 100644 index 0000000..5d73901 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/interfaces/IEspUserSettingsRepository.java @@ -0,0 +1,14 @@ +package com.pogoda.weather.interfaces; + +import org.springframework.data.repository.CrudRepository; +import com.pogoda.weather.model.EspUserSettings; +import java.util.Optional; + +public interface IEspUserSettingsRepository extends CrudRepository { + + Optional findByUserId(int userId); + + Iterable findByLngId(int lngId); + + Iterable findByDarkModeOn(boolean darkModeOn); +} diff --git a/spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersAlertRepository.java b/spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersAlertRepository.java new file mode 100644 index 0000000..21ce6a7 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersAlertRepository.java @@ -0,0 +1,21 @@ +package com.pogoda.weather.interfaces; + +import org.springframework.data.repository.CrudRepository; +import com.pogoda.weather.model.EspUsersAlert; + +import java.util.Optional; + +public interface IEspUsersAlertRepository extends CrudRepository { + + // Znajdź alerty powiązane z użytkownikiem na podstawie userId + Iterable findByUserId(String userId); + + // Znajdź alert na podstawie userId i alertId + Optional findByUserIdAndAlertId(String userId, String alertId); + + // Sprawdź, czy użytkownik ma powiązany alert + boolean existsByUserIdAndAlertId(String userId, String alertId); + + // Usuń alert na podstawie userId i alertId + void deleteByUserIdAndAlertId(String userId, String alertId); +} diff --git a/spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersRepository.java b/spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersRepository.java new file mode 100644 index 0000000..e743654 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/interfaces/IEspUsersRepository.java @@ -0,0 +1,12 @@ +package com.pogoda.weather.interfaces; + +import org.springframework.data.repository.CrudRepository; +import com.pogoda.weather.model.EspUsers; +import java.util.Optional; + +public interface IEspUsersRepository extends CrudRepository { + + Optional findByLogin(String login); + + boolean existsByLogin(String login); +} \ No newline at end of file diff --git a/spring/src/main/java/com/pogoda/weather/model/EspAlerts.java b/spring/src/main/java/com/pogoda/weather/model/EspAlerts.java new file mode 100644 index 0000000..a3ea5bd --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/model/EspAlerts.java @@ -0,0 +1,30 @@ +package com.pogoda.weather.model; + + + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter + +public class EspAlerts { + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private String alertId; + private String alertType; + + public EspAlerts(String alertType) { + this.alertType = alertType; + } + +} \ No newline at end of file diff --git a/spring/src/main/java/com/pogoda/weather/model/EspLanguages.java b/spring/src/main/java/com/pogoda/weather/model/EspLanguages.java new file mode 100644 index 0000000..ab9543a --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/model/EspLanguages.java @@ -0,0 +1,28 @@ +package com.pogoda.weather.model; + +import java.util.UUID; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +public class EspLanguages { + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private String langId; + private String name; + + public EspLanguages(String name) { + this.name = name; + } +} diff --git a/spring/src/main/java/com/pogoda/weather/model/EspMeasureUnits.java b/spring/src/main/java/com/pogoda/weather/model/EspMeasureUnits.java new file mode 100644 index 0000000..c56aa32 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/model/EspMeasureUnits.java @@ -0,0 +1,27 @@ +package com.pogoda.weather.model; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter + +public class EspMeasureUnits { + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private String unitId; + private String name; + + public EspMeasureUnits(String name) { + this.name = name; + } +} diff --git a/spring/src/main/java/com/pogoda/weather/model/EspMeasurements.java b/spring/src/main/java/com/pogoda/weather/model/EspMeasurements.java new file mode 100644 index 0000000..71b1883 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/model/EspMeasurements.java @@ -0,0 +1,35 @@ +package com.pogoda.weather.model; + +import java.util.UUID; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +public class EspMeasurements { + + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private String id; + private int pressure1; + private int pressure2; + private int temperature1; + private int temperature2; // i tu bedzie wiecej teraz tylko model + + public EspMeasurements(int press1, int press2, int temp1, int temp2) { + this.pressure1 = press1; + this.pressure2 = press2; + this.temperature1 = temp1; + this.temperature2 = temp2; + } +} diff --git a/spring/src/main/java/com/pogoda/weather/model/EspUserSettings.java b/spring/src/main/java/com/pogoda/weather/model/EspUserSettings.java new file mode 100644 index 0000000..1dc90a9 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/model/EspUserSettings.java @@ -0,0 +1,34 @@ +package com.pogoda.weather.model; + + + +import jakarta.persistence.Entity; + +import jakarta.persistence.Id; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity + +@NoArgsConstructor +@Getter +@Setter + +public class EspUserSettings { + + @Id + private int userId; + private int preferedUnitId; + private int lngId; + private boolean darkModeOn; + + public EspUserSettings(int userId, int preferedUnitId, int lngId, boolean darkModeOn) { + this.userId = userId; + this.preferedUnitId = preferedUnitId; + this.lngId = lngId; + this.darkModeOn = darkModeOn; + } + +} \ No newline at end of file diff --git a/spring/src/main/java/com/pogoda/weather/model/EspUsers.java b/spring/src/main/java/com/pogoda/weather/model/EspUsers.java new file mode 100644 index 0000000..9c3386b --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/model/EspUsers.java @@ -0,0 +1,30 @@ +package com.pogoda.weather.model; + + + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +public class EspUsers { + @Id + @GeneratedValue(strategy = GenerationType.UUID) + private String id; + private String login; + private String passwordHash; + + public EspUsers(String login, String passwordHash) { + this.login = login; + this.passwordHash = passwordHash; + } +} diff --git a/spring/src/main/java/com/pogoda/weather/model/EspUsersAlert.java b/spring/src/main/java/com/pogoda/weather/model/EspUsersAlert.java new file mode 100644 index 0000000..0439e58 --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/model/EspUsersAlert.java @@ -0,0 +1,27 @@ +package com.pogoda.weather.model; + +import java.io.Serializable; + + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Entity +@IdClass(EspUsersAlert.class) +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +public class EspUsersAlert implements Serializable +{ + @Id + private String userId; + @Id + private String alertId; + + + +} diff --git a/spring/src/main/java/com/pogoda/weather/services/Service.java b/spring/src/main/java/com/pogoda/weather/services/Service.java new file mode 100644 index 0000000..b1b52ea --- /dev/null +++ b/spring/src/main/java/com/pogoda/weather/services/Service.java @@ -0,0 +1,16 @@ +package com.pogoda.weather.services; + +import com.pogoda.weather.data.EspMeasurementsRepo; + +import lombok.AllArgsConstructor; + +@AllArgsConstructor +@org.springframework.stereotype.Service +public class Service { + EspMeasurementsRepo espDataRepo; + + public void tuBedzieLogika() { + System.out.println("To przeciez logiczne"); + } + +} diff --git a/spring/src/main/resources/application.properties b/spring/src/main/resources/application.properties index cf32b59..99ca61b 100644 --- a/spring/src/main/resources/application.properties +++ b/spring/src/main/resources/application.properties @@ -1 +1,10 @@ spring.application.name=weather +spring.datasource.url=jdbc:mysql://localhost:3306/Weather +spring.datasource.username=WeatherServer +spring.datasource.password=t4jn3h4sL0 +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver + +spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true From 0819532266e5db426c142c2f9eb6c193853acae1 Mon Sep 17 00:00:00 2001 From: EldoPlo <129173853+EldoPlo@users.noreply.github.com> Date: Sat, 16 Nov 2024 22:08:18 +0100 Subject: [PATCH 2/2] [Added models, interfaces and repo in spring] --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index aac2728..ab8d442 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,4 @@ spring/*.rar hs_err_pid* replay_pid* -.vscode +