From 5d07527e63fe3ebeecc19c9a05eee6791c177d06 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 26 Jan 2020 18:05:10 +0100 Subject: Implement POST /wiki/{pageName}. Change-Id: Idf82cac81b13eb2ac4098b847f6415b01cdb0605 --- .../mulk/mulkcms2/benki/wiki/WikiPageRevision.java | 17 +++++++++ .../eu/mulk/mulkcms2/benki/wiki/WikiResource.java | 41 +++++++++++++++++----- 2 files changed, 50 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiPageRevision.java b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiPageRevision.java index 35a4986..c6bcf5f 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiPageRevision.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiPageRevision.java @@ -38,4 +38,21 @@ public class WikiPageRevision extends PanacheEntityBase { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "author", referencedColumnName = "id") public User author; + + public WikiPageRevision() {} + + public WikiPageRevision( + OffsetDateTime date, + String title, + String content, + String format, + WikiPage page, + User author) { + this.date = date; + this.title = title; + this.content = content; + this.format = format; + this.page = page; + this.author = author; + } } 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 a8d7db4..10112db 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java @@ -2,6 +2,7 @@ package eu.mulk.mulkcms2.benki.wiki; import static javax.ws.rs.core.MediaType.TEXT_HTML; +import eu.mulk.mulkcms2.benki.users.User; import io.quarkus.panache.common.Sort; import io.quarkus.qute.Template; import io.quarkus.qute.TemplateExtension; @@ -9,12 +10,13 @@ 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.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.time.temporal.TemporalAccessor; import java.util.Optional; import javax.inject.Inject; +import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.NotFoundException; import javax.ws.rs.POST; @@ -47,10 +49,11 @@ public class WikiResource { @GET @Path("/{pageName}") @Produces(TEXT_HTML) + @Authenticated public TemplateInstance getPage(@PathParam("pageName") String pageName) { Optional maybePage = WikiPageRevision.find( - "from WikiPageRevision rev" + " join fetch rev.author" + " where rev.title = ?1", + "from WikiPageRevision rev join fetch rev.author where rev.title = ?1", Sort.by("date").descending(), pageName) .firstResultOptional(); @@ -64,21 +67,43 @@ public class WikiResource { @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()); + public void updatePage( + @PathParam("pageName") String pageName, + @FormParam("title") String title, + @FormParam("content") String content) { + var userName = identity.getPrincipal().getName(); + + Optional maybeCurrentRevision = + WikiPageRevision.find( + "from WikiPageRevision rev join fetch rev.author where rev.title = ?1", + Sort.by("date").descending(), + pageName) + .firstResultOptional(); + if (maybeCurrentRevision.isEmpty()) { + throw new NotFoundException(); } - throw new NotImplementedYetException(); + var currentRevision = maybeCurrentRevision.get(); + + var pageRevision = + new WikiPageRevision( + OffsetDateTime.now(), + title, + content, + "html5", + currentRevision.page, + User.find("name = ?1", userName).singleResult()); + + WikiPageRevision.persist(pageRevision); } @GET @Path("/{pageName}/revisions") @Produces(TEXT_HTML) + @Authenticated public TemplateInstance getPageRevisions(@PathParam("pageName") String pageName) { Optional maybePrimaryRevision = WikiPageRevision.find( - "from WikiPageRevision rev" + " join fetch rev.author" + " where rev.title = ?1", + "from WikiPageRevision rev join fetch rev.author where rev.title = ?1", Sort.by("date").descending(), pageName) .firstResultOptional(); -- cgit v1.2.3