From a84be3bc9944ec08e361e73e043ee4aa9d4d474a Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Wed, 2 Nov 2022 05:33:37 +0100 Subject: feat(wiki): Implement new Wiki page creation. Change-Id: I807147f3a4d105c08a9ffda130afec36dc13228a --- build.xml | 1 + .../eu/mulk/mulkcms2/benki/wiki/WikiResource.java | 52 +++++++++++++++------- .../resources/META-INF/resources/wiki/wikiPage.js | 12 +++-- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/build.xml b/build.xml index 49e7beb..d80c6b2 100644 --- a/build.xml +++ b/build.xml @@ -89,6 +89,7 @@ + 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 4100dc2..cd10267 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/wiki/WikiResource.java @@ -13,6 +13,7 @@ import io.quarkus.security.identity.SecurityIdentity; import java.net.URI; import java.net.URISyntaxException; import java.time.OffsetDateTime; +import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.time.temporal.TemporalAccessor; @@ -59,16 +60,28 @@ public class WikiResource { @Produces(TEXT_HTML) @Authenticated public TemplateInstance getPage(@PathParam("pageName") String pageName) { + WikiPageRevision page; + Optional maybePage = WikiPageRevision.find( "from WikiPageRevision rev join fetch rev.author where rev.title = ?1", Sort.by("date").descending(), pageName) .firstResultOptional(); - if (maybePage.isEmpty()) { - throw new NotFoundException(); + if (maybePage.isPresent()) { + page = maybePage.get(); + } else { + var userName = identity.getPrincipal().getName(); + User user = + User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); + page = new WikiPageRevision(); + page.content = ""; + page.title = pageName; + page.date = OffsetDateTime.now(ZoneOffset.UTC); + page.format = "html5"; + page.author = user; } - var page = maybePage.get(); + return Templates.wikiPage(page); } @@ -96,6 +109,8 @@ public class WikiResource { } var userName = identity.getPrincipal().getName(); + User user = + User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult(); Optional maybeCurrentRevision = WikiPageRevision.find( @@ -103,26 +118,33 @@ public class WikiResource { Sort.by("date").descending(), pageName) .firstResultOptional(); - if (maybeCurrentRevision.isEmpty()) { - throw new NotFoundException(); + + final WikiPage page; + if (maybeCurrentRevision.isPresent()) { + // Update the existing page. + var currentRevision = maybeCurrentRevision.get(); + page = currentRevision.page; + + title = title != null ? title : currentRevision.title; + content = content != null ? content : currentRevision.content; + } else { + // Create a new page. + page = new WikiPage(); + page.persist(); + + title = title != null ? title : pageName; + content = content != null ? content : ""; } - var currentRevision = maybeCurrentRevision.get(); var pageRevision = - new WikiPageRevision( - OffsetDateTime.now(), - title != null ? title : currentRevision.title, - content != null ? content : currentRevision.content, - "html5", - currentRevision.page, - User.find("from BenkiUser u join u.nicknames n where ?1 = n", userName).singleResult()); - - pageRevision.persistAndFlush(); + new WikiPageRevision(OffsetDateTime.now(), title, content, "html5", page, user); + pageRevision.persist(); return jsonProvider .createObjectBuilder() .add("status", "ok") .add("content", pageRevision.enrichedContent()) + .add("title", pageRevision.title) .build(); } diff --git a/src/main/resources/META-INF/resources/wiki/wikiPage.js b/src/main/resources/META-INF/resources/wiki/wikiPage.js index 9f96732..0e35d44 100644 --- a/src/main/resources/META-INF/resources/wiki/wikiPage.js +++ b/src/main/resources/META-INF/resources/wiki/wikiPage.js @@ -4,6 +4,8 @@ window.addEventListener('DOMContentLoaded', () => { let editor = ContentTools.EditorApp.get(); editor.init('*[data-editable]', 'data-name'); + let {pageTitle} = document.getElementById('wiki-page').dataset; + editor.addEventListener('saved', async function (ev) { document.getElementById("warning-panel").close(); @@ -20,8 +22,6 @@ window.addEventListener('DOMContentLoaded', () => { requestParams.append(name, regions[name]); } - let {pageTitle} = document.getElementById('wiki-page').dataset; - let response = await fetch(`/wiki/${pageTitle}`, { method: 'POST', body: requestParams @@ -32,7 +32,7 @@ window.addEventListener('DOMContentLoaded', () => { document.getElementById("warning-text").innerText = `Failed to save page: ${response.statusText}`; this.busy(false); -return; + return; } let status = await response.json(); @@ -41,13 +41,17 @@ return; document.getElementById("warning-text").innerText = `Failed to save page: ${JSON.stringify(status)}`; this.busy(false); -return; + return; } if (status.hasOwnProperty("content")) { document.getElementById("wiki-content").innerHTML = status.content; } + if (status.hasOwnProperty("title")) { + pageTitle = status.title; + } + this.busy(false); }); }); -- cgit v1.2.3