Recupération Demande suppression partie Admin
This commit is contained in:
parent
ed647c8891
commit
ff7c27ba90
@ -46,6 +46,10 @@ public class MainVerticle extends AbstractVerticle {
|
||||
setWeatherData.setUserHandler(setUser);
|
||||
QueryDeleteObject RequestDeleteObject = new QueryDeleteObject(databaseService);
|
||||
|
||||
SetRequestDeleteObject DeleteObject = new SetRequestDeleteObject(databaseService);
|
||||
|
||||
|
||||
|
||||
|
||||
// Déclaration des routes
|
||||
router.get("/objets").handler(queryObjects::getObjects);
|
||||
@ -68,13 +72,10 @@ public class MainVerticle extends AbstractVerticle {
|
||||
router.post("/addCategories").handler(setObjects::newCategorie);
|
||||
router.post("/deleteCategories").handler(setObjects::deleteCategories);
|
||||
router.post("/requestDeleteObject").handler(RequestDeleteObject::deleteObject);
|
||||
|
||||
|
||||
|
||||
router.get("/getDemandeSuppression").handler(DeleteObject::getAllDeletionRequests);
|
||||
// Routes d'authentification
|
||||
router.post("/signup").handler(authHandler::handleSignup);
|
||||
router.post("/login").handler(authHandler::handleLogin);
|
||||
|
||||
|
||||
// Création du serveur HTTP
|
||||
vertx.createHttpServer()
|
||||
|
||||
@ -12,53 +12,86 @@ public class QueryDeleteObject {
|
||||
}
|
||||
|
||||
public void deleteObject(RoutingContext context) {
|
||||
JsonObject body = context.body().asJsonObject();
|
||||
JsonObject body = context.body().asJsonObject();
|
||||
|
||||
if (body == null) {
|
||||
context.response().setStatusCode(400).end(new JsonObject().put("error", "Requête invalide").encode());
|
||||
return;
|
||||
}
|
||||
|
||||
// Lecture des paramètres
|
||||
Integer objectId = body.getInteger("object_id");
|
||||
Integer userId = body.getInteger("requested_by");
|
||||
|
||||
System.out.println("Body reçu : " + body.encode());
|
||||
System.out.println("objectId = " + objectId + ", userId = " + userId);
|
||||
|
||||
if (objectId == null || userId == null) {
|
||||
context.response().setStatusCode(400).end(new JsonObject().put("error", "Champs manquants").encode());
|
||||
return;
|
||||
}
|
||||
|
||||
// Vérification si l'objet existe
|
||||
String checkQuery = "SELECT id FROM weather_objects WHERE id = ?";
|
||||
|
||||
databaseService.pool.preparedQuery(checkQuery).execute(Tuple.of(objectId), res -> {
|
||||
if (res.failed()) {
|
||||
res.cause().printStackTrace(); // Affiche l'erreur SQL s'il y en a une
|
||||
context.response().setStatusCode(500).end(new JsonObject().put("error", "Erreur base de données").encode());
|
||||
if (body == null) {
|
||||
context.response().setStatusCode(400).end(new JsonObject().put("error", "Requête invalide").encode());
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.result().rowCount() == 0) {
|
||||
context.response().setStatusCode(404).end(new JsonObject().put("error", "Objet non trouvé").encode());
|
||||
// Lecture des paramètres
|
||||
Integer objectId = body.getInteger("object_id");
|
||||
Integer userId = body.getInteger("requested_by");
|
||||
|
||||
System.out.println("Body reçu : " + body.encode());
|
||||
System.out.println("objectId = " + objectId + ", userId = " + userId);
|
||||
|
||||
if (objectId == null || userId == null) {
|
||||
context.response().setStatusCode(400).end(new JsonObject().put("error", "Champs manquants").encode());
|
||||
return;
|
||||
}
|
||||
|
||||
// Insertion simple (pas de vérif doublon pour l’instant)
|
||||
String checkDuplicate = "SELECT 1 FROM deletion_requests WHERE object_id = ? AND requested_by = ?";
|
||||
String insertQuery = "INSERT INTO deletion_requests (object_id, requested_by) VALUES (?, ?)";
|
||||
// Vérification si l'objet existe
|
||||
String checkQuery = "SELECT id FROM weather_objects WHERE id = ?";
|
||||
|
||||
databaseService.pool.preparedQuery(insertQuery).execute(Tuple.of(objectId, userId), insertRes -> {
|
||||
if (insertRes.succeeded()) {
|
||||
context.response().setStatusCode(200).end(new JsonObject().put("message", "Demande envoyée").encode());
|
||||
databaseService.pool.preparedQuery(checkQuery).execute(Tuple.of(objectId), res -> {
|
||||
if (res.failed()) {
|
||||
res.cause().printStackTrace(); // Affiche l'erreur SQL s'il y en a une
|
||||
context.response().setStatusCode(500)
|
||||
.end(new JsonObject().put("error", "Erreur base de données").encode());
|
||||
return;
|
||||
}
|
||||
|
||||
if (res.result().rowCount() == 0) {
|
||||
context.response().setStatusCode(404).end(new JsonObject().put("error", "Objet non trouvé").encode());
|
||||
return;
|
||||
}
|
||||
|
||||
// Insertion simple (pas de vérif doublon pour l’instant)
|
||||
String checkDuplicate = "SELECT 1 FROM deletion_requests WHERE object_id = ? AND requested_by = ?";
|
||||
String insertQuery = "INSERT INTO deletion_requests (object_id, requested_by) VALUES (?, ?)";
|
||||
|
||||
databaseService.pool.preparedQuery(insertQuery).execute(Tuple.of(objectId, userId), insertRes -> {
|
||||
if (insertRes.succeeded()) {
|
||||
context.response().setStatusCode(200)
|
||||
.end(new JsonObject().put("message", "Demande envoyée").encode());
|
||||
} else {
|
||||
insertRes.cause().printStackTrace(); // Pour voir l'erreur en console
|
||||
context.response().setStatusCode(500)
|
||||
.end(new JsonObject().put("error", "Erreur insertion").encode());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void getAllDeletionRequests(RoutingContext context) {
|
||||
String query = "SELECT * FROM deletion_requests";
|
||||
|
||||
databaseService.pool.query(query).execute(res -> {
|
||||
if (res.succeeded()) {
|
||||
var results = res.result();
|
||||
var jsonArray = new io.vertx.core.json.JsonArray();
|
||||
|
||||
results.forEach(row -> {
|
||||
JsonObject json = new JsonObject()
|
||||
.put("id", row.getInteger("id"))
|
||||
.put("object_id", row.getInteger("object_id"))
|
||||
.put("requested_by", row.getInteger("requested_by"))
|
||||
.put("requested_at", row.getLocalDateTime("requested_at").toString()); // si tu as un champ
|
||||
// de type timestamp
|
||||
jsonArray.add(json);
|
||||
});
|
||||
|
||||
context.response()
|
||||
.putHeader("Content-Type", "application/json")
|
||||
.end(jsonArray.encode());
|
||||
} else {
|
||||
insertRes.cause().printStackTrace(); // Pour voir l'erreur en console
|
||||
context.response().setStatusCode(500).end(new JsonObject().put("error", "Erreur insertion").encode());
|
||||
res.cause().printStackTrace();
|
||||
context.response().setStatusCode(500)
|
||||
.end(new JsonObject().put("error", "Erreur lors de la récupération des demandes").encode());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
package com.example.starter;
|
||||
|
||||
import io.vertx.core.json.JsonArray;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.sqlclient.Row;
|
||||
import io.vertx.sqlclient.RowSet;
|
||||
|
||||
public class SetRequestDeleteObject {
|
||||
private final DatabaseService databaseService;
|
||||
|
||||
// Le nom du constructeur doit correspondre au nom de la classe
|
||||
public SetRequestDeleteObject(DatabaseService dtbS) {
|
||||
this.databaseService = dtbS;
|
||||
}
|
||||
|
||||
public void getAllDeletionRequests(RoutingContext context) {
|
||||
String query = "SELECT * FROM deletion_requests";
|
||||
|
||||
databaseService.pool.query(query).execute(res -> {
|
||||
if (res.succeeded()) {
|
||||
RowSet<Row> results = res.result();
|
||||
JsonArray jsonArray = new JsonArray();
|
||||
|
||||
results.forEach(row -> {
|
||||
JsonObject json = new JsonObject()
|
||||
.put("id", row.getInteger("id"))
|
||||
.put("object_id", row.getInteger("object_id"))
|
||||
.put("requested_by", row.getInteger("requested_by"))
|
||||
.put("request_date", row.getLocalDateTime("request_date").toString()); // Assurez-vous que le type correspond
|
||||
jsonArray.add(json);
|
||||
});
|
||||
|
||||
context.response()
|
||||
.putHeader("Content-Type", "application/json")
|
||||
.end(jsonArray.encode());
|
||||
} else {
|
||||
res.cause().printStackTrace();
|
||||
context.response().setStatusCode(500).end(new JsonObject().put("error", "Erreur lors de la récupération des demandes").encode());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,36 +1,30 @@
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useEffect } from "react";
|
||||
import { Check, X } from "lucide-react";
|
||||
import { useAuth } from "../AuthContext";
|
||||
|
||||
function Alert({ affAlert, setAffAlert, message }) {
|
||||
const { user } = useAuth();
|
||||
|
||||
// Gère la disparition de l'alerte après 3 secondes
|
||||
useEffect(() => {
|
||||
if (affAlert) {
|
||||
const timer = setTimeout(() => {
|
||||
setAffAlert(false); // Cache l'alerte après 3 secondes
|
||||
setAffAlert(false);
|
||||
}, 3000);
|
||||
return () => clearTimeout(timer); // Nettoie le timer au besoin
|
||||
return () => clearTimeout(timer);
|
||||
}
|
||||
}, [affAlert, setAffAlert]);
|
||||
|
||||
return (
|
||||
affAlert && user?.role !== "user" && (
|
||||
<div className="flex flex-col md:flex-row bg-slate-600 w-full md:w-1/2 lg:w-1/3 fixed top-20 right-1 sm:right-4 rounded-lg p-4 md:p-5 items-center gap-4 md:gap-6 shadow-lg opacity-90">
|
||||
<div className="fixed top-6 right-4 z-50 bg-slate-700 text-white px-6 py-4 rounded-xl shadow-lg flex items-center gap-4 w-[90%] sm:w-[350px]">
|
||||
<Check className="text-green-400 w-6 h-6 flex-shrink-0" />
|
||||
<p className="text-sm sm:text-base flex-grow">{message}</p>
|
||||
<button
|
||||
onClick={() => setAffAlert(false)}
|
||||
className="absolute top-2 right-2 text-white hover:text-gray-300"
|
||||
className="text-white hover:text-gray-300"
|
||||
>
|
||||
<X />
|
||||
</button>
|
||||
|
||||
<Check className="text-green-700 w-12 h-12 md:w-16 md:h-16" />
|
||||
|
||||
|
||||
<p className="text-sm md:text-base text-white text-center md:text-left">
|
||||
{message}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
);
|
||||
|
||||
@ -9,6 +9,7 @@ function AdminObjet() {
|
||||
const [categories, setCategories] = useState();
|
||||
const [newCategory, setNewCategory] = useState("");
|
||||
const [objects, setObjects] = useState([]);
|
||||
const [deleteRequests, setDeleteRequests] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
axios.get(`${API_BASE_URL}/objets`).then((response) => {
|
||||
@ -16,6 +17,20 @@ function AdminObjet() {
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
axios
|
||||
.get(`${API_BASE_URL}/getDemandeSuppression`)
|
||||
.then((response) => {
|
||||
setDeleteRequests(response.data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(
|
||||
"Erreur lors de la récupération des requêtes de suppression :",
|
||||
error
|
||||
);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleAddCategory = () => {
|
||||
const trimmed = newCategory.trim();
|
||||
if (trimmed !== "" && !categories.includes(trimmed)) {
|
||||
@ -315,6 +330,62 @@ function AdminObjet() {
|
||||
Enregistrer les règles globales
|
||||
</button>
|
||||
</section>
|
||||
|
||||
|
||||
<section className="bg-white p-6 rounded-xl shadow-md mt-12">
|
||||
<h2 className="text-2xl font-semibold mb-4">
|
||||
Demandes de Suppression d'Objets
|
||||
</h2>
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full min-w-[640px] divide-y divide-gray-200">
|
||||
<thead>
|
||||
<tr>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Objet ID</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Utilisateur ID</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Date de Requête</th>
|
||||
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody className="bg-white divide-y divide-gray-200">
|
||||
{deleteRequests.map((request) => (
|
||||
<tr key={request.id}>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{request.id}</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{request.object_id}</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{request.requested_by}</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{request.request_date}</td>
|
||||
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-900 flex gap-2">
|
||||
<button
|
||||
className="bg-green-500 hover:bg-green-600 text-white px-3 py-1 rounded-md text-sm"
|
||||
onClick={() => handleAccept(request.id)}
|
||||
>
|
||||
Accepter
|
||||
</button>
|
||||
<button
|
||||
className="bg-red-500 hover:bg-red-600 text-white px-3 py-1 rounded-md text-sm"
|
||||
onClick={() => handleReject(request.id)}
|
||||
>
|
||||
Refuser
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
{deleteRequests.length === 0 && (
|
||||
<tr>
|
||||
<td
|
||||
colSpan="5"
|
||||
className="px-6 py-4 text-center text-sm text-gray-500"
|
||||
>
|
||||
Aucune demande de suppression trouvée.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -43,6 +43,7 @@ const initialWidgets = [
|
||||
{ id: 3, type: "reporting" },
|
||||
{ id: 4, type: "adminobjet" },
|
||||
{ id: 5, type: "objects" },
|
||||
{ id: 6, type: "requestObject" }
|
||||
];
|
||||
|
||||
function Dashboard() {
|
||||
@ -70,6 +71,10 @@ function Dashboard() {
|
||||
axios.get(`${API_BASE_URL}/objets`).then((response) => {
|
||||
setAdminObjects(response.data);
|
||||
});
|
||||
|
||||
axios.get(`${API_BASE_URL}/demandeSuppression`).then((response) => {
|
||||
setRequestDeleteObject(response.data);
|
||||
})
|
||||
}, []);
|
||||
|
||||
const [adminObjects, setAdminObjects] = useState([
|
||||
@ -284,6 +289,25 @@ function Dashboard() {
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{widget.type ==="requestObject" && (
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold mb-4">Requête suppression objets</h2>
|
||||
|
||||
<div className="mb-4">
|
||||
<p className="text-gray-700 mb-2">Générer des rapports d'utilisation :</p>
|
||||
<div className="flex gap-4">
|
||||
<button
|
||||
className="bg-indigo-600 text-white px-4 py-2 rounded-md hover:bg-indigo-700"
|
||||
onClick={() => exportCSV()}
|
||||
>
|
||||
Requête objets
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
)}
|
||||
{widget.type === "reporting" && (
|
||||
<div>
|
||||
<h2 className="text-xl font-semibold mb-4">Rapports et Statistiques</h2>
|
||||
@ -372,6 +396,12 @@ function Dashboard() {
|
||||
>
|
||||
Rapports et Statistiques
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleWidgetSelection("requestObject")}
|
||||
className="px-4 py-2 bg-gray-200 hover:bg-gray-300 rounded-md text-left"
|
||||
>
|
||||
Demande de suppression d'objets
|
||||
</button>
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setShowAddWidgetModal(false)}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user