From b48f2bc73a18175d7e6748d3ab2febd87986d188 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Fri, 27 May 2022 23:14:05 +0200 Subject: [PATCH] add post function --- .../com/covas/Resources/UsersRessources.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/covas/Resources/UsersRessources.java b/src/main/java/com/covas/Resources/UsersRessources.java index a857ef2..525749d 100644 --- a/src/main/java/com/covas/Resources/UsersRessources.java +++ b/src/main/java/com/covas/Resources/UsersRessources.java @@ -14,6 +14,7 @@ import javax.ws.rs.CookieParam; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.PATCH; +import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -120,12 +121,47 @@ public class UsersRessources { return responseHttp; } + // POST + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + public Response createUser(UsersEntity users) { + Response.Status status = Response.Status.OK; + UsersEntity usersOrig = UsersEntity.findByPseudo(users.pseudo); + if (usersOrig != null) { + status = Response.Status.UNAUTHORIZED; + } else { + UsersEntity usersNew = new UsersEntity(); + usersNew.name = users.name; + usersNew.pseudo = users.pseudo; + usersNew.firstName = users.firstName; + usersNew.email = users.email; + usersNew.birth = LocalDate.of(users.birth.getYear(), users.birth.getMonth(), + users.birth.getDayOfMonth()); + usersNew.created_at = LocalDateTime.now(); + usersNew.updated_at = LocalDateTime.now(); + usersNew.password = Hash + .encryptSHA512(Base64.toBase64String(users.password.getBytes(StandardCharsets.UTF_8))); + usersNew.roles = users.roles; + usersNew.status = false; + usersNew.active_mail = false; + usersNew.persist(); + if (usersNew.isPersistent()) { + status = Response.Status.CREATED; + } else { + status = Response.Status.NO_CONTENT; + } + } + return Response.status(status).build(); + } + + // PUT @PUT @RolesAllowed("Admin") @Consumes(MediaType.APPLICATION_JSON) @Transactional - public Response createUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response addUser(@Context SecurityContext ctx, @CookieParam("user") String userCookie, UsersEntity users) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid);