Refactoring etamélioration
This commit is contained in:
@@ -7,7 +7,6 @@ import jakarta.validation.constraints.NotNull;
|
|||||||
import jakarta.validation.constraints.Size;
|
import jakarta.validation.constraints.Size;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
@@ -36,6 +35,7 @@ public class Events extends BaseEntity {
|
|||||||
@JsonProperty("date")
|
@JsonProperty("date")
|
||||||
private LocalDateTime eventDate;
|
private LocalDateTime eventDate;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@Size(max = 100)
|
@Size(max = 100)
|
||||||
@Column(name = "location", nullable = false, length = 100)
|
@Column(name = "location", nullable = false, length = 100)
|
||||||
@JsonProperty("location")
|
@JsonProperty("location")
|
||||||
@@ -54,36 +54,26 @@ public class Events extends BaseEntity {
|
|||||||
@JsonProperty("imageUrl")
|
@JsonProperty("imageUrl")
|
||||||
private String imageUrl;
|
private String imageUrl;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Column(name = "status", nullable = false)
|
||||||
|
@JsonProperty("status")
|
||||||
|
private String status = "OPEN"; // Par défaut, un événement est "OUVERT".
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "creator_id")
|
@JoinColumn(name = "creator_id", nullable = false)
|
||||||
@JsonProperty("creator")
|
@JsonProperty("creator")
|
||||||
private Users creator;
|
private Users creator;
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "participatedEvents", fetch = FetchType.LAZY)
|
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
|
||||||
|
@JoinTable(
|
||||||
|
name = "event_participants",
|
||||||
|
joinColumns = @JoinColumn(name = "event_id"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name = "user_id")
|
||||||
|
)
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private Set<Users> participants = new HashSet<>();
|
private Set<Users> participants = new HashSet<>();
|
||||||
|
|
||||||
// Méthode pour ajouter un participant
|
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
|
||||||
public void addParticipant(Users user) {
|
|
||||||
if (participants == null) {
|
|
||||||
participants = new HashSet<>();
|
|
||||||
}
|
|
||||||
if (!participants.contains(user)) {
|
|
||||||
participants.add(user);
|
|
||||||
user.getParticipatedEvents().add(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Méthode pour retirer un participant
|
|
||||||
public void removeParticipant(Users user) {
|
|
||||||
if (participants != null && participants.contains(user)) {
|
|
||||||
participants.remove(user);
|
|
||||||
user.getParticipatedEvents().remove(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Si vous avez une fonctionnalité de "like", vous pouvez ajouter une collection et des méthodes similaires
|
|
||||||
@ManyToMany(fetch = FetchType.LAZY)
|
|
||||||
@JoinTable(
|
@JoinTable(
|
||||||
name = "event_likes",
|
name = "event_likes",
|
||||||
joinColumns = @JoinColumn(name = "event_id"),
|
joinColumns = @JoinColumn(name = "event_id"),
|
||||||
@@ -92,17 +82,32 @@ public class Events extends BaseEntity {
|
|||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private Set<Users> likes = new HashSet<>();
|
private Set<Users> likes = new HashSet<>();
|
||||||
|
|
||||||
public void addLike(Users user) {
|
public void addParticipant(Users user) {
|
||||||
if (likes == null) {
|
participants.add(user);
|
||||||
likes = new HashSet<>();
|
user.getParticipatedEvents().add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeParticipant(Users user) {
|
||||||
|
participants.remove(user);
|
||||||
|
user.getParticipatedEvents().remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addLike(Users user) {
|
||||||
likes.add(user);
|
likes.add(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeLike(Users user) {
|
public void removeLike(Users user) {
|
||||||
if (likes != null) {
|
|
||||||
likes.remove(user);
|
likes.remove(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PreRemove
|
||||||
|
private void preRemove() {
|
||||||
|
for (Users participant : participants) {
|
||||||
|
participant.getParticipatedEvents().remove(this);
|
||||||
|
}
|
||||||
|
for (Users like : likes) {
|
||||||
|
like.getLikedEvents().remove(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -116,6 +121,7 @@ public class Events extends BaseEntity {
|
|||||||
", category='" + category + '\'' +
|
", category='" + category + '\'' +
|
||||||
", link='" + link + '\'' +
|
", link='" + link + '\'' +
|
||||||
", imageUrl='" + imageUrl + '\'' +
|
", imageUrl='" + imageUrl + '\'' +
|
||||||
|
", status='" + status + '\'' +
|
||||||
", creator=" + (creator != null ? creator.getId() : null) +
|
", creator=" + (creator != null ? creator.getId() : null) +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ import jakarta.validation.constraints.Size;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.List;
|
import java.util.HashSet;
|
||||||
import java.util.UUID;
|
import java.util.Set;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@@ -20,6 +22,8 @@ import lombok.Setter;
|
|||||||
@Table(name = "Users")
|
@Table(name = "Users")
|
||||||
public class Users extends BaseEntity {
|
public class Users extends BaseEntity {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(Users.class);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Size(max = 100)
|
@Size(max = 100)
|
||||||
@Column(name = "nom", nullable = false, length = 100)
|
@Column(name = "nom", nullable = false, length = 100)
|
||||||
@@ -42,7 +46,7 @@ public class Users extends BaseEntity {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@Size(min = 8, max = 255)
|
@Size(min = 8, max = 255)
|
||||||
@Column(name = "mot_de_passe", nullable = false, length = 255)
|
@Column(name = "mot_de_passe", nullable = false, length = 255)
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
|
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
|
||||||
private String motDePasse;
|
private String motDePasse;
|
||||||
|
|
||||||
@Size(max = 50)
|
@Size(max = 50)
|
||||||
@@ -50,35 +54,65 @@ public class Users extends BaseEntity {
|
|||||||
@JsonProperty("role")
|
@JsonProperty("role")
|
||||||
private String role;
|
private String role;
|
||||||
|
|
||||||
|
// Relation avec les événements créés par l'utilisateur
|
||||||
@OneToMany(mappedBy = "creator", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "creator", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private List<Events> createdEvents;
|
private Set<Events> createdEvents = new HashSet<>();
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.LAZY)
|
// Relation avec les événements auxquels l'utilisateur participe
|
||||||
|
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
|
||||||
@JoinTable(
|
@JoinTable(
|
||||||
name = "User_Event_Participation",
|
name = "User_Event_Participation",
|
||||||
joinColumns = @JoinColumn(name = "user_id"),
|
joinColumns = @JoinColumn(name = "user_id"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "event_id")
|
inverseJoinColumns = @JoinColumn(name = "event_id")
|
||||||
)
|
)
|
||||||
@JsonIgnore
|
@JsonIgnore
|
||||||
private List<Events> participatedEvents;
|
private Set<Events> participatedEvents = new HashSet<>();
|
||||||
|
|
||||||
// Méthode pour définir un mot de passe haché avec SHA-256
|
// Relation avec les événements "likés" par l'utilisateur
|
||||||
|
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
|
||||||
|
@JoinTable(
|
||||||
|
name = "User_Event_Likes",
|
||||||
|
joinColumns = @JoinColumn(name = "user_id"),
|
||||||
|
inverseJoinColumns = @JoinColumn(name = "event_id")
|
||||||
|
)
|
||||||
|
@JsonIgnore
|
||||||
|
private Set<Events> likedEvents = new HashSet<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Définit le mot de passe de l'utilisateur en le hachant avec l'algorithme SHA-256.
|
||||||
|
*
|
||||||
|
* @param motDePasse Le mot de passe en clair que l'utilisateur souhaite utiliser.
|
||||||
|
*/
|
||||||
public void setMotDePasse(String motDePasse) {
|
public void setMotDePasse(String motDePasse) {
|
||||||
|
logger.debug("Définition du mot de passe pour l'utilisateur {}", email);
|
||||||
this.motDePasse = hashPasswordSHA256(motDePasse);
|
this.motDePasse = hashPasswordSHA256(motDePasse);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hachage du mot de passe avec SHA-256
|
/**
|
||||||
|
* Hache le mot de passe en utilisant l'algorithme SHA-256.
|
||||||
|
*
|
||||||
|
* @param motDePasse Le mot de passe en clair.
|
||||||
|
* @return Le mot de passe haché en format hexadécimal.
|
||||||
|
*/
|
||||||
private String hashPasswordSHA256(String motDePasse) {
|
private String hashPasswordSHA256(String motDePasse) {
|
||||||
|
logger.debug("Hachage du mot de passe pour l'utilisateur {}", email);
|
||||||
try {
|
try {
|
||||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||||
byte[] encodedhash = digest.digest(motDePasse.getBytes(StandardCharsets.UTF_8));
|
byte[] encodedhash = digest.digest(motDePasse.getBytes(StandardCharsets.UTF_8));
|
||||||
return bytesToHex(encodedhash);
|
return bytesToHex(encodedhash);
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
logger.error("Erreur lors du hachage du mot de passe pour l'utilisateur {}: {}", email, e.getMessage());
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convertit un tableau de bytes en une chaîne hexadécimale.
|
||||||
|
*
|
||||||
|
* @param hash Le tableau de bytes à convertir.
|
||||||
|
* @return La chaîne hexadécimale correspondante.
|
||||||
|
*/
|
||||||
private String bytesToHex(byte[] hash) {
|
private String bytesToHex(byte[] hash) {
|
||||||
StringBuilder hexString = new StringBuilder(2 * hash.length);
|
StringBuilder hexString = new StringBuilder(2 * hash.length);
|
||||||
for (byte b : hash) {
|
for (byte b : hash) {
|
||||||
@@ -91,6 +125,23 @@ public class Users extends BaseEntity {
|
|||||||
return hexString.toString();
|
return hexString.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Méthode appelée avant la suppression d'un utilisateur. Elle retire cet utilisateur des événements
|
||||||
|
* auxquels il participe ou qu'il a "liké" pour éviter des violations de contraintes de clé étrangère.
|
||||||
|
*/
|
||||||
|
@PreRemove
|
||||||
|
private void preRemove() {
|
||||||
|
logger.info("Préparation à la suppression de l'utilisateur {}", email);
|
||||||
|
for (Events event : participatedEvents) {
|
||||||
|
event.getParticipants().remove(this);
|
||||||
|
logger.debug("L'utilisateur {} a été retiré de l'événement {}", email, event.getId());
|
||||||
|
}
|
||||||
|
for (Events event : likedEvents) {
|
||||||
|
event.getLikes().remove(this);
|
||||||
|
logger.debug("L'utilisateur {} a été retiré des 'likes' de l'événement {}", email, event.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Users{" +
|
return "Users{" +
|
||||||
|
|||||||
@@ -195,4 +195,30 @@ public class EventsResource {
|
|||||||
event.removeLike(user);
|
event.removeLike(user);
|
||||||
return Response.ok(event).build();
|
return Response.ok(event).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Path("{eventId}/close")
|
||||||
|
@Transactional
|
||||||
|
@Operation(summary = "Fermer un événement", description = "Ferme un événement existant")
|
||||||
|
public Response closeEvent(@PathParam("eventId") UUID eventId) {
|
||||||
|
Events event = eventsRepository.findById(eventId);
|
||||||
|
|
||||||
|
if (event == null) {
|
||||||
|
return Response.status(Response.Status.NOT_FOUND)
|
||||||
|
.entity("Événement non trouvé.")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ("CLOSED".equals(event.getStatus())) {
|
||||||
|
return Response.status(Response.Status.BAD_REQUEST)
|
||||||
|
.entity("L'événement est déjà fermé.")
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
event.setStatus("CLOSED");
|
||||||
|
eventsRepository.persist(event);
|
||||||
|
|
||||||
|
return Response.ok("Événement fermé avec succès.").build();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user