KB66 Use type-safe templates everywhere.
Change-Id: I879e76e5bbaf91349f6df4637d9dc15291a3ada1
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
index f783ba0..6c87096 100644
--- a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
+++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
@@ -245,7 +245,23 @@
return scope == Scope.top_level;
}
- public static class PostPage<T extends Post<? extends PostText>> {
+ public static class Day<T extends Post<? extends PostText<?>>> {
+ public final @CheckForNull LocalDate date;
+ public final List<T> posts;
+
+ private Day(LocalDate date, List<T> posts) {
+ this.date = date;
+ this.posts = posts;
+ }
+
+ public void cacheDescriptions() {
+ for (var post : posts) {
+ post.getTexts().values().forEach(PostText::getDescriptionHtml);
+ }
+ }
+ }
+
+ public static class PostPage<T extends Post<? extends PostText<?>>> {
public @CheckForNull final Integer prevCursor;
public @CheckForNull final Integer cursor;
public @CheckForNull final Integer nextCursor;
@@ -268,39 +284,23 @@
days().forEach(Day::cacheDescriptions);
}
- public class Day {
- public final @CheckForNull LocalDate date;
- public final List<T> posts;
-
- private Day(LocalDate date, List<T> posts) {
- this.date = date;
- this.posts = posts;
- }
-
- public void cacheDescriptions() {
- for (var post : posts) {
- post.getTexts().values().forEach(PostText::getDescriptionHtml);
- }
- }
- }
-
- public List<Day> days() {
+ public List<Day<T>> days() {
return posts.stream()
.collect(Collectors.groupingBy(post -> post.date.toLocalDate()))
.entrySet()
.stream()
- .map(x -> new Day(x.getKey(), x.getValue()))
- .sorted(Comparator.comparing((Day day) -> day.date).reversed())
+ .map(x -> new Day<T>(x.getKey(), x.getValue()))
+ .sorted(Comparator.comparing((Day<T> day) -> day.date).reversed())
.collect(Collectors.toUnmodifiableList());
}
}
- public static PostPage<Post<? extends PostText>> findViewable(
+ public static PostPage<Post<? extends PostText<?>>> findViewable(
PostFilter postFilter, Session session, @CheckForNull User viewer, @CheckForNull User owner) {
return findViewable(postFilter, session, viewer, owner, null, null, null);
}
- public static PostPage<Post<? extends PostText>> findViewable(
+ public static PostPage<Post<? extends PostText<?>>> findViewable(
PostFilter postFilter,
Session session,
@CheckForNull User viewer,
@@ -322,7 +322,7 @@
return findViewable(entityClass, session, viewer, owner, cursor, count, searchQuery);
}
- protected static <T extends Post<? extends PostText>> PostPage<T> findViewable(
+ protected static <T extends Post<? extends PostText<?>>> PostPage<T> findViewable(
Class<? extends T> entityClass,
Session session,
@CheckForNull User viewer,
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java
index 495c780..e1ba1aa 100644
--- a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java
+++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java
@@ -15,13 +15,11 @@
import eu.mulk.mulkcms2.benki.accesscontrol.PageKey;
import eu.mulk.mulkcms2.benki.accesscontrol.Role;
import eu.mulk.mulkcms2.benki.login.LoginRoles;
-import eu.mulk.mulkcms2.benki.login.LoginStatus;
import eu.mulk.mulkcms2.benki.posts.Post.PostPage;
import eu.mulk.mulkcms2.benki.users.User;
-import io.quarkus.qute.Template;
+import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateExtension;
import io.quarkus.qute.TemplateInstance;
-import io.quarkus.qute.api.ResourcePath;
import io.quarkus.security.identity.SecurityIdentity;
import java.math.BigInteger;
import java.net.URI;
@@ -88,9 +86,22 @@
@ConfigProperty(name = "mulkcms.posts.default-max-results")
int defaultMaxResults;
- @ResourcePath("benki/posts/postList.html")
- @Inject
- Template postList;
+ @CheckedTemplate(basePath = "benki/posts")
+ static class Templates {
+
+ public static native TemplateInstance postList(
+ List<Post.Day<Post<? extends PostText<?>>>> postDays,
+ @CheckForNull String feedUri,
+ String pageTitle,
+ Boolean showBookmarkForm,
+ Boolean showLazychatForm,
+ Boolean hasPreviousPage,
+ Boolean hasNextPage,
+ @CheckForNull Integer previousCursor,
+ @CheckForNull Integer nextCursor,
+ @CheckForNull Integer pageSize,
+ @CheckForNull String searchQuery);
+ }
@Inject protected SecurityIdentity identity;
@@ -135,18 +146,18 @@
feedUri += "?page-key=" + pageKey.key.toString(36);
}
- return postList
- .data("postDays", q.days())
- .data("feedUri", feedUri)
- .data("pageTitle", pageTitle)
- .data("showBookmarkForm", showBookmarkForm())
- .data("showLazychatForm", showLazychatForm())
- .data("hasPreviousPage", q.prevCursor != null)
- .data("hasNextPage", q.nextCursor != null)
- .data("previousCursor", q.prevCursor)
- .data("nextCursor", q.nextCursor)
- .data("pageSize", maxResults)
- .data("searchQuery", searchQuery);
+ return Templates.postList(
+ q.days(),
+ feedUri,
+ pageTitle,
+ showBookmarkForm(),
+ showLazychatForm(),
+ q.prevCursor != null,
+ q.nextCursor != null,
+ q.prevCursor,
+ q.nextCursor,
+ maxResults,
+ searchQuery);
}
@GET
@@ -173,17 +184,18 @@
feedUri += "?page-key=" + pageKey.key.toString(36);
}
- return postList
- .data("postDays", q.days())
- .data("feedUri", feedUri)
- .data("pageTitle", pageTitle)
- .data("showBookmarkForm", showBookmarkForm())
- .data("showLazychatForm", showLazychatForm())
- .data("hasPreviousPage", q.prevCursor != null)
- .data("hasNextPage", q.nextCursor != null)
- .data("previousCursor", q.prevCursor)
- .data("nextCursor", q.nextCursor)
- .data("pageSize", maxResults);
+ return Templates.postList(
+ q.days(),
+ feedUri,
+ pageTitle,
+ showBookmarkForm(),
+ showLazychatForm(),
+ q.prevCursor != null,
+ q.nextCursor != null,
+ q.prevCursor,
+ q.nextCursor,
+ maxResults,
+ null);
}
@Transactional
@@ -214,16 +226,18 @@
public TemplateInstance getPostHtml(@PathParam("id") int id) {
var post = getPostIfVisible(id);
- return postList
- .data("postDays", new PostPage<>(null, null, null, List.of(post)).days())
- .data("pageTitle", String.format("Post #%d", id))
- .data("showBookmarkForm", false)
- .data("showLazychatForm", false)
- .data("hasPreviousPage", false)
- .data("hasNextPage", false)
- .data("previousCursor", null)
- .data("nextCursor", null)
- .data("pageSize", null);
+ return Templates.postList(
+ new PostPage<Post<? extends PostText<?>>>(null, null, null, List.of(post)).days(),
+ null,
+ String.format("Post #%d", id),
+ false,
+ false,
+ false,
+ false,
+ null,
+ null,
+ null,
+ null);
}
@GET