Merge pull request 'event' (#10) from event into master
Reviewed-on: #10
This commit is contained in:
commit
e3ab679f51
8
pom.xml
8
pom.xml
@ -80,6 +80,10 @@
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-redis-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-mongodb-panache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5</artifactId>
|
||||
@ -132,7 +136,6 @@
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>native</id>
|
||||
|
||||
<activation>
|
||||
<property>
|
||||
<name>native</name>
|
||||
@ -150,7 +153,6 @@
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
|
||||
<systemPropertyVariables>
|
||||
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
|
||||
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
|
||||
@ -163,7 +165,7 @@
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<quarkus.package.type>native</quarkus.package.type>
|
||||
<quarkus.package.type>native</quarkus.package.type>
|
||||
</properties>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
29
src/main/java/com/covas/Entity/EventEntity.java
Normal file
29
src/main/java/com/covas/Entity/EventEntity.java
Normal file
@ -0,0 +1,29 @@
|
||||
package com.covas.Entity;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
|
||||
import com.covas.Json.Organisateurs;
|
||||
|
||||
import io.quarkus.mongodb.panache.PanacheMongoEntity;
|
||||
import io.quarkus.mongodb.panache.common.MongoEntity;
|
||||
|
||||
@MongoEntity(collection="event")
|
||||
public class EventEntity extends PanacheMongoEntity {
|
||||
|
||||
public String name;
|
||||
|
||||
public Short status;
|
||||
|
||||
public String address;
|
||||
|
||||
public Organisateurs organisateurs;
|
||||
|
||||
public Collection<String> participants;
|
||||
|
||||
public Collection<String> interesses;
|
||||
|
||||
public LocalDateTime created_at;
|
||||
public LocalDateTime updated_at;
|
||||
public LocalDateTime deleted_at;
|
||||
}
|
21
src/main/java/com/covas/Json/Organisateurs.java
Normal file
21
src/main/java/com/covas/Json/Organisateurs.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.covas.Json;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
public class Organisateurs {
|
||||
|
||||
public Collection<String> users_id;
|
||||
public Collection<String> groups_id;
|
||||
|
||||
public Organisateurs(){
|
||||
|
||||
}
|
||||
|
||||
public Organisateurs(Collection<String> users_id, Collection<String> groups_id){
|
||||
this.users_id = users_id;
|
||||
this.groups_id = groups_id;
|
||||
}
|
||||
}
|
@ -26,7 +26,6 @@ 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;
|
||||
|
276
src/main/java/com/covas/Resources/EventRessources.java
Normal file
276
src/main/java/com/covas/Resources/EventRessources.java
Normal file
@ -0,0 +1,276 @@
|
||||
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.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.EventEntity;
|
||||
import com.covas.Entity.PublisherEntity;
|
||||
import com.covas.Entity.UsersEntity;
|
||||
import com.covas.Json.Organisateurs;
|
||||
|
||||
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("event")
|
||||
public class EventRessources {
|
||||
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 getEvents(@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<EventEntity> listEvents = EventEntity.findAll().page(Page.of(page, nbPages)).list();
|
||||
|
||||
|
||||
responseHttp = Response.ok(listEvents).build();
|
||||
if(uuid != null){
|
||||
EventEntity eventSingle= EventEntity.findById(new ObjectId(uuid));
|
||||
responseHttp = Response.ok(eventSingle).build();
|
||||
}
|
||||
|
||||
if(search != null){
|
||||
List<EventEntity> eventsList = EventEntity.find("name", search).page(Page.of(page, nbPages)).list();
|
||||
responseHttp = Response.ok(eventsList).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(EventEntity.count()).build();
|
||||
}
|
||||
return responseHttp;
|
||||
}
|
||||
|
||||
@GET
|
||||
@RolesAllowed("Admin")
|
||||
@Path("{id}")
|
||||
public Response getSingleEvent(@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)) {
|
||||
EventEntity event = EventEntity.findById(new ObjectId(id));
|
||||
responseHttp = Response.status(Response.Status.NOT_FOUND).build();
|
||||
if (event != null) {
|
||||
responseHttp = Response.ok(event).build();
|
||||
}
|
||||
|
||||
}
|
||||
return responseHttp;
|
||||
}
|
||||
|
||||
|
||||
// PUT
|
||||
@PUT
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@RolesAllowed({"Admin", "User"})
|
||||
@Path("{id}")
|
||||
@Transactional
|
||||
public Response createEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie, @PathParam("id") String id, EventEntity event) {
|
||||
|
||||
UUID kid = UUID.fromString(jwt.getClaim(Claims.kid));
|
||||
UsersEntity user = UsersEntity.findById(kid);
|
||||
Response.Status status = getResponseCheck(ctx, userCookie, user);
|
||||
EventEntity newEvent = new EventEntity();
|
||||
if (status.equals(Response.Status.OK)) {
|
||||
newEvent.name = event.name;
|
||||
newEvent.address = event.address;
|
||||
newEvent.organisateurs = event.organisateurs;
|
||||
newEvent.created_at = LocalDateTime.now();
|
||||
newEvent.updated_at = LocalDateTime.now();
|
||||
newEvent.persist();
|
||||
status = Response.Status.CREATED;
|
||||
|
||||
}
|
||||
return Response.status(status).entity(newEvent).build();
|
||||
}
|
||||
|
||||
|
||||
// DELETE
|
||||
@DELETE
|
||||
@Path("{id}")
|
||||
@RolesAllowed({"Admin", "User"})
|
||||
@Transactional
|
||||
public Response changeStatusSingleEventToDelete(@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)) {
|
||||
EventEntity singleEvent = EventEntity.find("id", new ObjectId(id)).firstResult();
|
||||
if (singleEvent == null) {
|
||||
status = Response.Status.NOT_FOUND;
|
||||
} else {
|
||||
singleEvent.status = -1;
|
||||
singleEvent.updated_at = LocalDateTime.now();
|
||||
singleEvent.deleted_at = LocalDateTime.now();
|
||||
singleEvent.persist();
|
||||
}
|
||||
}
|
||||
return Response.status(status).build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("/disable/{id}")
|
||||
@RolesAllowed({"Admin", "User"})
|
||||
@Transactional
|
||||
public Response changeStatusSingleEventToDisable(@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)) {
|
||||
EventEntity singleEvent = EventEntity.find("id", new ObjectId(id)).firstResult();
|
||||
if (singleEvent == null) {
|
||||
status = Response.Status.NOT_FOUND;
|
||||
} else {
|
||||
singleEvent.status = 0;
|
||||
singleEvent.updated_at = LocalDateTime.now();
|
||||
singleEvent.deleted_at = LocalDateTime.now();
|
||||
singleEvent.persist();
|
||||
}
|
||||
}
|
||||
return Response.status(status).build();
|
||||
}
|
||||
|
||||
// PATCH
|
||||
@PATCH
|
||||
@RolesAllowed({"Admin", "User"})
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Transactional
|
||||
@Path("{id}")
|
||||
public Response updateEvent(@Context SecurityContext ctx, @CookieParam("user") String userCookie,
|
||||
EventEntity event, @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)) {
|
||||
|
||||
EventEntity eventOrig = EventEntity.findById(new ObjectId(id));
|
||||
if (eventOrig == null) {
|
||||
status = Response.Status.NOT_FOUND;
|
||||
} else {
|
||||
eventOrig.name = event.name;
|
||||
|
||||
|
||||
eventOrig.updated_at = LocalDateTime.now();
|
||||
|
||||
if(event.status == 1){
|
||||
eventOrig.deleted_at = null;
|
||||
eventOrig.status = 1;
|
||||
}
|
||||
eventOrig.persist();
|
||||
|
||||
}
|
||||
}
|
||||
return Response.status(status).build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PATCH
|
||||
@RolesAllowed({"Admin", "User"})
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@Transactional
|
||||
@Path("enable/{id}")
|
||||
public Response enableEvent(@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)) {
|
||||
|
||||
EventEntity eventOrig = EventEntity.findById(new ObjectId(id));
|
||||
if (eventOrig == null) {
|
||||
status = Response.Status.NOT_FOUND;
|
||||
} else {
|
||||
eventOrig.status = 1;
|
||||
eventOrig.persist();
|
||||
}
|
||||
}
|
||||
return Response.status(status).build();
|
||||
}
|
||||
}
|
@ -33,3 +33,6 @@ quarkus.http.cors.access-control-allow-credentials=true
|
||||
|
||||
#quarkus.native.additional-build-args=-H:IncludeResources=.*\\.pem,.\\*.txt
|
||||
quarkus.native.additional-build-args=-H:ResourceConfigurationFiles=${RESOURCES:resource-config.json}
|
||||
|
||||
quarkus.mongodb.connection-string=mongodb://${MONGO_USER:admin}:${MONGO_PASSWORD:mongo}@${MONGO_HOST:mongo}:${MONGO_PORT:27017}
|
||||
quarkus.mongodb.database = ${MONGO_DATABASE:admin}:
|
Loading…
x
Reference in New Issue
Block a user