Implement POST /wiki/{pageName}.

Change-Id: Idf82cac81b13eb2ac4098b847f6415b01cdb0605
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 @@
   @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 @@
 
 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.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 @@
   @GET
   @Path("/{pageName}")
   @Produces(TEXT_HTML)
+  @Authenticated
   public TemplateInstance getPage(@PathParam("pageName") String pageName) {
     Optional<WikiPageRevision> 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 @@
   @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<WikiPageRevision> 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<WikiPageRevision> 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();