diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index ef8d610..11d5f6d 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -8,6 +8,7 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; + import javax.persistence.Table; import com.covas.Classes.Hash; @@ -56,6 +57,10 @@ public class UsersEntity extends PanacheEntityBase { return find("pseudo", pseudo).firstResult(); } + public static UsersEntity findByEmail(String email){ + return find("email", email).firstResult(); + } + public static void add(String pseudo, String email, String name, String firstName, LocalDate birth, String password, String roles){ UsersEntity users = new UsersEntity(); diff --git a/src/main/java/com/covas/Json/UserExist.java b/src/main/java/com/covas/Json/UserExist.java new file mode 100644 index 0000000..da737b8 --- /dev/null +++ b/src/main/java/com/covas/Json/UserExist.java @@ -0,0 +1,16 @@ +package com.covas.Json; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class UserExist { + + public final Boolean email; + public final Boolean pseudo; + + public UserExist(Boolean email, Boolean pseudo){ + this.email = email; + this.pseudo = pseudo; + } + +} diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 633b592..3f4b8c4 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -9,6 +9,7 @@ import javax.annotation.security.RolesAllowed; import javax.inject.Inject; import javax.transaction.Transactional; import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.core.Response.Status; import javax.ws.rs.Consumes; import javax.ws.rs.CookieParam; import javax.ws.rs.DELETE; @@ -26,6 +27,7 @@ import javax.ws.rs.core.Response; import com.covas.Classes.Hash; import com.covas.Entity.UsersEntity; +import com.covas.Json.UserExist; import com.covas.Json.UserSingle; import io.quarkus.panache.common.Page; @@ -150,10 +152,23 @@ public class UsersRessources { @Transactional public Response createUser(UsersEntity users) { Response.Status status = Response.Status.OK; - UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); - if (usersOrig != null) { + UsersEntity usersPseudo = UsersEntity.findByPseudo(users.pseudo); + UsersEntity usersEmail = UsersEntity.findByPseudo(users.email); + Boolean createUserBool = true; + Boolean pseudoExist = false; + Boolean emailExist = false; + if (usersPseudo != null) { + createUserBool = false; status = Response.Status.UNAUTHORIZED; - } else { + pseudoExist = true; + } + if(usersEmail != null){ + createUserBool = false; + status = Response.Status.UNAUTHORIZED; + emailExist = true; + } + UserExist userExist = new UserExist(emailExist, pseudoExist); + if(createUserBool) { UsersEntity usersNew = new UsersEntity(); usersNew.name = users.name; usersNew.pseudo = users.pseudo; @@ -174,7 +189,7 @@ public class UsersRessources { status = Response.Status.NO_CONTENT; } } - return Response.status(status).build(); + return Response.status(status).entity(userExist).build(); }