diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..2f7896d1d1365eafb0da03d9fe456fac81408487 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/src/main/java/forms/CommentForm.java b/src/main/java/forms/CommentForm.java new file mode 100644 index 0000000000000000000000000000000000000000..3bcc80782f6c575d3a175c514f0db1374d59bda6 --- /dev/null +++ b/src/main/java/forms/CommentForm.java @@ -0,0 +1,61 @@ +package forms; + +import beans.Comment; +import beans.Post; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Axel L. + */ +public class CommentForm { + + private static final int MAX_COMMENT = 255; // Longueur max du commentaire + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Comment createComment(HttpServletRequest req, Web_user author, Post post) { + String content = req.getParameter("content"); + content = (content == null) ? null : content.trim(); + + Comment comment = new Comment(); + comment.setContent(content); + comment.setAuthor(author); + comment.setPost(post); + + try { + validateContent(content); + } catch (Exception ex) { + errors.put("content", ex.getMessage()); + } + + if (errors.isEmpty()) { + // Partie Dao + result = "Commentaire publié"; + } else { + result = "Échec lors de l'ajout du commentaire"; + } + return comment; + } + + private void validateContent(String content) throws Exception { + if (content == null) { + throw new Exception("Votre commentaire est vide"); + } + if (content.length() > MAX_COMMENT) { + throw new Exception("Le commentaire doit faire maximum " + MAX_COMMENT + " caractères"); + } + } +} diff --git a/src/main/java/forms/ConnexionForm.java b/src/main/java/forms/ConnexionForm.java index 1aaa296f7f9b26cbd8928654c41cb9d997e3681f..73163e41e812271a0460391d8aea559ecdde9687 100644 --- a/src/main/java/forms/ConnexionForm.java +++ b/src/main/java/forms/ConnexionForm.java @@ -1,9 +1,94 @@ package forms; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + /** * * @author Axel L. */ public class ConnexionForm { + + private static final String EMAIL = "email"; + private static final String PASSWORD = "password"; + private static final int MIN_PASSWORD = 8; + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Web_user connectUser(HttpServletRequest req) { + String email = getParamValue(req, EMAIL); + String password = getParamValue(req, PASSWORD); + + Web_user user = new Web_user(); + + try { + validateEmail(email); + } catch (Exception ex) { + setError(EMAIL, ex.getMessage()); + } + user.setEmail(email); + + try { + validatePassword(password); + } catch (Exception ex) { + setError(PASSWORD, ex.getMessage()); + } + user.setPassword(password); + + if (errors.isEmpty()) { + // Appel DAO + result = "Succès de la connexion"; + } else { + result = "Échec de la connexion"; + } + return user; + } + + // Vérifie si l'email est valide + private void validateEmail(String email) throws Exception { + if (email == null) { + throw new Exception("Merci de saisir une adresse email"); + + } else if (!email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)")) { + throw new Exception("Merci de saisir une adresse email valide"); + } + } + + // Vérifie le mot de passe + private void validatePassword(String password) throws Exception { + if (password != null) { + if (password.length() < MIN_PASSWORD) { + throw new Exception("Le mot de passe doit contenir au moins " + MIN_PASSWORD + " caractères"); + } + } else { + throw new Exception("Merci de saisir votre mot de passe"); + } + } + + // Ajoute un message correspondant au champ spécifié à la map des erreurs + private void setError(String field, String message) { + errors.put(field, message); + } + + // Méthode utilitaire qui retourne null si un champ est .. null + private static String getParamValue(HttpServletRequest req, String paramKey) { + String value = req.getParameter(paramKey); + if (value == null || value.trim().length() == 0) { + return null; + } else { + return value.trim(); + } + } } diff --git a/src/main/java/forms/InscriptionForm.java b/src/main/java/forms/InscriptionForm.java index 8b935f5a8718c48001721285966184139a9ca680..2757c1709884638d3b9db4eaaca3f6518641c00a 100644 --- a/src/main/java/forms/InscriptionForm.java +++ b/src/main/java/forms/InscriptionForm.java @@ -1,9 +1,117 @@ package forms; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + /** * * @author Axel L. */ public class InscriptionForm { - + + private static final String EMAIL = "email"; + private static final String PASSWORD = "password"; + private static final String USERNAME = "username"; + + // Variables données + private static final int MIN_PASSWORD = 8; // Variable pour le nombre de caractères minimum pour le mot de passe + private static final int MIN_USERNAME = 3; // Variable pour le nombre de caractères minimum pour le pseudo + private static final int MAX_USERNAME = 50; // Variable pour le nombre de caractères maximum pour le pseudo + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Web_user submitUser(HttpServletRequest req) { + String email = getParamValue(req, EMAIL); + String password = req.getParameter(PASSWORD); + String confirmPassword = req.getParameter("confirmPassword"); + String username = getParamValue(req, USERNAME); + + Web_user user = new Web_user(); + user.setEmail(email); + user.setUsername(username); + + try { + validateEmail(email); + } catch (Exception ex) { + setError(EMAIL, ex.getMessage()); + } + user.setEmail(email); + + try { + validatePassword(password); + } catch (Exception ex) { + setError(PASSWORD, ex.getMessage()); + } + user.setPassword(password); + + try { + validateName(username); + } catch (Exception ex) { + setError(USERNAME, ex.getMessage()); + } + user.setUsername(username); + + if (errors.isEmpty()) { + // Partie Dao + result = "Succès de l'inscription"; + } else { + result = "Échec de l'inscription"; + } + return user; + } + + // Vérifie si l'email est valide + private void validateEmail(String email) throws Exception { + if (email == null) { + throw new Exception("Merci de saisir une adresse email"); + + } else if (!email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)")) { + throw new Exception("Merci de saisir une adresse email valide"); + } + } + + // Vérifie le mot de passe + private void validatePassword(String password) throws Exception { + if (password != null) { + if (password.length() < MIN_PASSWORD) { + throw new Exception("Le mot de passe doit contenir au moins " + MIN_PASSWORD + " caractères"); + } + } else { + throw new Exception("Merci de saisir votre mot de passe"); + } + } + + // Vérifie le pseudo / nom + private void validateName(String username) throws Exception { + if (username == null) { + throw new Exception("Merci de choisir un nom d'utilisateur"); + } + + if (username.length() < MIN_USERNAME || username.length() > MAX_USERNAME) { + throw new Exception("Le nom d'utilisateur doit contenir entre " + MIN_USERNAME + " et " + MAX_USERNAME + "caractères"); + } + } + + private void setError(String field, String message) { + errors.put(field, message); + } + + private static String getParamValue(HttpServletRequest req, String paramKey) { + String value = req.getParameter(paramKey); + if(value == null || value.trim().isEmpty()) { + return null; + } + return value.trim(); + } } diff --git a/src/main/java/forms/ModificationForm.java b/src/main/java/forms/ModificationForm.java new file mode 100644 index 0000000000000000000000000000000000000000..fa480a82c00c89b6ebc3086726c93004bfad7944 --- /dev/null +++ b/src/main/java/forms/ModificationForm.java @@ -0,0 +1,69 @@ +package forms; + +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Axel L. + */ +public class ModificationForm { + + private static final String CURRENT = "currentPassword"; + private static final String PASSWORD = "password"; + private static final int MIN_PASSWORD = 8; + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Web_user modifyUser(HttpServletRequest req, Web_user sessionUser) { + String current = req.getParameter(CURRENT); + String password = req.getParameter(PASSWORD); + String confirmPassword = req.getParameter("confirmPassword"); + + //DAO + try { + validatePassword(password, confirmPassword); + } catch (Exception ex) { + setError(PASSWORD, ex.getMessage()); + } + if (errors.isEmpty()) { + // Partie Dao + result = "Mot de passe modifié"; + } else { + result = "Échec de la modification"; + } + // Provisoire + Web_user daoUser = null; + return daoUser; + } + + // Vérifie le mot de passe + private void validatePassword(String password, String confirmPassword) throws Exception { + if (password != null) { + if (password.length() < MIN_PASSWORD) { + throw new Exception("Le mot de passe doit contenir au moins " + MIN_PASSWORD + " caractères"); + } + } else { + throw new Exception("Merci de saisir votre mot de passe"); + } + if (!password.equals(confirmPassword)) { + throw new Exception("Les mots de passe sont différents"); + } + } + + // Ajoute un message correspondant au champ spécifié à la map des erreurs + private void setError(String field, String message) { + errors.put(field, message); + } +} diff --git a/src/main/java/forms/PostForm.java b/src/main/java/forms/PostForm.java index 1815e80b58dd26a9377119ba1571981ad005c9ba..4a70810e79399ee6e71bd28b62f1a20aed73b992 100644 --- a/src/main/java/forms/PostForm.java +++ b/src/main/java/forms/PostForm.java @@ -1,9 +1,111 @@ package forms; +import beans.Post; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + /** * * @author Axel L. */ public class PostForm { - + + private static final String TITLE = "title"; + private static final String DESCRIPTION = "description"; + private static final String VIDEO = "videoUrl"; + + private static final int MAX_TITLE = 50; // Longueur Max du titre + private static final int MAX_CONTENT = 500; // Longueur MAX de la description + private static final int MAX_VIDEO = 255; // Longueur du lien de la vidéo + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Post createPost(HttpServletRequest req, Web_user author) { + + String title = getParamValue(req, TITLE); + String description = getParamValue(req, DESCRIPTION); + String videoUrl = getParamValue(req, VIDEO); + + Post post = new Post(); + post.setAuthor(author); + + try { + validateTitle(title); + } catch (Exception ex) { + setError(TITLE, ex.getMessage()); + } + post.setTitle(title); + + try { + validateContent(description); + } catch (Exception ex) { + setError(DESCRIPTION, ex.getMessage()); + } + post.setDescription(description); + + try { + validateVideo(videoUrl); + } catch (Exception ex) { + setError(VIDEO, ex.getMessage()); + } + post.setVideoUrl(videoUrl); + + if (errors.isEmpty()) { + // Partie Dao + result = "Post publié"; + } else { + result = "Échec de la publication"; + } + return post; + } + + private void validateTitle(String title) throws Exception { + if (title == null) { + throw new Exception("Merci de donner un titre à votre publication"); + } + if (title.length() > MAX_TITLE) { + throw new Exception("Le titre ne doit pas dépasser " + MAX_TITLE + "caractères"); + } + } + + private void validateContent(String description) throws Exception { + if (description == null) { + throw new Exception("Merci d'ajouter une description"); + } + if (description.length() > MAX_CONTENT) { + throw new Exception("La description ne doit pas dépasser " + MAX_CONTENT + "caractères"); + } + } + + private void validateVideo(String videoUrl) throws Exception { + if (videoUrl == null) { + throw new Exception("Merci de fournir un lien pour votre vidéo"); + } + if (videoUrl.length() > MAX_VIDEO) { + throw new Exception("Ce lien n'est pas correct"); + } + } + + private void setError(String field, String message) { + errors.put(field, message); + } + + private static String getParamValue(HttpServletRequest req, String paramKey) { + String value = req.getParameter(paramKey); + if (value == null || value.trim().isEmpty()) { + return null; + } + return value.trim(); + } } diff --git a/target/MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf b/target/MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf new file mode 100644 index 0000000000000000000000000000000000000000..31aa64251bcb9f08e05b5661e280c54364efe659 --- /dev/null +++ b/target/MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf @@ -0,0 +1,3 @@ +<%-- any content can be specified here e.g.: --%> +<%@ page pageEncoding="UTF-8" %> +

Footer

diff --git a/target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css b/target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css new file mode 100644 index 0000000000000000000000000000000000000000..bd5be7ac10a2a9e879aecf7b802b3e4817d686fb --- /dev/null +++ b/target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css @@ -0,0 +1,9 @@ +/* +Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license +Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/CascadeStyleSheet.css to edit this template +*/ +/* + Created on : Oct 6, 2026, 3:04:31 PM + Author : Axel L. +*/ +