Merge pull request 'mailResources' (#5) from mailResources into master

Reviewed-on: #5
This commit is contained in:
v4l3n71n 2022-06-29 20:48:54 +00:00
commit 4654eab06e
5 changed files with 109 additions and 2 deletions

16
pom.xml
View File

@ -64,6 +64,22 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mailer</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-qute</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-redis-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>

View File

@ -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");
}

View File

@ -0,0 +1,75 @@
package com.covas.Resources;
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 io.quarkus.mailer.MailTemplate;
import io.quarkus.redis.client.RedisClient;
@Path("mail")
public class MailRessource {
@Inject
private MailTemplate mailer;
@Inject
RedisClient redisClient;
@GET
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();
redisClient.set(Arrays.asList(users.id.toString(),randomKey));
redisClient.expire(users.id.toString(), "300");
mailer.to(users.email).subject(String.format("Compte %s créée", users.email)).data("name", users.pseudo).data("id",users.id).data("key",randomKey).send().subscribeAsCompletionStage();
return Response.ok().build();
}
@GET
@Path("{id}")
@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();
}
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(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();
}
}

View File

@ -12,3 +12,16 @@ quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/toto
quarkus.hibernate-orm.database.generation = drop-and-create
covas.schema.create = true
quarkus.mailer.auth-methods=DIGEST-MD5 CRAM-SHA256 CRAM-SHA1 CRAM-MD5 PLAIN LOGIN
quarkus.mailer.from=valczebackup@gmail.com
quarkus.mailer.host=smtp.gmail.com
quarkus.mailer.port=587
quarkus.mailer.start-tls=REQUIRED
quarkus.mailer.username=valczebackup@gmail.com
quarkus.mailer.password=aohrpmqvxldwyebs
quarkus.mailer.mock=false
quarkus.redis.hosts=redis://localhost:6379

View File

@ -0,0 +1,3 @@
Coucou {name}
Voici un lien https://localhost:8080/api/mail/{id}?key={key}