Merge pull request 'comment' (#9) from comment into master

Reviewed-on: #9
This commit is contained in:
v4l3n71n 2023-01-09 21:47:54 +00:00
commit 69d5d3d5ee
7 changed files with 372 additions and 5 deletions

View File

@ -19,4 +19,14 @@ C'est la partie backend du projet COVAS généré par le générateur fourni par
## Statut du commentaire
-1 : suppression
0 : desactivé
1 : activé
2 : censuré

View File

@ -0,0 +1,62 @@
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 = "comment")
@NamedQueries({
@NamedQuery(name = "Comment.bySearch", query = "from CommentEntity u where u.comment like :comment"),
})
public class CommentEntity 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 comment;
@Column(nullable = false)
public LocalDateTime created_at;
@Column(nullable = false)
public LocalDateTime updated_at;
@ColumnDefault("null")
public LocalDateTime deleted_at;
@ColumnDefault("1")
public Short status;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "users_id", referencedColumnName = "id")
public UsersEntity users;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "publishers_id", referencedColumnName = "id")
public PublisherEntity publishers;
public static List<CommentEntity> findByUsers(String uuid){
return find("users_id", uuid).list();
}
}

View File

@ -2,6 +2,7 @@ package com.covas.Entity;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
@ -16,6 +17,7 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.CascadeType;
import org.hibernate.annotations.ColumnDefault;
@ -60,6 +62,10 @@ public class PublisherEntity extends PanacheEntityBase implements Serializable {
@JoinColumn(name = "users_id", referencedColumnName = "id")
public UsersEntity users;
@OneToMany(mappedBy = "publishers")
public Collection<CommentEntity> comment;
public static List<PublisherEntity> findByUsers(String uuid){
return find("users_id", uuid).list();
}

View File

@ -77,6 +77,9 @@ public class UsersEntity extends PanacheEntityBase implements Serializable {
@OneToMany(mappedBy = "users")
public Collection<PublisherEntity> publisher;
@OneToMany(mappedBy = "users")
public Collection<CommentEntity> comment;
public static UsersEntity findByPseudo(String pseudo){
return find("pseudo", pseudo).firstResult();
}

View File

@ -1,6 +1,6 @@
package com.covas.Enum;
public enum Type {
VIDEO, PHOTO, TEXT, URL
VIDEO, PHOTO, TEXT, URL, EVENT
}

View File

@ -0,0 +1,289 @@
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 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();
}
}

View File

@ -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;
@ -159,6 +155,7 @@ public class PublisherRessources {
}
}
Response responseHttp = Response.status(status).build();
if (status.equals(Response.Status.OK)) {
responseHttp = Response.status(status).entity(new PublisherByUser(user.pseudo, user.publisher))
.build();