From 9ef8bb92ffdc778617ec02b7af6624fd7ffa16df Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 28 May 2022 09:58:59 +0200 Subject: [PATCH] add coluln deleted_at and update uri for patch --- src/main/java/com/covas/Entity/UsersEntity.java | 2 ++ .../java/com/covas/Resources/UsersRessources.java | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index ce8979f..1f1d2e9 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -49,6 +49,8 @@ public class UsersEntity extends PanacheEntityBase { public LocalDateTime created_at; @Column(nullable = false) public LocalDateTime updated_at; + @ColumnDefault("null") + public LocalDateTime deleted_at; public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index 525749d..9392a36 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -158,10 +158,11 @@ public class UsersRessources { // PUT @PUT + @Path("{pseudo}") @RolesAllowed("Admin") @Consumes(MediaType.APPLICATION_JSON) @Transactional - public Response addUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response addUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("pseudo") String pseudo, UsersEntity users) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -169,13 +170,13 @@ public class UsersRessources { if (status.equals(Response.Status.OK)) { - UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); + UsersEntity usersOrig = UsersEntity.findByPseudo(pseudo); if (usersOrig != null) { status = Response.Status.UNAUTHORIZED; } else { UsersEntity usersNew = new UsersEntity(); usersNew.name = users.name; - usersNew.pseudo = users.pseudo; + usersNew.pseudo = pseudo; usersNew.firstName = users.firstName; usersNew.email = users.email; usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), @@ -210,6 +211,8 @@ public class UsersRessources { if (status.equals(Response.Status.OK)) { user.status = false; + user.updated_at = LocalDateTime.now(); + user.deleted_at = LocalDateTime.now(); user.persist(); if (!user.isPersistent()) { status = Response.Status.NOT_MODIFIED; @@ -233,6 +236,8 @@ public class UsersRessources { status = Response.Status.NOT_FOUND; } else { singleUser.status = false; + singleUser.updated_at = LocalDateTime.now(); + singleUser.deleted_at = LocalDateTime.now(); singleUser.persist(); if (!singleUser.isPersistent()) { status = Response.Status.NOT_MODIFIED; @@ -268,6 +273,10 @@ public class UsersRessources { usersOrig.password = Hash .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); usersOrig.roles = users.roles; + if(users.status){ + usersOrig.deleted_at = null; + usersOrig.status = true; + } usersOrig.persist(); if (!usersOrig.isPersistent()) { status = Response.Status.NOT_MODIFIED;