Moved bash scripts for database management. Added CORS Configuration and using REST API to gather list of roles.

This commit is contained in:
Yohan Boujon 2024-12-21 16:04:16 +01:00
parent 97ede71cf8
commit c452198ee9
8 changed files with 92 additions and 12 deletions

View file

@ -1,5 +1,5 @@
INSERT INTO `service-architecture`.roles (id, name) INSERT INTO `service-architecture`.roles (id, name)
VALUES VALUES
(1, 'user'), (1, 'User'),
(2, 'volunteer'), (2, 'Volunteer'),
(3, 'admin'); (3, 'Admin');

View file

@ -21,6 +21,19 @@ public class RoleServiceApplication {
SpringApplication.run(RoleServiceApplication.class, args); 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") @GetMapping("/get_roles")
public List<Role> getRoles() { public List<Role> getRoles() {
return roleRepository.findAll(); return roleRepository.findAll();

View file

@ -1,7 +1,7 @@
server.port=8089 server.port=8089
spring.datasource.url=jdbc:mysql://localhost:3306/service-architecture spring.datasource.url=jdbc:mysql://localhost:3306/service-architecture
spring.datasource.username= spring.datasource.username=yoboujon
spring.datasource.password= spring.datasource.password=25158114
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

34
helpapp-frontend/base.js Normal file
View file

@ -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<Array>} - 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;
}

View file

@ -0,0 +1,17 @@
<body>
<h1>Dynamic Dropdown with HTMX</h1>
<label for="roles">Choose a role:</label>
<select id="roles" hx-get="http://localhost:8089/get_roles" hx-target="#roles" hx-swap="outerHTML">
<!-- Initially empty, will be populated by HTMX -->
<option value="" disabled selected>Loading roles...</option>
</select>
<button hx-get="http://localhost:8089/get_roles" hx-target="#roles" hx-swap="outerHTML">
Refresh Roles
</button>
<script>
// HTMX will automatically handle the JSON response and populate the dropdown
</script>
</body>

View file

@ -1,10 +1,14 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/htmx.org@1.9.12"></script> <!-- Include HTMX -->
<script src="base.js"></script> <!-- Include BASE -->
<title>HelpApp</title> <title>HelpApp</title>
</head> </head>
<body> <body>
<header> <header>
<h1>HelpApp</h1> <h1>HelpApp</h1>
@ -27,8 +31,6 @@
<input type="email" id="email" placeholder="Email" required> <input type="email" id="email" placeholder="Email" required>
<input type="password" id="password" placeholder="Mot de passe" required> <input type="password" id="password" placeholder="Mot de passe" required>
<select id="role" required> <select id="role" required>
<option value="REQUESTER">Demandeur</option>
<option value="VOLUNTEER">Volontaire</option>
</select> </select>
<button type="submit">Créer un compte</button> <button type="submit">Créer un compte</button>
</form> </form>
@ -49,7 +51,20 @@
<script> <script>
const API_BASE = "http://localhost"; const API_BASE = "http://localhost";
// Append the user-registration dropdown for each role from the database
function roleDefine() {
const select = document.getElementById("role");
fetchData(`${API_BASE}:8089/get_roles`).then(data => {
data.forEach(element => {
const option = createOption(element.id, element.name);
select.appendChild(option);
});
});
}
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
roleDefine();
const loginForm = document.getElementById("login-form"); const loginForm = document.getElementById("login-form");
const loginResponse = document.getElementById("login-response"); const loginResponse = document.getElementById("login-response");
const registerForm = document.getElementById("register-form"); const registerForm = document.getElementById("register-form");
@ -109,4 +124,5 @@
}); });
</script> </script>
</body> </body>
</html> </html>

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
source ./db.sh source ./db/db.sh
check_env check_env
echo "> Creating database 'service-architecture'..." echo "> Creating database 'service-architecture'..."
@ -11,14 +11,14 @@ then
fi fi
echo "> Uploading database 'service-architecture'..." 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 then
echo -e $RED"Error: Failed to execute db.sql, contact the maintainers."$RESET echo -e $RED"Error: Failed to execute db.sql, contact the maintainers."$RESET
exit 1 exit 1
fi fi
echo "> Populating database 'service-architecture'..." 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 then
echo -e $RED"Error: Failed to execute init.sql, contact the maintainers."$RESET echo -e $RED"Error: Failed to execute init.sql, contact the maintainers."$RESET
exit 1 exit 1

View file

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
source ./db.sh source ./db/db.sh
check_env check_env
echo "Removing database 'service-architecture'..." echo "Removing database 'service-architecture'..."