mirror of
https://github.com/Lemonochrme/service-architecture.git
synced 2025-06-08 05:30:50 +02:00
Moved bash scripts for database management. Added CORS Configuration and using REST API to gather list of roles.
This commit is contained in:
parent
97ede71cf8
commit
c452198ee9
8 changed files with 92 additions and 12 deletions
|
@ -1,5 +1,5 @@
|
|||
INSERT INTO `service-architecture`.roles (id, name)
|
||||
VALUES
|
||||
(1, 'user'),
|
||||
(2, 'volunteer'),
|
||||
(3, 'admin');
|
||||
(1, 'User'),
|
||||
(2, 'Volunteer'),
|
||||
(3, 'Admin');
|
||||
|
|
|
@ -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<Role> getRoles() {
|
||||
return roleRepository.findAll();
|
||||
|
|
|
@ -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
|
||||
|
|
34
helpapp-frontend/base.js
Normal file
34
helpapp-frontend/base.js
Normal 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;
|
||||
}
|
17
helpapp-frontend/htmx.html
Normal file
17
helpapp-frontend/htmx.html
Normal 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>
|
|
@ -1,10 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<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>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<h1>HelpApp</h1>
|
||||
|
@ -27,8 +31,6 @@
|
|||
<input type="email" id="email" placeholder="Email" required>
|
||||
<input type="password" id="password" placeholder="Mot de passe" required>
|
||||
<select id="role" required>
|
||||
<option value="REQUESTER">Demandeur</option>
|
||||
<option value="VOLUNTEER">Volontaire</option>
|
||||
</select>
|
||||
<button type="submit">Créer un compte</button>
|
||||
</form>
|
||||
|
@ -49,7 +51,20 @@
|
|||
<script>
|
||||
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", () => {
|
||||
roleDefine();
|
||||
|
||||
const loginForm = document.getElementById("login-form");
|
||||
const loginResponse = document.getElementById("login-response");
|
||||
const registerForm = document.getElementById("register-form");
|
||||
|
@ -109,4 +124,5 @@
|
|||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -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
|
|
@ -1,6 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
source ./db.sh
|
||||
source ./db/db.sh
|
||||
|
||||
check_env
|
||||
echo "Removing database 'service-architecture'..."
|
Loading…
Add table
Reference in a new issue