add db-table configuration (WIP)
This commit is contained in:
19
src/main/java/de/mbremer/configuration/Configuration.java
Normal file
19
src/main/java/de/mbremer/configuration/Configuration.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package de.mbremer.configuration;
|
||||||
|
|
||||||
|
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Configuration extends PanacheEntity {
|
||||||
|
String propertyName;
|
||||||
|
String value;
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package de.mbremer.configuration;
|
||||||
|
|
||||||
|
import io.quarkus.hibernate.orm.panache.PanacheQuery;
|
||||||
|
import org.eclipse.microprofile.config.spi.ConfigSource;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class DatabaseConfigSource implements ConfigSource {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getPropertyNames() {
|
||||||
|
try {
|
||||||
|
return Configuration.streamAll().map(c -> ((Configuration) c).getPropertyName()).collect(Collectors.toSet());
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// DB in not up
|
||||||
|
return Set.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getValue(String propertyName) {
|
||||||
|
try {
|
||||||
|
PanacheQuery<Configuration> conf = Configuration.find("propertyName", propertyName);
|
||||||
|
if (conf == null) {
|
||||||
|
// DB in not up
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return conf.singleResultOptional().map(c -> ((Configuration) c).getValue()).orElse(null);
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
// DB in not up
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return DatabaseConfigSource.class.getSimpleName();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,23 +1,31 @@
|
|||||||
package de.mbremer.secutity;
|
package de.mbremer.secutity;
|
||||||
|
|
||||||
|
import de.mbremer.configuration.Configuration;
|
||||||
import de.mbremer.room.Room;
|
import de.mbremer.room.Room;
|
||||||
|
import de.mbremer.traffic.TrafficAdminForm;
|
||||||
|
import de.mbremer.traffic.TrafficUserForm;
|
||||||
|
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
|
||||||
import io.quarkus.panache.common.Sort;
|
import io.quarkus.panache.common.Sort;
|
||||||
import io.quarkus.qute.Location;
|
import io.quarkus.qute.Location;
|
||||||
import io.quarkus.qute.Template;
|
import io.quarkus.qute.Template;
|
||||||
import io.quarkus.qute.TemplateInstance;
|
import io.quarkus.qute.TemplateInstance;
|
||||||
import io.quarkus.security.identity.SecurityIdentity;
|
import io.quarkus.security.identity.SecurityIdentity;
|
||||||
|
import org.eclipse.microprofile.config.inject.ConfigProperty;
|
||||||
import org.jboss.logging.Logger;
|
import org.jboss.logging.Logger;
|
||||||
|
import org.jboss.resteasy.annotations.jaxrs.PathParam;
|
||||||
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
|
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
|
||||||
|
|
||||||
import javax.annotation.security.PermitAll;
|
import javax.annotation.security.PermitAll;
|
||||||
import javax.annotation.security.RolesAllowed;
|
import javax.annotation.security.RolesAllowed;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
import javax.persistence.NoResultException;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.NewCookie;
|
import javax.ws.rs.core.NewCookie;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import static de.mbremer.secutity.Role.ADMIN;
|
import static de.mbremer.secutity.Role.ADMIN;
|
||||||
|
|
||||||
@@ -36,6 +44,9 @@ public class UserResource {
|
|||||||
@Inject
|
@Inject
|
||||||
@Location("user.html")
|
@Location("user.html")
|
||||||
Template userTemplate;
|
Template userTemplate;
|
||||||
|
// TODO: 12.10.21
|
||||||
|
// @ConfigProperty(name = "google_api_key")
|
||||||
|
// Optional<String> googleApiKey;
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Produces(MediaType.TEXT_HTML)
|
@Produces(MediaType.TEXT_HTML)
|
||||||
@@ -44,11 +55,14 @@ public class UserResource {
|
|||||||
TemplateInstance templateInstance = userTemplate
|
TemplateInstance templateInstance = userTemplate
|
||||||
.data("current_user", User.find("username", identity.getPrincipal().getName()).singleResult());
|
.data("current_user", User.find("username", identity.getPrincipal().getName()).singleResult());
|
||||||
|
|
||||||
|
// log.info("Google-API-Key" + googleApiKey.orElse("-"));
|
||||||
|
|
||||||
if (identity.hasRole("ADMIN")) {
|
if (identity.hasRole("ADMIN")) {
|
||||||
templateInstance
|
templateInstance
|
||||||
.data("is_admin", true)
|
.data("is_admin", true)
|
||||||
.data("users", User.listAll(Sort.by("username")))
|
.data("users", User.listAll(Sort.by("username")))
|
||||||
.data("rooms", Room.listAll(Sort.by("name")));
|
.data("rooms", Room.listAll(Sort.by("name")));
|
||||||
|
// .data("google_api_key", googleApiKey.orElse(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
return templateInstance;
|
return templateInstance;
|
||||||
@@ -144,4 +158,35 @@ public class UserResource {
|
|||||||
|
|
||||||
return getUser();
|
return getUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||||
|
@Produces(MediaType.TEXT_HTML)
|
||||||
|
@Transactional
|
||||||
|
@Path("/traffic/config/")
|
||||||
|
public TemplateInstance configureTrafficAdmin(@MultipartForm TrafficAdminForm trafficAdminForm) {
|
||||||
|
log.info("configure traffic");
|
||||||
|
|
||||||
|
try {
|
||||||
|
Configuration apiKeyConfig = Configuration.find("propertyName", "google_api_key").singleResult();
|
||||||
|
apiKeyConfig.setValue(trafficAdminForm.googleApiKey);
|
||||||
|
log.info("update API-Key: " + trafficAdminForm.googleApiKey);
|
||||||
|
} catch (NoResultException e) {
|
||||||
|
new Configuration("google_api_key", trafficAdminForm.googleApiKey).persist();
|
||||||
|
log.info("create API-Key: " + trafficAdminForm.googleApiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
return getUser();
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
||||||
|
@Produces(MediaType.TEXT_HTML)
|
||||||
|
@Transactional
|
||||||
|
@Path("/traffic/config/{username}")
|
||||||
|
public TemplateInstance configureTrafficUser(@PathParam("username") String username, @MultipartForm TrafficUserForm trafficUserForm) {
|
||||||
|
log.info("configure traffic for " + username);
|
||||||
|
|
||||||
|
return getUser();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/main/java/de/mbremer/traffic/TrafficAdminForm.java
Normal file
11
src/main/java/de/mbremer/traffic/TrafficAdminForm.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package de.mbremer.traffic;
|
||||||
|
|
||||||
|
import javax.ws.rs.FormParam;
|
||||||
|
|
||||||
|
|
||||||
|
public class TrafficAdminForm {
|
||||||
|
public @FormParam("googleapikey")
|
||||||
|
String googleApiKey;
|
||||||
|
public @FormParam("officeadress")
|
||||||
|
String officeAdress;
|
||||||
|
}
|
||||||
4
src/main/java/de/mbremer/traffic/TrafficUserForm.java
Normal file
4
src/main/java/de/mbremer/traffic/TrafficUserForm.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package de.mbremer.traffic;
|
||||||
|
|
||||||
|
public class TrafficUserForm {
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
de.mbremer.configuration.DatabaseConfigSource
|
||||||
5
src/main/resources/db/migration/V0004__configuration.sql
Normal file
5
src/main/resources/db/migration/V0004__configuration.sql
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
create table configuration (
|
||||||
|
id bigint not null primary key,
|
||||||
|
propertyName varchar not null unique,
|
||||||
|
value varchar not null
|
||||||
|
);
|
||||||
@@ -40,16 +40,16 @@
|
|||||||
<div class="mt-4">
|
<div class="mt-4">
|
||||||
<h3>Verkehrsinfos</h3>
|
<h3>Verkehrsinfos</h3>
|
||||||
<div class="mx-auto">
|
<div class="mx-auto">
|
||||||
<form class="row g-3 mt-2">
|
<form class="row g-3 mt-2" action="/user/traffic/config" method="POST" name="trafficAdminForm" enctype="multipart/form-data">
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<div class="form-floating">
|
<div class="form-floating">
|
||||||
<input type="text" name="key" class="form-control" id="key" placeholder="google-API-Key" required>
|
<input type="text" name="googleapikey" class="form-control" id="key" placeholder="google-API-Key" required>
|
||||||
<label for="key">google-API-Key</label>
|
<label for="key">google-API-Key</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-7">
|
<div class="col-md-7">
|
||||||
<div class="form-floating">
|
<div class="form-floating">
|
||||||
<input type="text" name="office" class="form-control" id="office" placeholder="Büroadresse" required>
|
<input type="text" name="officeadress" class="form-control" id="office" placeholder="Büroadresse" required>
|
||||||
<label for="home">Büroadresse</label>
|
<label for="home">Büroadresse</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -73,7 +73,7 @@
|
|||||||
<h3>Verkehrsinfos</h3>
|
<h3>Verkehrsinfos</h3>
|
||||||
Die Fahrzeit wird über die googlemaps-API ermittelt und bei Überschreitung des Grenzwerts via E-Mail gewarnt.
|
Die Fahrzeit wird über die googlemaps-API ermittelt und bei Überschreitung des Grenzwerts via E-Mail gewarnt.
|
||||||
<div class="mx-auto">
|
<div class="mx-auto">
|
||||||
<form class="row g-3 mt-2">
|
<form class="row g-3 mt-2" action="/user/traffic/config/{current_user.username}" method="POST" name="trafficAdminForm" enctype="multipart/form-data">
|
||||||
<div class="col-md-4">
|
<div class="col-md-4">
|
||||||
<div class="form-floating">
|
<div class="form-floating">
|
||||||
<input type="email" name="email" class="form-control" id="email" placeholder="E-Mail-Adresse" required>
|
<input type="email" name="email" class="form-control" id="email" placeholder="E-Mail-Adresse" required>
|
||||||
|
|||||||
Reference in New Issue
Block a user