Compare commits
No commits in common. "0dba06a39b21f6bb05cf49cc1a9c7434afc1e58e" and "6d60e93d109f7001364a510fe431e6d754e52eb5" have entirely different histories.
0dba06a39b
...
6d60e93d10
@ -55,7 +55,7 @@ public class MessageEntity extends PanacheEntityBase implements Serializable {
|
|||||||
public UsersEntity users;
|
public UsersEntity users;
|
||||||
|
|
||||||
|
|
||||||
public static List<MessageEntity> findByUsers(String uuid){
|
public static List<CommentEntity> findByUsers(String uuid){
|
||||||
return find("users_id", uuid).list();
|
return find("users_id", uuid).list();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -90,7 +90,7 @@ public class CommentRessources {
|
|||||||
|
|
||||||
responseHttp = Response.ok(listComments).build();
|
responseHttp = Response.ok(listComments).build();
|
||||||
if(uuid != null){
|
if(uuid != null){
|
||||||
CommentEntity commentSingle = CommentEntity.findById(UUID.fromString(uuid));
|
CommentEntity commentSingle= PublisherEntity.findById(UUID.fromString(uuid));
|
||||||
responseHttp = Response.ok(commentSingle).build();
|
responseHttp = Response.ok(commentSingle).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,289 +0,0 @@
|
|||||||
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.MessageEntity;
|
|
||||||
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("message")
|
|
||||||
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 getMessages(@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<MessageEntity> listMessages = MessageEntity.findAll().page(Page.of(page, nbPages)).list();
|
|
||||||
|
|
||||||
|
|
||||||
responseHttp = Response.ok(listMessages).build();
|
|
||||||
if(uuid != null){
|
|
||||||
MessageEntity messageEntity= MessageEntity.findById(UUID.fromString(uuid));
|
|
||||||
responseHttp = Response.ok(messageEntity).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(search != null){
|
|
||||||
List<MessageEntity> messagesList = MessageEntity.find("#Message.bySearch", Parameters.with("content", search)).page(Page.of(page, nbPages)).list();
|
|
||||||
|
|
||||||
responseHttp = Response.ok(messagesList).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(MessageEntity.count()).build();
|
|
||||||
}
|
|
||||||
return responseHttp;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GET
|
|
||||||
@RolesAllowed("Admin")
|
|
||||||
@Path("{id}")
|
|
||||||
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);
|
|
||||||
Response.Status status = getResponseCheck(ctx, userCookie, user);
|
|
||||||
Response responseHttp = Response.status(status).build();
|
|
||||||
if (status.equals(Response.Status.OK)) {
|
|
||||||
UUID uid = UUID.fromString(id);
|
|
||||||
MessageEntity message = MessageEntity.findById(uid);
|
|
||||||
responseHttp = Response.status(Response.Status.NOT_FOUND).build();
|
|
||||||
if (message != null) {
|
|
||||||
responseHttp = Response.ok(message).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