From ca2ae584bcfef52f9622f189f23d24969d136782 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Thu, 14 Apr 2022 22:03:42 +0200 Subject: [PATCH] =?UTF-8?q?DB=20init=20au=20d=C3=A9marrage=20de=20l'applic?= =?UTF-8?q?ation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/covas/ApplicationLifeCycle.java | 50 +++++++++++++++++++ src/main/java/com/covas/TokenRessource.java | 25 ---------- 2 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/covas/ApplicationLifeCycle.java diff --git a/src/main/java/com/covas/ApplicationLifeCycle.java b/src/main/java/com/covas/ApplicationLifeCycle.java new file mode 100644 index 0000000..1c6edea --- /dev/null +++ b/src/main/java/com/covas/ApplicationLifeCycle.java @@ -0,0 +1,50 @@ +package com.covas; + + +import org.jboss.logging.Logger; + +import io.quarkus.runtime.ShutdownEvent; +import io.quarkus.runtime.StartupEvent; + +import java.time.LocalDate; +import java.time.Month; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.transaction.Transactional; + +import org.eclipse.microprofile.config.inject.ConfigProperty; + + +@ApplicationScoped +public class ApplicationLifeCycle { + @Inject + @ConfigProperty(name = "covas.schema.create", defaultValue = "true") + boolean schemaCreate; + + private static final Logger LOGGER = Logger.getLogger(ApplicationLifeCycle.class); + + @Transactional + void onStart(@Observes StartupEvent ev) { + LOGGER.info("The application has started"); + if (schemaCreate){ + Users users = new Users(); + if(users.findByName("Peter") == null){ + users.name = "Peter"; + users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); + users.status = true; + users.persist(); + LOGGER.info("Users test was created"); + } else { + LOGGER.info("User test wasn't created"); + } + } else { + LOGGER.info("DB init wassn't created"); + } + } + void onStop(@Observes ShutdownEvent ev) { + LOGGER.info("The application is stopping..."); + } + +} diff --git a/src/main/java/com/covas/TokenRessource.java b/src/main/java/com/covas/TokenRessource.java index 8e7b05d..eee5144 100644 --- a/src/main/java/com/covas/TokenRessource.java +++ b/src/main/java/com/covas/TokenRessource.java @@ -27,43 +27,18 @@ import io.smallrye.jwt.auth.principal.JWTParser; import io.smallrye.jwt.auth.principal.ParseException; import io.smallrye.jwt.build.Jwt; -import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.jwt.JsonWebToken; @Path("/token") public class TokenRessource { - @Inject - @ConfigProperty(name = "covas.schema.create", defaultValue = "true") - boolean schemaCreate; - @Inject JsonWebToken jwt; @Inject JWTParser parser; - @PUT - @Path("init") - @Transactional - public Response init(){ - if (schemaCreate){ - Users users = new Users(); - if(users.findByName("Peter") == null){ - users.name = "Peter"; - users.birth = LocalDate.of(1993, Month.FEBRUARY, 23); - users.status = true; - users.persist(); - return Response.status(Response.Status.CREATED).build(); - } - } - return Response.status(Response.Status.NOT_MODIFIED).build(); - - - } - - @GET @Path("authentificate") @Produces(MediaType.APPLICATION_JSON)