diff --git a/README.md b/README.md index 4e52667..6650793 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ Course Exercice : Application to help others ```bash +./init.sh mvn compile - mvn spring-boot:run ``` diff --git a/helpapp-backend/.gitignore b/helpapp-backend/.gitignore new file mode 100644 index 0000000..bf3708f --- /dev/null +++ b/helpapp-backend/.gitignore @@ -0,0 +1 @@ +*/src/main/resources/application.properties \ No newline at end of file diff --git a/helpapp-backend/administration-service/src/main/resources/application.properties b/helpapp-backend/administration-service/src/main/resources/application.properties deleted file mode 100644 index 4c00e40..0000000 --- a/helpapp-backend/administration-service/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -server.port=8080 diff --git a/helpapp-backend/feedback-service/src/main/resources/application.properties b/helpapp-backend/feedback-service/src/main/resources/application.properties deleted file mode 100644 index 4d360de..0000000 --- a/helpapp-backend/feedback-service/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -server.port=8081 diff --git a/helpapp-backend/request-service/src/main/resources/application.properties b/helpapp-backend/request-service/src/main/resources/application.properties deleted file mode 100644 index 3cf12af..0000000 --- a/helpapp-backend/request-service/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -server.port=8082 diff --git a/helpapp-backend/role-service/src/main/resources/application.properties b/helpapp-backend/role-service/src/main/resources/application.properties deleted file mode 100644 index f2e0be5..0000000 --- a/helpapp-backend/role-service/src/main/resources/application.properties +++ /dev/null @@ -1,7 +0,0 @@ -server.port=8089 -spring.datasource.url=jdbc:mysql://localhost:3306/service-architecture -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-backend/user-service/src/main/resources/application.properties b/helpapp-backend/user-service/src/main/resources/application.properties deleted file mode 100644 index 8f91ca7..0000000 --- a/helpapp-backend/user-service/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -server.port=8083 diff --git a/helpapp-backend/volunteer-service/src/main/resources/application.properties b/helpapp-backend/volunteer-service/src/main/resources/application.properties deleted file mode 100644 index 5e3bb81..0000000 --- a/helpapp-backend/volunteer-service/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -server.port=8084 diff --git a/helpapp-frontend/.gitignore b/helpapp-frontend/.gitignore new file mode 100644 index 0000000..f480996 --- /dev/null +++ b/helpapp-frontend/.gitignore @@ -0,0 +1 @@ +generated.js \ No newline at end of file diff --git a/helpapp-frontend/index.html b/helpapp-frontend/index.html index ac5d72e..50e0915 100644 --- a/helpapp-frontend/index.html +++ b/helpapp-frontend/index.html @@ -6,6 +6,7 @@ + HelpApp @@ -48,81 +49,7 @@ - + \ No newline at end of file diff --git a/helpapp-frontend/index.js b/helpapp-frontend/index.js new file mode 100644 index 0000000..2cdc2ee --- /dev/null +++ b/helpapp-frontend/index.js @@ -0,0 +1,73 @@ +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}:${ROLE_PORT}/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"); + const registerResponse = document.getElementById("register-response"); + const userRegistrationSection = document.getElementById("user-registration"); + + // Check if user is logged in + if (localStorage.getItem("userId")) { + userRegistrationSection.style.display = "none"; + } + + // User Login + loginForm.addEventListener("submit", async (e) => { + e.preventDefault(); + const email = document.getElementById("login-email").value; + const password = document.getElementById("login-password").value; + + try { + const response = await fetch(`${API_BASE}:8083/users/authenticate`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email, password }), + }); + const result = await response.json(); + if (result.success) { + localStorage.setItem("userId", result.userId); + loginResponse.textContent = `Connexion réussie : ID ${result.userId}`; + userRegistrationSection.style.display = "none"; + } else { + loginResponse.textContent = `Échec de la connexion : ${result.message}`; + } + } catch (error) { + loginResponse.textContent = "Erreur lors de la connexion."; + } + }); + + // User Registration + registerForm.addEventListener("submit", async (e) => { + e.preventDefault(); + const name = document.getElementById("name").value; + const email = document.getElementById("email").value; + const password = document.getElementById("password").value; + const role = document.getElementById("role").value; + + try { + const response = await fetch(`${API_BASE}:8083/users`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ name, email, password, role }), + }); + const result = await response.json(); + registerResponse.textContent = `Utilisateur créé : ID ${result.id}`; + } catch (error) { + registerResponse.textContent = "Erreur lors de la création de l'utilisateur."; + } + }); +}); \ No newline at end of file diff --git a/init.sh b/init.sh index 96a690f..c8065df 100755 --- a/init.sh +++ b/init.sh @@ -2,6 +2,20 @@ source ./db/db.sh +generate_properties() +{ + url="$1/src/main/resources/application.properties" + echo -e "server.port=$port +spring.datasource.url=jdbc:mysql://localhost:3306/service-architecture +spring.datasource.username=$user +spring.datasource.password=$pwd +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" > "$url" + ((port++)) +} + +## CREATING DATABASE check_env echo "> Creating database 'service-architecture'..." if ! mariadb -u "$user" -p"$pwd" -e "CREATE DATABASE IF NOT EXISTS \`service-architecture\`;" @@ -23,5 +37,40 @@ then echo -e $RED"Error: Failed to execute init.sql, contact the maintainers."$RESET exit 1 fi - echo -e $GREEN"Database setup successful!"$RESET + +## CREATING application.properties +services=() +port=8081 +echo "> Checking Microservices..." +for dir in helpapp-backend/*-service; do + if [[ -d "$dir" ]]; then + services+=("$dir") + fi +done +if [[ ${#services[@]} -gt 0 ]]; then + echo -e $GREEN"Found ${#services[@]} Microservices!"$RESET +else + echo -e $RED"Error: Found no microservices:\nAre you in the right directory ?"$RESET + exit 1 +fi + +count=0 +for service in "${services[@]}"; do + ((count++)) + echo -ne "\r> Generating 'application.properties'... [$count/${#services[@]}]" + generate_properties "$service" +done + +port=8081 +generated_js="/* !! THIS IS A GENERATED FILE, DO NOT MODIFY !! */\n\n" +echo -e "\n> Generating 'generated.js'..." +for service in "${services[@]}"; do + service_name=$(basename "$service") # Remove the last '/' + modified_name="${service_name%-service}" # Remove the "-service" + modified_name="${modified_name^^}_PORT" # Uppercase and add '_PORT' + generated_js+="const $modified_name=$port\n" + ((port++)) +done +echo -e "$generated_js" > helpapp-frontend/generated.js +echo -e $GREEN"Setup successfull for each microservice !"