From 6d60e93d109f7001364a510fe431e6d754e52eb5 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 28 Jan 2023 19:05:15 +0100 Subject: [PATCH 1/8] entity message --- .../java/com/covas/Entity/MessageEntity.java | 61 +++++++++++++++++++ .../java/com/covas/Entity/UsersEntity.java | 3 + 2 files changed, 64 insertions(+) create mode 100644 src/main/java/com/covas/Entity/MessageEntity.java diff --git a/src/main/java/com/covas/Entity/MessageEntity.java b/src/main/java/com/covas/Entity/MessageEntity.java new file mode 100644 index 0000000..bc0549e --- /dev/null +++ b/src/main/java/com/covas/Entity/MessageEntity.java @@ -0,0 +1,61 @@ +package com.covas.Entity; + +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.CascadeType; + +import org.hibernate.annotations.ColumnDefault; +import org.hibernate.annotations.GenericGenerator; + + +import io.quarkus.hibernate.orm.panache.PanacheEntityBase; + + +@Entity +@Table(name = "message") +@NamedQueries({ +@NamedQuery(name = "Message.bySearch", query = "from MessageEntity u where u.content like :content"), +}) +public class MessageEntity extends PanacheEntityBase implements Serializable { + @Id + @Column(name = "id") + @GeneratedValue(generator = "UUID") + @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator") + public UUID id; + + @Column(columnDefinition="TEXT") + public String content; + + @Column(nullable = false) + public LocalDateTime created_at; + @Column(nullable = false) + public LocalDateTime updated_at; + @ColumnDefault("null") + public LocalDateTime deleted_at; + + public String conversation; + + @ColumnDefault("1") + public Short status; + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "users_id", referencedColumnName = "id") + public UsersEntity users; + + + public static List findByUsers(String uuid){ + return find("users_id", uuid).list(); + } +} \ No newline at end of file diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index bc86693..6bae1fd 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -80,6 +80,9 @@ public class UsersEntity extends PanacheEntityBase implements Serializable { @OneToMany(mappedBy = "users") public Collection comment; + @OneToMany(mappedBy = "users") + public Collection message; + public static UsersEntity findByPseudo(String pseudo){ return find("pseudo", pseudo).firstResult(); } From 90ba63afd5df8e4b0e1076868dbdab1259130a46 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 29 Jan 2023 21:07:01 +0100 Subject: [PATCH 2/8] add message resources --- .../java/com/covas/Entity/MessageEntity.java | 2 +- .../covas/Resources/MessageRessources.java | 288 ++++++++++++++++++ 2 files changed, 289 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/covas/Resources/MessageRessources.java diff --git a/src/main/java/com/covas/Entity/MessageEntity.java b/src/main/java/com/covas/Entity/MessageEntity.java index bc0549e..ae2e784 100644 --- a/src/main/java/com/covas/Entity/MessageEntity.java +++ b/src/main/java/com/covas/Entity/MessageEntity.java @@ -55,7 +55,7 @@ public class MessageEntity extends PanacheEntityBase implements Serializable { public UsersEntity users; - public static List findByUsers(String uuid){ + public static List findByUsers(String uuid){ return find("users_id", uuid).list(); } } \ No newline at end of file diff --git a/src/main/java/com/covas/Resources/MessageRessources.java b/src/main/java/com/covas/Resources/MessageRessources.java new file mode 100644 index 0000000..f48ecd3 --- /dev/null +++ b/src/main/java/com/covas/Resources/MessageRessources.java @@ -0,0 +1,288 @@ +package com.covas.Resources; + +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +import javax.annotation.security.RolesAllowed; +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.Consumes; +import javax.ws.rs.CookieParam; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PATCH; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.covas.Entity.CommentEntity; +import com.covas.Entity.PublisherEntity; +import com.covas.Entity.UsersEntity; + +import io.quarkus.panache.common.Page; +import io.quarkus.panache.common.Parameters; + +import org.eclipse.microprofile.jwt.Claims; +import org.eclipse.microprofile.jwt.JsonWebToken; +import org.jboss.logging.Logger; +import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; + +@Produces(MediaType.APPLICATION_JSON) +@Path("mewsage") +public class MessageRessources { + private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); + @Inject + JsonWebToken jwt; + + /// Function + private Boolean checkUserCookie(String userCookie, UsersEntity users) { + if ((userCookie == null) || (users == null)) { + return false; + } + String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); + if (!name.equals(users.pseudo) && (users.status != 1)) { + return false; + } + return true; + } + + private Response.Status getResponseCheck(SecurityContext ctx, String userCookie, UsersEntity users) { + if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) { + return Response.Status.INTERNAL_SERVER_ERROR; + } + if (!checkUserCookie(userCookie, users)) { + return Response.Status.FORBIDDEN; + } + return Response.Status.OK; + } + + /// Appel HTTP + + /// GET + @GET + @RolesAllowed("Admin") + public Response getComments(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, + @QueryParam("status") Short status, + @QueryParam("search") String search, + @QueryParam("uuid") String uuid) { + if(nbPages == null){ + nbPages = 20; + } + if(page == null){ + page = 0; + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status statusHttp = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(statusHttp).build(); + if (statusHttp.equals(Response.Status.OK)) { + List listComments = CommentEntity.findAll().page(Page.of(page, nbPages)).list(); + + + responseHttp = Response.ok(listComments).build(); + if(uuid != null){ + CommentEntity commentSingle= PublisherEntity.findById(UUID.fromString(uuid)); + responseHttp = Response.ok(commentSingle).build(); + } + + if(search != null){ + List commentsList = CommentEntity.find("#Comment.bySearch", Parameters.with("comment", search)).page(Page.of(page, nbPages)).list(); + + responseHttp = Response.ok(commentsList).build(); + + } + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("count") + public Response getCount(@CookieParam("user") String userCookie, @Context SecurityContext ctx){ + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)){ + responseHttp = Response.ok(CommentEntity.count()).build(); + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("{id}") + public Response getSingleComment(@PathParam("id") String id, @CookieParam("user") String userCookie, + @Context SecurityContext ctx) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)) { + UUID uid = UUID.fromString(id); + CommentEntity comment = CommentEntity.findById(uid); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (comment != null) { + responseHttp = Response.ok(comment).build(); + } + + } + return responseHttp; + } + + + // PUT + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @RolesAllowed({"Admin", "User"}) + @Path("{id}") + @Transactional + public Response createComment(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, CommentEntity comment) { + + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + CommentEntity newComment = new CommentEntity(); + PublisherEntity publishers = PublisherEntity.findById(UUID.fromString(id)); + if (status.equals(Response.Status.OK)) { + newComment.comment = comment.comment; + newComment.users = user; + newComment.publishers = publishers; + newComment.created_at = LocalDateTime.now(); + newComment.updated_at = LocalDateTime.now(); + newComment.persist(); + if (newComment.isPersistent()) { + status = Response.Status.CREATED; + } else { + status = Response.Status.NO_CONTENT; + } + } + return Response.status(status).entity(newComment).build(); + } + + + // DELETE + @DELETE + @Path("{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSingleCommentToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + CommentEntity singleComment = CommentEntity.find("id", UUID.fromString(id)).firstResult(); + if (singleComment == null) { + status = Response.Status.NOT_FOUND; + } else { + singleComment.status = -1; + singleComment.updated_at = LocalDateTime.now(); + singleComment.deleted_at = LocalDateTime.now(); + singleComment.persist(); + if (!singleComment.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + @DELETE + @Path("/disable/{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSingleCommentToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + CommentEntity singleComment = CommentEntity.find("id", UUID.fromString(id)).firstResult(); + if (singleComment == null) { + status = Response.Status.NOT_FOUND; + } else { + singleComment.status = 0; + singleComment.updated_at = LocalDateTime.now(); + singleComment.deleted_at = LocalDateTime.now(); + singleComment.persist(); + if (!singleComment.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + // PATCH + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("{id}") + public Response updateCommentAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + CommentEntity comment, @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + + if (status.equals(Response.Status.OK)) { + + CommentEntity commentOrig = CommentEntity.findById(UUID.fromString(id)); + if (commentOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + commentOrig.comment = comment.comment; + + + commentOrig.updated_at = LocalDateTime.now(); + + if(comment.status == 1){ + commentOrig.deleted_at = null; + commentOrig.status = 1; + } + commentOrig.persist(); + if (!commentOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + + + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("enable/{id}") + public Response enableCommentAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + + CommentEntity commentOrig = CommentEntity.findById(UUID.fromString(id)); + if (commentOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + commentOrig.status = 1; + commentOrig.persist(); + if (!commentOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } +} From 0dba06a39b21f6bb05cf49cc1a9c7434afc1e58e Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 29 Jan 2023 21:20:27 +0100 Subject: [PATCH 3/8] message resources wip --- .../covas/Resources/CommentRessources.java | 2 +- .../covas/Resources/MessageRessources.java | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/covas/Resources/CommentRessources.java b/src/main/java/com/covas/Resources/CommentRessources.java index e9f43f5..de778ba 100644 --- a/src/main/java/com/covas/Resources/CommentRessources.java +++ b/src/main/java/com/covas/Resources/CommentRessources.java @@ -90,7 +90,7 @@ public class CommentRessources { responseHttp = Response.ok(listComments).build(); if(uuid != null){ - CommentEntity commentSingle= PublisherEntity.findById(UUID.fromString(uuid)); + CommentEntity commentSingle = CommentEntity.findById(UUID.fromString(uuid)); responseHttp = Response.ok(commentSingle).build(); } diff --git a/src/main/java/com/covas/Resources/MessageRessources.java b/src/main/java/com/covas/Resources/MessageRessources.java index f48ecd3..3e48686 100644 --- a/src/main/java/com/covas/Resources/MessageRessources.java +++ b/src/main/java/com/covas/Resources/MessageRessources.java @@ -24,6 +24,7 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Entity.CommentEntity; +import com.covas.Entity.MessageEntity; import com.covas.Entity.PublisherEntity; import com.covas.Entity.UsersEntity; @@ -36,7 +37,7 @@ import org.jboss.logging.Logger; import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; @Produces(MediaType.APPLICATION_JSON) -@Path("mewsage") +@Path("message") public class MessageRessources { private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); @Inject @@ -69,7 +70,7 @@ public class MessageRessources { /// GET @GET @RolesAllowed("Admin") - public Response getComments(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + public Response getMessages(@CookieParam("user") String userCookie, @Context SecurityContext ctx, @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages, @QueryParam("status") Short status, @QueryParam("search") String search, @@ -85,19 +86,19 @@ public class MessageRessources { Response.Status statusHttp = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(statusHttp).build(); if (statusHttp.equals(Response.Status.OK)) { - List listComments = CommentEntity.findAll().page(Page.of(page, nbPages)).list(); + List listMessages = MessageEntity.findAll().page(Page.of(page, nbPages)).list(); - responseHttp = Response.ok(listComments).build(); + responseHttp = Response.ok(listMessages).build(); if(uuid != null){ - CommentEntity commentSingle= PublisherEntity.findById(UUID.fromString(uuid)); - responseHttp = Response.ok(commentSingle).build(); + MessageEntity messageEntity= MessageEntity.findById(UUID.fromString(uuid)); + responseHttp = Response.ok(messageEntity).build(); } if(search != null){ - List commentsList = CommentEntity.find("#Comment.bySearch", Parameters.with("comment", search)).page(Page.of(page, nbPages)).list(); + List messagesList = MessageEntity.find("#Message.bySearch", Parameters.with("content", search)).page(Page.of(page, nbPages)).list(); - responseHttp = Response.ok(commentsList).build(); + responseHttp = Response.ok(messagesList).build(); } } @@ -113,7 +114,7 @@ public class MessageRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)){ - responseHttp = Response.ok(CommentEntity.count()).build(); + responseHttp = Response.ok(MessageEntity.count()).build(); } return responseHttp; } @@ -121,7 +122,7 @@ public class MessageRessources { @GET @RolesAllowed("Admin") @Path("{id}") - public Response getSingleComment(@PathParam("id") String id, @CookieParam("user") String userCookie, + public Response getSingleMessage(@PathParam("id") String id, @CookieParam("user") String userCookie, @Context SecurityContext ctx) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); @@ -129,10 +130,10 @@ public class MessageRessources { Response responseHttp = Response.status(status).build(); if (status.equals(Response.Status.OK)) { UUID uid = UUID.fromString(id); - CommentEntity comment = CommentEntity.findById(uid); + MessageEntity message = MessageEntity.findById(uid); responseHttp = Response.status(Response.Status.NOT_FOUND).build(); - if (comment != null) { - responseHttp = Response.ok(comment).build(); + if (message != null) { + responseHttp = Response.ok(message).build(); } } From 905fc40851f8cb5ec0bc70ccc306d1cb876ac6e9 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 30 Jan 2023 22:27:41 +0100 Subject: [PATCH 4/8] message ressources --- .../covas/Resources/MessageRessources.java | 89 +++++++++---------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/covas/Resources/MessageRessources.java b/src/main/java/com/covas/Resources/MessageRessources.java index 3e48686..1623190 100644 --- a/src/main/java/com/covas/Resources/MessageRessources.java +++ b/src/main/java/com/covas/Resources/MessageRessources.java @@ -147,27 +147,26 @@ public class MessageRessources { @RolesAllowed({"Admin", "User"}) @Path("{id}") @Transactional - public Response createComment(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, CommentEntity comment) { + public Response createMessage(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, MessageEntity message) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); - CommentEntity newComment = new CommentEntity(); - PublisherEntity publishers = PublisherEntity.findById(UUID.fromString(id)); + MessageEntity newMessage = new MessageEntity(); if (status.equals(Response.Status.OK)) { - newComment.comment = comment.comment; - newComment.users = user; - newComment.publishers = publishers; - newComment.created_at = LocalDateTime.now(); - newComment.updated_at = LocalDateTime.now(); - newComment.persist(); - if (newComment.isPersistent()) { + newMessage.content = message.content; + newMessage.conversation = message.conversation; + newMessage.users = user; + newMessage.created_at = LocalDateTime.now(); + newMessage.updated_at = LocalDateTime.now(); + newMessage.persist(); + if (newMessage.isPersistent()) { status = Response.Status.CREATED; } else { status = Response.Status.NO_CONTENT; } } - return Response.status(status).entity(newComment).build(); + return Response.status(status).entity(newMessage).build(); } @@ -176,21 +175,21 @@ public class MessageRessources { @Path("{id}") @RolesAllowed({"Admin", "User"}) @Transactional - public Response changeStatusSingleCommentToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response changeStatusSingleMessageToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - CommentEntity singleComment = CommentEntity.find("id", UUID.fromString(id)).firstResult(); - if (singleComment == null) { + MessageEntity singleMessage= MessageEntity.find("id", UUID.fromString(id)).firstResult(); + if (singleMessage == null) { status = Response.Status.NOT_FOUND; } else { - singleComment.status = -1; - singleComment.updated_at = LocalDateTime.now(); - singleComment.deleted_at = LocalDateTime.now(); - singleComment.persist(); - if (!singleComment.isPersistent()) { + singleMessage.status = -1; + singleMessage.updated_at = LocalDateTime.now(); + singleMessage.deleted_at = LocalDateTime.now(); + singleMessage.persist(); + if (!singleMessage.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } @@ -200,23 +199,23 @@ public class MessageRessources { @DELETE @Path("/disable/{id}") - @RolesAllowed({"Admin", "User"}) + @RolesAllowed("Admin") @Transactional - public Response changeStatusSingleCommentToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + public Response changeStatusSingleMessageToDisable(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - CommentEntity singleComment = CommentEntity.find("id", UUID.fromString(id)).firstResult(); - if (singleComment == null) { + MessageEntity singleMessage = MessageEntity.find("id", UUID.fromString(id)).firstResult(); + if (singleMessage == null) { status = Response.Status.NOT_FOUND; } else { - singleComment.status = 0; - singleComment.updated_at = LocalDateTime.now(); - singleComment.deleted_at = LocalDateTime.now(); - singleComment.persist(); - if (!singleComment.isPersistent()) { + singleMessage.status = 0; + singleMessage.updated_at = LocalDateTime.now(); + singleMessage.deleted_at = LocalDateTime.now(); + singleMessage.persist(); + if (!singleMessage.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } @@ -230,29 +229,29 @@ public class MessageRessources { @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("{id}") - public Response updateCommentAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, - CommentEntity comment, @PathParam("id") String id) { + public Response updateMessageAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + MessageEntity message, @PathParam("id") String id) { UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); UsersEntity user = UsersEntity.findById(kid); Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - CommentEntity commentOrig = CommentEntity.findById(UUID.fromString(id)); - if (commentOrig == null) { + MessageEntity messageOrig = MessageEntity.findById(UUID.fromString(id)); + if (messageOrig == null) { status = Response.Status.NOT_FOUND; } else { - commentOrig.comment = comment.comment; + messageOrig.content = message.content; - commentOrig.updated_at = LocalDateTime.now(); + messageOrig.updated_at = LocalDateTime.now(); - if(comment.status == 1){ - commentOrig.deleted_at = null; - commentOrig.status = 1; + if(message.status == 1){ + messageOrig.deleted_at = null; + messageOrig.status = 1; } - commentOrig.persist(); - if (!commentOrig.isPersistent()) { + messageOrig.persist(); + if (!messageOrig.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } @@ -263,7 +262,7 @@ public class MessageRessources { @PATCH - @RolesAllowed({"Admin", "User"}) + @RolesAllowed("Admin") @Consumes(MediaType.APPLICATION_JSON) @Transactional @Path("enable/{id}") @@ -273,13 +272,13 @@ public class MessageRessources { Response.Status status = getResponseCheck(ctx, userCookie, user); if (status.equals(Response.Status.OK)) { - CommentEntity commentOrig = CommentEntity.findById(UUID.fromString(id)); - if (commentOrig == null) { + MessageEntity messageOrig = MessageEntity.findById(UUID.fromString(id)); + if (messageOrig == null) { status = Response.Status.NOT_FOUND; } else { - commentOrig.status = 1; - commentOrig.persist(); - if (!commentOrig.isPersistent()) { + messageOrig.status = 1; + messageOrig.persist(); + if (!messageOrig.isPersistent()) { status = Response.Status.NOT_MODIFIED; } } From a88079f5fac66390f9500dfc3d408e58b630388b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 30 Jan 2023 22:49:04 +0100 Subject: [PATCH 5/8] message by user --- .../java/com/covas/Json/MessageByUser.java | 22 ++++++++++++++++ .../covas/Resources/MessageRessources.java | 26 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/com/covas/Json/MessageByUser.java diff --git a/src/main/java/com/covas/Json/MessageByUser.java b/src/main/java/com/covas/Json/MessageByUser.java new file mode 100644 index 0000000..4c02497 --- /dev/null +++ b/src/main/java/com/covas/Json/MessageByUser.java @@ -0,0 +1,22 @@ +package com.covas.Json; + +import java.util.Collection; + +import com.covas.Entity.MessageEntity; + +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class MessageByUser { + + public final String pseudo; + public final Collection message; + + public MessageByUser(String pseudo, Collection message){ + this.pseudo = pseudo; + this.message = message; + + + } + +} diff --git a/src/main/java/com/covas/Resources/MessageRessources.java b/src/main/java/com/covas/Resources/MessageRessources.java index 1623190..eddb47c 100644 --- a/src/main/java/com/covas/Resources/MessageRessources.java +++ b/src/main/java/com/covas/Resources/MessageRessources.java @@ -27,6 +27,7 @@ import com.covas.Entity.CommentEntity; import com.covas.Entity.MessageEntity; import com.covas.Entity.PublisherEntity; import com.covas.Entity.UsersEntity; +import com.covas.Json.MessageByUser; import io.quarkus.panache.common.Page; import io.quarkus.panache.common.Parameters; @@ -140,6 +141,31 @@ public class MessageRessources { return responseHttp; } + @GET + @RolesAllowed("User") + @Path("info") + public Response getInfoMessage(@Context SecurityContext ctx, @CookieParam("user") String userCookie) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + if (user == null) { + status = Response.Status.NOT_FOUND; + } + if (!checkUserCookie(userCookie, user)) { + status = Response.Status.FORBIDDEN; + } + } + Response responseHttp = Response.status(status).build(); + + if (status.equals(Response.Status.OK)) { + responseHttp = Response.status(status).entity(new MessageByUser(user.pseudo, user.message)) + .build(); + } + return responseHttp; + } + + // PUT @PUT From 809055f4325fbae8b7fd2d93eac33730e0db6a5e Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 30 Jan 2023 23:01:06 +0100 Subject: [PATCH 6/8] new entity conversation --- .../com/covas/Entity/ConversationEntity.java | 19 +++++++++++++++++++ .../java/com/covas/Entity/UsersEntity.java | 2 -- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/covas/Entity/ConversationEntity.java diff --git a/src/main/java/com/covas/Entity/ConversationEntity.java b/src/main/java/com/covas/Entity/ConversationEntity.java new file mode 100644 index 0000000..7679625 --- /dev/null +++ b/src/main/java/com/covas/Entity/ConversationEntity.java @@ -0,0 +1,19 @@ +package com.covas.Entity; + +import java.time.LocalDateTime; +import java.util.Collection; + +import io.quarkus.mongodb.panache.PanacheMongoEntity; +import io.quarkus.mongodb.panache.common.MongoEntity; + +@MongoEntity(collection="conversation") +public class ConversationEntity extends PanacheMongoEntity { + + + public Collection users; + public Short status; + + public LocalDateTime created_at; + public LocalDateTime updated_at; + public LocalDateTime deleted_at; +} diff --git a/src/main/java/com/covas/Entity/UsersEntity.java b/src/main/java/com/covas/Entity/UsersEntity.java index 6bae1fd..93918bc 100644 --- a/src/main/java/com/covas/Entity/UsersEntity.java +++ b/src/main/java/com/covas/Entity/UsersEntity.java @@ -105,8 +105,6 @@ public class UsersEntity extends PanacheEntityBase implements Serializable { users.created_at = LocalDateTime.now(); users.updated_at = LocalDateTime.now(); users.description = ""; - users.persist(); - } } \ No newline at end of file From 04c018f5c0bade8af7ba341ad503f0b11f996a76 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 2 Feb 2023 22:53:49 +0100 Subject: [PATCH 7/8] add conversation resources --- .../Resources/ConversationRessources.java | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 src/main/java/com/covas/Resources/ConversationRessources.java diff --git a/src/main/java/com/covas/Resources/ConversationRessources.java b/src/main/java/com/covas/Resources/ConversationRessources.java new file mode 100644 index 0000000..f846b7a --- /dev/null +++ b/src/main/java/com/covas/Resources/ConversationRessources.java @@ -0,0 +1,217 @@ +package com.covas.Resources; + +import java.nio.charset.StandardCharsets; +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; +import java.util.ArrayList; +import java.util.Collection; + + +import javax.annotation.security.RolesAllowed; +import javax.enterprise.context.Conversation; +import javax.inject.Inject; +import javax.transaction.Transactional; +import javax.ws.rs.core.SecurityContext; +import javax.ws.rs.Consumes; +import javax.ws.rs.CookieParam; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.PATCH; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import com.covas.Entity.ConversationEntity; +import com.covas.Entity.FriendEntity; +import com.covas.Entity.GroupEntity; +import com.covas.Entity.UsersEntity; + +import io.quarkus.panache.common.Page; + +import org.bson.types.ObjectId; +import org.eclipse.microprofile.jwt.Claims; +import org.eclipse.microprofile.jwt.JsonWebToken; +import org.jboss.logging.Logger; +import org.postgresql.shaded.com.ongres.scram.common.bouncycastle.base64.Base64; + +@Produces(MediaType.APPLICATION_JSON) +@Path("conversation") +public class ConversationRessources { + private static final Logger LOGGER = Logger.getLogger(UsersRessources.class); + @Inject + JsonWebToken jwt; + + /// Function + private Boolean checkUserCookie(String userCookie, UsersEntity users) { + if ((userCookie == null) || (users == null)) { + return false; + } + String name = new String(Base64.decode(userCookie), StandardCharsets.UTF_8); + if (!name.equals(users.pseudo) && (users.status != 1)) { + return false; + } + return true; + } + + private Response.Status getResponseCheck(SecurityContext ctx, String userCookie, UsersEntity users) { + if (!ctx.getUserPrincipal().getName().equals(jwt.getName())) { + return Response.Status.INTERNAL_SERVER_ERROR; + } + if (!checkUserCookie(userCookie, users)) { + return Response.Status.FORBIDDEN; + } + return Response.Status.OK; + } + + /// Appel HTTP + + + /// GET + @GET + @RolesAllowed("Admin") + public Response getFriends(@CookieParam("user") String userCookie, @Context SecurityContext ctx, + @QueryParam("page") Integer page, @QueryParam("nbPages") Integer nbPages) { + if(nbPages == null){ + nbPages = 20; + } + if(page == null){ + page = 0; + } + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status statusHttp = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(statusHttp).build(); + if (statusHttp.equals(Response.Status.OK)) { + List listConversations = ConversationEntity.findAll().page(Page.of(page, nbPages)).list(); + responseHttp = Response.ok(listConversations).build(); + + } + return responseHttp; + } + + @GET + @RolesAllowed("Admin") + @Path("count") + public Response getCount(@CookieParam("user") String userCookie, @Context SecurityContext ctx){ + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)){ + responseHttp = Response.ok(ConversationEntity.count()).build(); + } + return responseHttp; + } + + @GET + @RolesAllowed({"Admin", "User"}) + @Path("{id}") + public Response getSingleFriend(@PathParam("id") String id, @CookieParam("user") String userCookie, + @Context SecurityContext ctx) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + Response responseHttp = Response.status(status).build(); + if (status.equals(Response.Status.OK)) { + ConversationEntity conversation = ConversationEntity.findById(new ObjectId(id)); + responseHttp = Response.status(Response.Status.NOT_FOUND).build(); + if (conversation != null) { + responseHttp = Response.ok(conversation).build(); + } + + } + return responseHttp; + } + + + + // PUT + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @RolesAllowed({"Admin", "User"}) + @Path("{id}") + @Transactional + public Response createFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, ConversationEntity conversation) { + + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + ConversationEntity newConversation = new ConversationEntity(); + if (status.equals(Response.Status.OK)) { + newConversation.users = conversation.users; + newConversation.created_at = LocalDateTime.now(); + newConversation.updated_at = LocalDateTime.now(); + newConversation.persist(); + status = Response.Status.CREATED; + + } + return Response.status(status).entity(newConversation).build(); + } + + + // DELETE + @DELETE + @Path("{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSingleFriendToDelete(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + if (status.equals(Response.Status.OK)) { + ConversationEntity singleConversation = ConversationEntity.find("id", new ObjectId(id)).firstResult(); + if (singleConversation == null) { + status = Response.Status.NOT_FOUND; + } else { + singleConversation.status = -1; + singleConversation.updated_at = LocalDateTime.now(); + singleConversation.deleted_at = LocalDateTime.now(); + singleConversation.persist(); + } + } + return Response.status(status).build(); + } + + + + // PATCH + @PATCH + @RolesAllowed({"Admin", "User"}) + @Consumes(MediaType.APPLICATION_JSON) + @Transactional + @Path("{id}") + public Response updateFriend(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + ConversationEntity conversation, @PathParam("id") String id) { + UUID kid = UUID.fromString(jwt.getClaim(Claims.kid)); + UsersEntity user = UsersEntity.findById(kid); + Response.Status status = getResponseCheck(ctx, userCookie, user); + + if (status.equals(Response.Status.OK)) { + + ConversationEntity conversationOrig = ConversationEntity.findById(new ObjectId(id)); + if (conversationOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + conversationOrig.users = conversation.users; + + + conversationOrig.updated_at = LocalDateTime.now(); + + if(conversation.status == 1){ + conversationOrig.deleted_at = null; + conversationOrig.status = 1; + } + conversationOrig.persist(); + + } + } + return Response.status(status).build(); + } +} \ No newline at end of file From 22c68e7b21564d9b7d09106d8505d5eed19c15ff Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 9 Feb 2023 22:41:07 +0100 Subject: [PATCH 8/8] remove import useless --- src/main/java/com/covas/Resources/BlacklistRessources.java | 5 +---- .../java/com/covas/Resources/ConversationRessources.java | 6 +----- src/main/java/com/covas/Resources/FriendRessources.java | 4 ---- src/main/java/com/covas/Resources/GroupRessources.java | 3 +-- src/main/java/com/covas/Resources/MessageRessources.java | 2 -- 5 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/covas/Resources/BlacklistRessources.java b/src/main/java/com/covas/Resources/BlacklistRessources.java index 12ff241..cf0bfac 100644 --- a/src/main/java/com/covas/Resources/BlacklistRessources.java +++ b/src/main/java/com/covas/Resources/BlacklistRessources.java @@ -4,8 +4,7 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.List; import java.util.UUID; -import java.util.ArrayList; -import java.util.Collection; + import javax.annotation.security.RolesAllowed; @@ -27,8 +26,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Entity.BlacklistEntity; -import com.covas.Entity.FriendEntity; -import com.covas.Entity.GroupEntity; import com.covas.Entity.UsersEntity; import io.quarkus.panache.common.Page; diff --git a/src/main/java/com/covas/Resources/ConversationRessources.java b/src/main/java/com/covas/Resources/ConversationRessources.java index f846b7a..d07d359 100644 --- a/src/main/java/com/covas/Resources/ConversationRessources.java +++ b/src/main/java/com/covas/Resources/ConversationRessources.java @@ -4,12 +4,10 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.List; import java.util.UUID; -import java.util.ArrayList; -import java.util.Collection; + import javax.annotation.security.RolesAllowed; -import javax.enterprise.context.Conversation; import javax.inject.Inject; import javax.transaction.Transactional; import javax.ws.rs.core.SecurityContext; @@ -28,8 +26,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Entity.ConversationEntity; -import com.covas.Entity.FriendEntity; -import com.covas.Entity.GroupEntity; import com.covas.Entity.UsersEntity; import io.quarkus.panache.common.Page; diff --git a/src/main/java/com/covas/Resources/FriendRessources.java b/src/main/java/com/covas/Resources/FriendRessources.java index a511cfd..df49247 100644 --- a/src/main/java/com/covas/Resources/FriendRessources.java +++ b/src/main/java/com/covas/Resources/FriendRessources.java @@ -4,9 +4,6 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.List; import java.util.UUID; -import java.util.ArrayList; -import java.util.Collection; - import javax.annotation.security.RolesAllowed; import javax.inject.Inject; @@ -27,7 +24,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Entity.FriendEntity; -import com.covas.Entity.GroupEntity; import com.covas.Entity.UsersEntity; import io.quarkus.panache.common.Page; diff --git a/src/main/java/com/covas/Resources/GroupRessources.java b/src/main/java/com/covas/Resources/GroupRessources.java index 938a172..047a862 100644 --- a/src/main/java/com/covas/Resources/GroupRessources.java +++ b/src/main/java/com/covas/Resources/GroupRessources.java @@ -4,8 +4,7 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.List; import java.util.UUID; -import java.util.ArrayList; -import java.util.Collection; + import javax.annotation.security.RolesAllowed; diff --git a/src/main/java/com/covas/Resources/MessageRessources.java b/src/main/java/com/covas/Resources/MessageRessources.java index eddb47c..fdb91f9 100644 --- a/src/main/java/com/covas/Resources/MessageRessources.java +++ b/src/main/java/com/covas/Resources/MessageRessources.java @@ -23,9 +23,7 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import com.covas.Entity.CommentEntity; import com.covas.Entity.MessageEntity; -import com.covas.Entity.PublisherEntity; import com.covas.Entity.UsersEntity; import com.covas.Json.MessageByUser;