Browse Source

Correction petit bug et legere amelioration

Arthur Brandao 5 năm trước cách đây
mục cha
commit
a4850f19c0

+ 11 - 9
WebContent/abonnes.jsp

@@ -93,9 +93,10 @@
         	const conf = M.Modal.getInstance($('#confirmation'));
         	const idPagination = 'usager';
         	const perPage = 5;
-        	let usagerToDelete = null;
+        	var page = 1;
         	
         	function pagine(numPage) {
+        		numPage = numPage || page;
         		loader.open();
                 $.ajax({
                     type: "GET",
@@ -105,7 +106,7 @@
                     	pagination(idPagination, numPage, perPage, 0, $('#abo'), $('#pagination-abo'), []);
                 		let html = '<tr>';
                 		html += '<td class="right-align red-text" width="40%"><i class="material-icons" style="font-size:4em;padding-top:.3em;">error</i></td>';
-                		html += '<td class="left-align red-text" width="60%"><h4>Impossible de charger les usage</td>';
+                		html += '<td class="left-align red-text" width="60%"><h4>Impossible de charger les usager</td>';
                 		html += '</tr>';
                 		$('#pagination-' + idPagination + '-table').html(html);
                     },
@@ -126,26 +127,28 @@
                 });
         	};
         	
-			pagine(1);
+			pagine(page);
             // Detection changement page
             $('main').on('click', '.pagination-' + idPagination + '-prev', function () {
                 console.log(idPagination, 'prev', $(this).attr('data-page'));
-                pagine($(this).attr('data-page'));
+                page = $(this).attr('data-page');
+                pagine(page);
             });
             $('main').on('click', '.pagination-' + idPagination + '-next', function () {
                 console.log('abo', 'next', $(this).attr('data-page'));
-                pagine($(this).attr('data-page'));
+                page = $(this).attr('data-page');
+                pagine(page);
             });
             $('main').on('click', '.pagination-' + idPagination + '-number', function () {
                 console.log('abo', 'num', $(this).attr('data-page'));
-                pagine($(this).attr('data-page'));
+                page = $(this).attr('data-page');
+                pagine(page);
             });
             
             // Suppr abonne
             $('#abo').on('click', '.delete-usager', function () {
             	$("#conf-user").html($(this).attr('data-usager'));
             	$("#conf-delete").attr('data-code', $(this).attr('data-code'));
-            	usagerToDelete = $(this).parent().parent();
             	conf.open();
             });
             $('#conf-delete').on('click', function () {
@@ -163,8 +166,7 @@
                     	if (result.success) {
                     		$('#ok-text').html(result.data.msg);
                     		okPopup.open();
-                    		usagerToDelete.remove();
-                    		usagerToDelete = null;
+                    		pagine();
                     	} else {
                     		$('#error-text').html(result.data);
                         	errPopup.open();

+ 10 - 1
WebContent/js/script.js

@@ -24,6 +24,9 @@ function pagination(id, pageNum, perPage, total, tabElt, paginationElt, dataPage
     perPage = parseInt(perPage);
     total = parseInt(total);
     tableClass = tableClass || 'striped';
+    if (pageNum < 1) {
+    	pageNum = 1;
+    }
     // Création du tableau
     let tab = `<table id="pagination-${id}-table" class="${tableClass}">`;
     let first = true;
@@ -65,6 +68,10 @@ function pagination(id, pageNum, perPage, total, tabElt, paginationElt, dataPage
     tab += '</tbody></table>';
     // Creation de la pagination
     const nbPage = Math.ceil(total / perPage);
+    if (pageNum > nbPage) {
+    	// Si il n' y a pas assez de page pour aller à la page demandé on va au maximum
+    	pageNum = nbPage
+    }
     let pagination = `<ul class="pagination" data-active="${pageNum}">`;
     if(pageNum == 1) {
         pagination += '<li class="disabled"><span><i class="material-icons">chevron_left</i></span></li>';
@@ -87,6 +94,8 @@ function pagination(id, pageNum, perPage, total, tabElt, paginationElt, dataPage
     // Affichage
     tabElt.html(tab);
     paginationElt.html(pagination);
+    // Retourne la page courante
+    return pageNum;
 }
 
 /* --- Initialisation modules --- */
@@ -109,7 +118,7 @@ $(document).ready(function(){
         onOpenEnd: (modal) => {
             setTimeout(() => { 
                 M.Modal.getInstance(modal).close();
-            }, 3000);
+            }, 1500);
         }
     });
     M.Modal.init($('.loader'), {

+ 13 - 6
src/db/Database.java

@@ -23,6 +23,7 @@ public class Database {
 	
 	static {
 		ini();
+		unset();
 		setup();
 	}
 	
@@ -80,7 +81,7 @@ public class Database {
 	
 	public static boolean query(String sql) {
 		try(Statement st = db.createStatement()) {
-			st.executeQuery(sql);
+			st.executeQuery(prepare(sql));
 			return true;
 		} catch (SQLException | IllegalStateException e) {
 			LOGGER.warning(e.getMessage());
@@ -90,7 +91,7 @@ public class Database {
 	
 	public static <T> Optional<T> query(String sql, ResultSetMapper<T> mapper) {
 		try(Statement st = db.createStatement()) {
-			ResultSet result = st.executeQuery(sql);
+			ResultSet result = st.executeQuery(prepare(sql));
 			T obj = mapper.map(result);
 			return (obj == null) ? Optional.empty() : Optional.of(obj);
 		} catch (SQLException | IllegalStateException e) {
@@ -100,7 +101,7 @@ public class Database {
 	}
 	
 	public static <T> Optional<T> query(String sql, List<Object> params, ResultSetMapper<T> mapper) {
-		try(PreparedStatement st = db.prepareStatement(sql)) {
+		try(PreparedStatement st = db.prepareStatement(prepare(sql))) {
 			int i = 1;
 			for(Object param : params) {
 				st.setObject(i++, param);
@@ -116,7 +117,7 @@ public class Database {
 	
 	public static boolean execute(String sql) {
 		try(Statement st = db.createStatement()) {
-			int result = st.executeUpdate(sql);
+			int result = st.executeUpdate(prepare(sql));
 			return result == 1;
 		} catch (SQLException e) {
 			LOGGER.warning(e.getMessage());
@@ -125,7 +126,7 @@ public class Database {
 	}
 	
 	public static boolean execute(String sql, List<Object> params) {
-		try(PreparedStatement st = db.prepareStatement(sql)) {
+		try(PreparedStatement st = db.prepareStatement(prepare(sql))) {
 			int i = 1;
 			for (Object param : params) {
 				st.setObject(i++, param);
@@ -140,7 +141,7 @@ public class Database {
 	
 	public static Map<String, Object> insert(String sql, List<Object> params, String[] idField) {
 		Map<String, Object> ids = new HashMap<>();
-		try(PreparedStatement st = db.prepareStatement(sql, idField)) {
+		try(PreparedStatement st = db.prepareStatement(prepare(sql), idField)) {
 			// Ajout parametre
 			int i = 1;
 			for(Object param : params) {
@@ -186,5 +187,11 @@ public class Database {
 			throw new IllegalStateException("Database Manager Class is not a child of ManagaDatabase Class or no getInstance method found", e);
 		}
 	}
+	
+	private static String prepare(String sqlQuery) {
+    	String result = sqlQuery.trim();
+    	result = result.charAt(result.length() - 1) == ';' ? result.substring(0, result.length() - 1) : result;
+    	return result;
+    }
 
 }

+ 11 - 3
src/microfolie/entry/rest/UsagerController.java

@@ -41,9 +41,13 @@ public class UsagerController {
 		LOGGER.info("Begin: Usager page (num: " + num + ", perPage: " + perPage + ")");
 		JSONObject data = new JSONObject();
 		// Récupération info
-		data.put("total", service.getTotalNumber());
+		long total = service.getTotalNumber();
+		while (total <= perPage * (num - 1)) {
+			num--;
+		}
 		List<UsagerDTO> usagers = service.getPage(num, perPage);
 		// Formattage des données
+		data.put("total", total);
 		JSONArray list = new JSONArray();
 		usagers.forEach(elt -> {
 			StringBuilder action = new StringBuilder();
@@ -71,13 +75,17 @@ public class UsagerController {
 	@DELETE
 	@Path("/{code}")
 	public String delete(@PathParam("code") String code) {
+		JSONObject result;
+		LOGGER.info("Begin: Usager delete (" + code + ")");
 		if (service.delete(code)) {
 			JSONObject data = new JSONObject();
 			data.put("msg", "Usager supprim&eacute;");
-			return JsonUtils.success(data).toString();
+			result = JsonUtils.success(data);
 		} else {
-			return JsonUtils.error("Erreur lors de la suppression de l'usager").toString();
+			result = JsonUtils.error("Erreur lors de la suppression de l'usager");
 		}
+		LOGGER.info("End: Usager delete");
+		return result.toString();
 	}
 
 }