add message resources
This commit is contained in:
parent
6d60e93d10
commit
90ba63afd5
@ -55,7 +55,7 @@ public class MessageEntity extends PanacheEntityBase implements Serializable {
|
|||||||
public UsersEntity users;
|
public UsersEntity users;
|
||||||
|
|
||||||
|
|
||||||
public static List<CommentEntity> findByUsers(String uuid){
|
public static List<MessageEntity> findByUsers(String uuid){
|
||||||
return find("users_id", uuid).list();
|
return find("users_id", uuid).list();
|
||||||
}
|
}
|
||||||
}
|
}
|
288
src/main/java/com/covas/Resources/MessageRessources.java
Normal file
288
src/main/java/com/covas/Resources/MessageRessources.java
Normal file
@ -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<CommentEntity> 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<CommentEntity> 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user