From c452198ee912dc0424d1997b93ee8a82667d355c Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sat, 21 Dec 2024 16:04:16 +0100 Subject: [PATCH] Moved bash scripts for database management. Added CORS Configuration and using REST API to gather list of roles. --- db/init.sql | 6 ++-- .../helpapp/rest/RoleServiceApplication.java | 13 +++++++ .../src/main/resources/application.properties | 4 +-- helpapp-frontend/base.js | 34 +++++++++++++++++++ helpapp-frontend/htmx.html | 17 ++++++++++ helpapp-frontend/index.html | 22 ++++++++++-- db/init.sh => init.sh | 6 ++-- db/remove.sh => remove.sh | 2 +- 8 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 helpapp-frontend/base.js create mode 100644 helpapp-frontend/htmx.html rename db/init.sh => init.sh (79%) rename db/remove.sh => remove.sh (95%) diff --git a/db/init.sql b/db/init.sql index 20e28f4..af6c6cf 100644 --- a/db/init.sql +++ b/db/init.sql @@ -1,5 +1,5 @@ INSERT INTO `service-architecture`.roles (id, name) VALUES - (1, 'user'), - (2, 'volunteer'), - (3, 'admin'); + (1, 'User'), + (2, 'Volunteer'), + (3, 'Admin'); diff --git a/helpapp-backend/role-service/src/main/java/insa/application/helpapp/rest/RoleServiceApplication.java b/helpapp-backend/role-service/src/main/java/insa/application/helpapp/rest/RoleServiceApplication.java index 7b06920..9669849 100644 --- a/helpapp-backend/role-service/src/main/java/insa/application/helpapp/rest/RoleServiceApplication.java +++ b/helpapp-backend/role-service/src/main/java/insa/application/helpapp/rest/RoleServiceApplication.java @@ -21,6 +21,19 @@ public class RoleServiceApplication { SpringApplication.run(RoleServiceApplication.class, args); } + // CORS Configuration + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**").allowedOrigins("*") + .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") + .allowedHeaders("*"); + } + }; + } + @GetMapping("/get_roles") public List getRoles() { return roleRepository.findAll(); diff --git a/helpapp-backend/role-service/src/main/resources/application.properties b/helpapp-backend/role-service/src/main/resources/application.properties index fd04298..f2e0be5 100644 --- a/helpapp-backend/role-service/src/main/resources/application.properties +++ b/helpapp-backend/role-service/src/main/resources/application.properties @@ -1,7 +1,7 @@ server.port=8089 spring.datasource.url=jdbc:mysql://localhost:3306/service-architecture -spring.datasource.username= -spring.datasource.password= +spring.datasource.username=yoboujon +spring.datasource.password=25158114 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=none spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect diff --git a/helpapp-frontend/base.js b/helpapp-frontend/base.js new file mode 100644 index 0000000..5fc5d96 --- /dev/null +++ b/helpapp-frontend/base.js @@ -0,0 +1,34 @@ +/** + * Fetches data from the given URL and returns the parsed JSON response. + * + * @param {string} url - The URL to fetch data from. + * @returns {Promise} - A promise that resolves to an array from the JSON response. + */ +function fetchData(url) { + return fetch(url, { + method: 'GET', + headers: { + 'Accept': 'application/json', // Expecting JSON data from the server + } + }) + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); // Parse the JSON response + }) + .then(data => { + return data; // Return the array from the JSON response + }) + .catch(error => { + console.error('There was a problem with the fetch operation:', error); + return []; // Return an empty array in case of an error + }); +} + +function createOption(id, name) { + const option = document.createElement('option'); + option.value = id; + option.textContent = name; + return option; +} diff --git a/helpapp-frontend/htmx.html b/helpapp-frontend/htmx.html new file mode 100644 index 0000000..185567a --- /dev/null +++ b/helpapp-frontend/htmx.html @@ -0,0 +1,17 @@ + +

Dynamic Dropdown with HTMX

+ + + + + + + + \ No newline at end of file diff --git a/helpapp-frontend/index.html b/helpapp-frontend/index.html index 693981f..ac5d72e 100644 --- a/helpapp-frontend/index.html +++ b/helpapp-frontend/index.html @@ -1,10 +1,14 @@ + + + HelpApp +

HelpApp

@@ -27,8 +31,6 @@ @@ -49,7 +51,20 @@ - + + \ No newline at end of file diff --git a/db/init.sh b/init.sh similarity index 79% rename from db/init.sh rename to init.sh index b471592..96a690f 100755 --- a/db/init.sh +++ b/init.sh @@ -1,6 +1,6 @@ #!/bin/bash -source ./db.sh +source ./db/db.sh check_env echo "> Creating database 'service-architecture'..." @@ -11,14 +11,14 @@ then fi echo "> Uploading database 'service-architecture'..." -if ! mariadb -u "$user" -p"$pwd" "service-architecture" < db.sql +if ! mariadb -u "$user" -p"$pwd" "service-architecture" < db/db.sql then echo -e $RED"Error: Failed to execute db.sql, contact the maintainers."$RESET exit 1 fi echo "> Populating database 'service-architecture'..." -if ! mariadb -u "$user" -p"$pwd" "service-architecture" < init.sql +if ! mariadb -u "$user" -p"$pwd" "service-architecture" < db/init.sql then echo -e $RED"Error: Failed to execute init.sql, contact the maintainers."$RESET exit 1 diff --git a/db/remove.sh b/remove.sh similarity index 95% rename from db/remove.sh rename to remove.sh index fde4595..0840bb1 100755 --- a/db/remove.sh +++ b/remove.sh @@ -1,6 +1,6 @@ #!/bin/bash -source ./db.sh +source ./db/db.sh check_env echo "Removing database 'service-architecture'..."