DB init au démarrage de l'application

This commit is contained in:
Valentin CZERYBA 2022-04-14 22:03:42 +02:00
parent 7b5772b49f
commit ca2ae584bc
2 changed files with 50 additions and 25 deletions

View File

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

View File

@ -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)