From 9233c16656775b90c1b1d5db33d518bbe978effc Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Mon, 9 Jan 2023 21:54:37 +0100 Subject: [PATCH] Add comment resources --- .../java/com/covas/Entity/CommentEntity.java | 3 - .../covas/Resources/CommentRessources.java | 290 ++++++++++++++++++ .../covas/Resources/PublisherRessources.java | 4 - 3 files changed, 290 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/covas/Resources/CommentRessources.java diff --git a/src/main/java/com/covas/Entity/CommentEntity.java b/src/main/java/com/covas/Entity/CommentEntity.java index 0fe5a8e..c93dcab 100644 --- a/src/main/java/com/covas/Entity/CommentEntity.java +++ b/src/main/java/com/covas/Entity/CommentEntity.java @@ -7,8 +7,6 @@ import java.util.UUID; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @@ -21,7 +19,6 @@ import javax.persistence.CascadeType; import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.GenericGenerator; -import com.covas.Enum.Type; import io.quarkus.hibernate.orm.panache.PanacheEntityBase; diff --git a/src/main/java/com/covas/Resources/CommentRessources.java b/src/main/java/com/covas/Resources/CommentRessources.java new file mode 100644 index 0000000..76f729f --- /dev/null +++ b/src/main/java/com/covas/Resources/CommentRessources.java @@ -0,0 +1,290 @@ +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 com.covas.Json.PublisherByUser; + +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("comment") +public class CommentRessources { + 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 getPublishers(@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 getSinglePublisher(@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 createPublisher(@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 changeStatusSinglePublisherToDelete(@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)) { + PublisherEntity singlePublisher = PublisherEntity.find("id", UUID.fromString(id)).firstResult(); + if (singlePublisher == null) { + status = Response.Status.NOT_FOUND; + } else { + singlePublisher.status = -1; + singlePublisher.updated_at = LocalDateTime.now(); + singlePublisher.deleted_at = LocalDateTime.now(); + singlePublisher.persist(); + if (!singlePublisher.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } + + @DELETE + @Path("/disable/{id}") + @RolesAllowed({"Admin", "User"}) + @Transactional + public Response changeStatusSinglePublisherToDisable(@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)) { + PublisherEntity singlePublisher = PublisherEntity.find("id", UUID.fromString(id)).firstResult(); + if (singlePublisher == null) { + status = Response.Status.NOT_FOUND; + } else { + singlePublisher.status = 0; + singlePublisher.updated_at = LocalDateTime.now(); + singlePublisher.deleted_at = LocalDateTime.now(); + singlePublisher.persist(); + if (!singlePublisher.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 updatePublisherAdmin(@Context SecurityContext ctx, @CookieParam("user") String userCookie, + PublisherEntity publishers, @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)) { + + PublisherEntity publishersOrig = PublisherEntity.findById(UUID.fromString(id)); + if (publishersOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + publishersOrig.description = publishers.description; + publishersOrig.url = publishers.url; + publishersOrig.type = publishers.type; + + publishersOrig.updated_at = LocalDateTime.now(); + + if(publishers.status == 1){ + publishersOrig.deleted_at = null; + publishersOrig.status = 1; + } + publishersOrig.persist(); + if (!publishersOrig.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 enableUserAdmin(@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)) { + + PublisherEntity publishersOrig = PublisherEntity.findById(UUID.fromString(id)); + if (publishersOrig == null) { + status = Response.Status.NOT_FOUND; + } else { + publishersOrig.status = 1; + publishersOrig.persist(); + if (!publishersOrig.isPersistent()) { + status = Response.Status.NOT_MODIFIED; + } + } + } + return Response.status(status).build(); + } +} diff --git a/src/main/java/com/covas/Resources/PublisherRessources.java b/src/main/java/com/covas/Resources/PublisherRessources.java index 4b694d2..aa32769 100644 --- a/src/main/java/com/covas/Resources/PublisherRessources.java +++ b/src/main/java/com/covas/Resources/PublisherRessources.java @@ -1,7 +1,6 @@ package com.covas.Resources; import java.nio.charset.StandardCharsets; -import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; import java.util.UUID; @@ -15,7 +14,6 @@ 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; @@ -25,11 +23,9 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import com.covas.Classes.Hash; import com.covas.Entity.PublisherEntity; import com.covas.Entity.UsersEntity; import com.covas.Json.PublisherByUser; -import com.covas.Json.UserExist; import io.quarkus.panache.common.Page; import io.quarkus.panache.common.Parameters;