From 9222efa007b8790852dc761d09367863bf56ddf3 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Fri, 24 Jan 2020 19:11:24 +0100 Subject: Start implementing /wiki/{pageName}. Change-Id: Ia9adf24209be8eddcfec72a66434ea4100855533 --- .../eu/mulk/mulkcms2/benki/wiki/WikiResource.java | 63 ++++++++++++++++++++-- .../eu/mulk/mulkcms2/cms/web/ExampleResource.java | 30 ----------- src/main/resources/application.properties | 3 +- .../resources/templates/benki/wiki/wikiPage.html | 10 ++++ 4 files changed, 72 insertions(+), 34 deletions(-) delete mode 100644 src/main/java/eu/mulk/mulkcms2/cms/web/ExampleResource.java (limited to 'src') diff --git a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java index 0bf69d6..8f25254 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java @@ -2,25 +2,82 @@ package eu.mulk.mulkcms2.benki.wiki; import static javax.ws.rs.core.MediaType.TEXT_HTML; +import io.quarkus.panache.common.Sort; import io.quarkus.qute.Template; import io.quarkus.qute.TemplateInstance; import io.quarkus.qute.api.ResourcePath; +import io.quarkus.security.Authenticated; +import io.quarkus.security.identity.SecurityIdentity; +import io.smallrye.jwt.auth.principal.JWTCallerPrincipal; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Map; +import java.util.Optional; import javax.inject.Inject; import javax.ws.rs.GET; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import org.jboss.logging.Logger; +import org.jboss.resteasy.spi.NotImplementedYetException; @Path("/wiki") public class WikiResource { + private static Logger log = Logger.getLogger(WikiResource.class); + + private static DateTimeFormatter htmlDateFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; + + private static DateTimeFormatter humanDateFormatter = + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.SHORT); + @ResourcePath("benki/wiki/wikiPage.html") - @Inject Template wikiPage; + @Inject + Template wikiPage; + + @Inject SecurityIdentity identity; @GET - @Path("/pages/{pageName}") + @Path("/{pageName}") @Produces(TEXT_HTML) public TemplateInstance getPage(@PathParam("pageName") String pageName) { - return wikiPage.data("title", "TEST"); + Optional maybePage = + WikiPageRevision.find("title = ?1", Sort.by("date").descending(), pageName) + .firstResultOptional(); + if (maybePage.isEmpty()) { + throw new NotFoundException(); + } + var page = maybePage.get(); + return wikiPage + .data("title", page.title) + .data( + "date", + Map.of( + "htmlFormat", htmlDateFormatter.format(page.date), + "humanFormat", humanDateFormatter.format(page.date))) + .data( + "author", + Map.of("name", String.format("%s %s", page.author.firstName, page.author.lastName))) + .data("content", page.content); + } + + @POST + @Path("/{pageName}") + @Authenticated + public void updatePage(@PathParam("pageName") String pageName) { + if (!identity.isAnonymous()) { + var jwtCallerPrincipal = (JWTCallerPrincipal) identity.getPrincipal(); + log.infof("Logged in as user: %s", jwtCallerPrincipal.getName()); + } + throw new NotImplementedYetException(); + } + + @GET + @Path("/{pageName}/revisions") + @Produces(TEXT_HTML) + public TemplateInstance getPageRevisions(@PathParam("pageName") String pageName) { + throw new NotImplementedYetException(); } } diff --git a/src/main/java/eu/mulk/mulkcms2/cms/web/ExampleResource.java b/src/main/java/eu/mulk/mulkcms2/cms/web/ExampleResource.java deleted file mode 100644 index cac9ba8..0000000 --- a/src/main/java/eu/mulk/mulkcms2/cms/web/ExampleResource.java +++ /dev/null @@ -1,30 +0,0 @@ -package eu.mulk.mulkcms2.cms.web; - -import io.quarkus.security.Authenticated; -import io.quarkus.security.identity.SecurityIdentity; -import io.smallrye.jwt.auth.principal.JWTCallerPrincipal; -import javax.inject.Inject; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import org.jboss.logging.Logger; - -@Path("/example") -public class ExampleResource { - - private static Logger log = Logger.getLogger(ExampleResource.class); - - @Inject SecurityIdentity identity; - - @GET - @Produces({MediaType.TEXT_PLAIN}) - @Authenticated - public String hello() { - if (!identity.isAnonymous()) { - var jwtCallerPrincipal = (JWTCallerPrincipal) identity.getPrincipal(); - log.infof("Logged in as user: %s", jwtCallerPrincipal.getName()); - } - return "hello!"; - } -} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4ba1cb1..bf6018f 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,9 +7,10 @@ quarkus.datasource.min-size = 0 #quarkus.flyway.baseline-version = 1 #quarkus.flyway.schemas = public -%dev.quarkus.datasource.url = jdbc:postgresql://localhost:5432/flep +%dev.quarkus.datasource.url = jdbc:postgresql://localhost:5432/mulkcms %dev.quarkus.datasource.username = mulk %dev.quarkus.datasource.password = +%dev.quarkus.hibernate-orm.log.sql = true quarkus.oidc.auth-server-url = https://login.benkard.de/auth/realms/master quarkus.oidc.client-id = mulkcms diff --git a/src/main/resources/templates/benki/wiki/wikiPage.html b/src/main/resources/templates/benki/wiki/wikiPage.html index 9b3af5d..caf587a 100644 --- a/src/main/resources/templates/benki/wiki/wikiPage.html +++ b/src/main/resources/templates/benki/wiki/wikiPage.html @@ -6,5 +6,15 @@

{title}

+ +
+ Last edit: {author.name} +
+ +
+
+ {content.raw} +
+
-- cgit v1.2.3