add post function

This commit is contained in:
Valentin CZERYBA 2022-05-27 23:14:05 +02:00
parent e3dbd0b5d1
commit b48f2bc73a

View File

@ -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);