From 5ac8443304d9db760ccfb0c22f91f98278cb961d Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 23 Jun 2022 23:44:00 +0200 Subject: [PATCH] finish redis mail send --- .../ApplicationLifeCycle.java | 2 +- src/main/java/com/covas/Json/RedisMail.java | 15 -------- .../com/covas/Resources/MailRessource.java | 38 +++++++++++++++---- 3 files changed, 31 insertions(+), 24 deletions(-) delete mode 100644 src/main/java/com/covas/Json/RedisMail.java diff --git a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java index ecd1721..d617bf8 100644 --- a/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java +++ b/src/main/java/com/covas/ApplicationScoped/ApplicationLifeCycle.java @@ -35,7 +35,7 @@ public class ApplicationLifeCycle { LOGGER.info("Robert80 user is created"); UsersEntity.add("robert80", "robert80@gmail.com", "titi", "robert", LocalDate.of(1990, Month.JANUARY, 23), "toto", "User"); LOGGER.info("Peter93 user is created"); - UsersEntity.add("peter93", "peter93gmail.com", "yollo", "peter", LocalDate.of(1993, Month.FEBRUARY, 26), "toto", "Admin"); + UsersEntity.add("peter93", "valcze80@gmail.com", "yollo", "peter", LocalDate.of(1993, Month.FEBRUARY, 26), "toto", "Admin"); } else { LOGGER.info("DB init wassn't created"); } diff --git a/src/main/java/com/covas/Json/RedisMail.java b/src/main/java/com/covas/Json/RedisMail.java deleted file mode 100644 index e5792b0..0000000 --- a/src/main/java/com/covas/Json/RedisMail.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.covas.Json; - -import io.quarkus.runtime.annotations.RegisterForReflection; - -@RegisterForReflection -public class RedisMail { - public String key = ""; - public RedisMail(){ - key = ""; - } - - public RedisMail(String key){ - this.key = key; - } -} diff --git a/src/main/java/com/covas/Resources/MailRessource.java b/src/main/java/com/covas/Resources/MailRessource.java index 5a8edf2..c006301 100644 --- a/src/main/java/com/covas/Resources/MailRessource.java +++ b/src/main/java/com/covas/Resources/MailRessource.java @@ -4,16 +4,19 @@ import java.util.Arrays; import java.util.UUID; import javax.inject.Inject; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import com.covas.Entity.UsersEntity; -import com.covas.Json.RedisMail; import io.quarkus.mailer.MailTemplate; import io.quarkus.redis.client.RedisClient; +import org.jboss.logging.Logger; @Path("mail") public class MailRessource { @@ -24,10 +27,10 @@ public class MailRessource { @Inject RedisClient redisClient; - @GET - public Response sendMail(UsersEntity users){ - if(UsersEntity.findByPseudo(users.pseudo) == null){ + public Response sendMail(UsersEntity usersEntity){ + UsersEntity users = UsersEntity.findByPseudo(usersEntity.pseudo); + if(users == null){ return Response.status(Response.Status.NOT_FOUND).build(); } String randomKey=UUID.randomUUID().toString(); @@ -38,15 +41,34 @@ public class MailRessource { @GET @Path("{id}") - public Response activateUsers(@PathParam("id") String id, RedisMail redisMail){ - if(UsersEntity.findById(UUID.fromString(id)) == null){ + @Consumes(MediaType.APPLICATION_JSON) + public Response activateUsers(@PathParam("id") String id, @QueryParam("key") String key){ + UsersEntity users = UsersEntity.findById(UUID.fromString(id)); + if(users == null){ return Response.status(Response.Status.NOT_FOUND).build(); } - String redisKey = redisClient.get(id).toString(); + + io.vertx.redis.client.Response singleKey = redisClient.get(id); + if(singleKey == null){ + return Response.status(Response.Status.FORBIDDEN).build(); + } + + String redisKey = singleKey.toString(); - if(!redisKey.equals(redisMail.key)){ + if(!redisKey.equals(key)){ return Response.status(Response.Status.NOT_ACCEPTABLE).build(); } + redisClient.del(Arrays.asList(id)); + if(users.active_mail){ + return Response.status(Response.Status.NOT_MODIFIED).build(); + } + + users.active_mail = true; + users.persist(); + + if(users.isPersistent()){ + return Response.status(Response.Status.PARTIAL_CONTENT).build(); + } return Response.ok().build(); }