From d5a960f20fa13adc883b0f6e1ae4b2d43555563e Mon Sep 17 00:00:00 2001
From: Jessie Ragot <jessie.ragot@hotmail.com>
Date: Fri, 28 Feb 2025 14:26:59 +0100
Subject: [PATCH] Upload MedicalDocument and design for view-medical-file
 restored, is functional Change the home page depending on who is logged in,
 change the header (only for the doctor for the moment, as the other paths for
 patient and admin do not exist).

---
 .../controllers/HomeController.java           | 17 +++-
 src/main/resources/templates/home.html        | 33 +++++--
 .../templates/view-medical-file.html          | 96 ++++++++++---------
 3 files changed, 91 insertions(+), 55 deletions(-)

diff --git a/src/main/java/com/projet/projetIndu/controllers/HomeController.java b/src/main/java/com/projet/projetIndu/controllers/HomeController.java
index 40639b3..b07e68e 100644
--- a/src/main/java/com/projet/projetIndu/controllers/HomeController.java
+++ b/src/main/java/com/projet/projetIndu/controllers/HomeController.java
@@ -1,13 +1,28 @@
 package com.projet.projetIndu.controllers;
 
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
 
+import java.util.Collection;
+
 @Controller
 public class HomeController {
 
     @GetMapping("/")
-    public String home() {
+    public String home(Model model) {
+        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
+        if (authentication != null && authentication.isAuthenticated() && !authentication.getPrincipal().equals("anonymousUser")) {
+            model.addAttribute("username", authentication.getName());
+
+            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
+            boolean isDoctor = authorities.stream().anyMatch(auth -> auth.getAuthority().equals("DOCTOR"));
+            model.addAttribute("isDoctor", isDoctor);
+        }
+
         return "home";
     }
 }
diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html
index 5a59220..932d441 100644
--- a/src/main/resources/templates/home.html
+++ b/src/main/resources/templates/home.html
@@ -14,9 +14,21 @@
         <div class="flex justify-between items-center py-6">
             <h1 class="text-2xl font-semibold text-gray-900">Gestion Dossiers Médicaux</h1>
             <div class="space-x-4">
-                <a href="/doctors/dashboard" class="px-6 py-2 bg-green-500 text-white rounded-md hover:bg-green-600">Professionnel de santé</a>
-                <a th:href="@{/login}" class="text-blue-500 hover:text-blue-700">Connexion</a>
-                <a th:href="@{/register}" class="text-blue-500 hover:text-blue-700">Inscription</a>
+                <th:block th:if="${isDoctor}">
+                    <a href="/doctors/dashboard"
+                       class="px-6 py-2 bg-green-500 text-white rounded-md hover:bg-green-600">Professionnel de
+                        santé</a>
+                </th:block>
+                <th:block th:if="${username}">
+                    <span class="text-gray-900 font-semibold">Bienvenue, <b th:text="${username}"></b> !</span>
+                    <a href="/logout" class="ml-4 text-red-500 hover:text-red-700">Déconnexion</a>
+                </th:block>
+
+                <th:block th:unless="${username}">
+                    <a th:href="@{/login}" class="text-blue-500 hover:text-blue-700">Connexion</a>
+                    <a th:href="@{/register}" class="text-blue-500 hover:text-blue-700">Inscription</a>
+                </th:block>
+
             </div>
         </div>
     </div>
@@ -26,16 +38,21 @@
 <main class="mt-12">
     <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
         <div class="text-center">
-            <h2 class="text-4xl font-bold text-gray-800">Bienvenue sur notre application de gestion de dossiers médicaux</h2>
-            <p class="mt-4 text-lg text-gray-600">Accédez facilement à vos dossiers médicaux et organisez vos rendez-vous en toute sécurité.</p>
+            <h2 class="text-4xl font-bold text-gray-800">Bienvenue sur notre application de gestion de dossiers
+                médicaux</h2>
+            <p class="mt-4 text-lg text-gray-600">Accédez facilement à vos dossiers médicaux et organisez vos
+                rendez-vous en toute sécurité.</p>
         </div>
 
         <!-- Search Section -->
         <div class="mt-8 text-center">
             <form action="/doctors/search" method="get" class="max-w-md mx-auto flex items-center space-x-4">
-                <input type="text" name="firstName" class="w-full px-4 py-2 border rounded-md" placeholder="Prénom du médecin">
-                <input type="text" name="lastName" class="w-full px-4 py-2 border rounded-md" placeholder="Nom du médecin" required>
-                <button type="submit" class="px-6 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">Rechercher</button>
+                <input type="text" name="firstName" class="w-full px-4 py-2 border rounded-md"
+                       placeholder="Prénom du médecin">
+                <input type="text" name="lastName" class="w-full px-4 py-2 border rounded-md"
+                       placeholder="Nom du médecin" required>
+                <button type="submit" class="px-6 py-2 bg-blue-500 text-white rounded-md hover:bg-blue-600">Rechercher
+                </button>
             </form>
         </div>
     </div>
diff --git a/src/main/resources/templates/view-medical-file.html b/src/main/resources/templates/view-medical-file.html
index ce6b6b8..b336c37 100644
--- a/src/main/resources/templates/view-medical-file.html
+++ b/src/main/resources/templates/view-medical-file.html
@@ -3,65 +3,69 @@
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1">
-    <title>Dossier médical</title>
-    <link rel="stylesheet"
-          href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
+    <title>Détails du dossier médical</title>
+    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
 </head>
 <body class="bg-light">
 
 <div class="container mt-5">
+    <h1 class="text-center mb-4">Détails du dossier médical</h1>
 
-    <!-- Titre du dossier médical -->
-    <h1 class="text-center mb-4">Dossier Médical de <span
-            th:text="${medicalFile.patient.firstName} + ' ' + ${medicalFile.patient.lastName}"></span></h1>
-
-    <!-- Informations générales -->
-    <div class="card mb-4">
-        <div class="card-header bg-primary text-white">
-            <h3>Informations générales</h3>
-        </div>
-        <div class="card-body">
-            <p><strong>Allergies:</strong> <span th:text="${medicalFile.allergy}"></span></p>
-            <p><strong>Traitement:</strong> <span th:text="${medicalFile.treatment}"></span></p>
-        </div>
+    <div th:if="${medicalFile}">
+        <h5 class="card-title">Dossier médical de :
+            <span th:text="${medicalFile.patient != null ? medicalFile.patient.firstName : 'Inconnu'}"></span>
+            <span th:text="${medicalFile.patient != null ? medicalFile.patient.lastName : ''}"></span>
+        </h5>
     </div>
 
     <!-- Historique médical -->
-    <div class="card mb-4">
-        <div class="card-header bg-success text-white">
-            <h3>Historique médical</h3>
-        </div>
-        <div class="card-body">
-            <ul>
-                <li th:each="history : ${medicalFile.healthHistoricalList}">
-                    <p><strong>Antécédent:</strong> <span th:text="${history.antecedent}"></span></p>
-                    <p><strong>Notes:</strong> <span th:text="${history.notes}"></span></p>
-                    <p><strong>Date de consultation:</strong> <span th:text="${history.consultationDate}"></span></p>
-                </li>
-            </ul>
-        </div>
+    <div class="mt-3">
+        <h5>Historique médical :</h5>
+        <ul class="list-group">
+            <li th:each="history : ${medicalFile.healthHistoricalList}" class="list-group-item">
+                <strong>Antécédent:</strong> <span th:text="${history.antecedent}"></span><br>
+                <strong>Notes:</strong> <span th:text="${history.notes}"></span><br>
+                <strong>Date de consultation:</strong> <span th:text="${history.consultationDate}"></span>
+            </li>
+        </ul>
     </div>
 
-    <!-- Documents -->
-    <div class="card mb-4">
-        <div class="card-header bg-info text-white">
-            <h3>Documents</h3>
-        </div>
-        <div class="card-body">
-            <ul>
-                <li th:each="document : ${medicalFile.documentList}">
-                    <a th:href="@{/medical-files/{id}/documents/{docId}(id=${medicalFile.id}, docId=${document.id})}"
-                       class="btn btn-link" th:text="${document.fileName}"></a>
-                </li>
-            </ul>
-        </div>
-    </div>
+    <!-- Formulaire de téléchargement du document -->
+    <form method="post" th:action="@{/medical-files/{id}/documents(id=${medicalFile.id})}" enctype="multipart/form-data"
+          class="mt-3">
+        <label for="file" class="form-label">Importer un document :</label>
+        <input type="file" name="document" id="file" class="form-control mb-2"/>
+        <button type="submit" class="btn btn-primary">Uploader le document</button>
+    </form>
 
-    <!-- Retour à la liste -->
-    <a href="/medical-files" class="btn btn-secondary mt-3">Retour à la liste</a>
+    <!-- Liste des documents avec lien de téléchargement -->
+    <div class="mt-3">
+        <h5>Documents disponibles :</h5>
+        <ul class="list-group">
+            <li th:each="doc : ${medicalFile.documentList}"
+                class="list-group-item d-flex justify-content-between align-items-center">
+                <span th:text="${doc.fileName}"></span>
+                <div>
+                    <a th:href="@{/medical-files/{id}/documents/{docId}(id=${medicalFile.id}, docId=${doc.id})}"
+                       class="btn btn-sm btn-warning">
+                        Télécharger
+                    </a>
+                    <form th:action="@{/medical-files/{id}/documents/{docId}(id=${medicalFile.id}, docId=${doc.id})}"
+                          method="post" class="d-inline">
+                        <input type="hidden" name="_method" value="delete"/>
+                        <button type="submit" class="btn btn-sm btn-danger"
+                                onclick="return confirm('Voulez-vous vraiment supprimer ce document ?');">
+                            Supprimer
+                        </button>
+                    </form>
+                </div>
+            </li>
+        </ul>
+    </div>
 
+    <a href="/medical-files" class="btn btn-primary mt-3">Retour à la liste</a>
 </div>
 
 <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
 </body>
-</html>
+</html>
\ No newline at end of file
-- 
GitLab