From 1c2a8a70eed63a6b59dde22bdcb1d029e8485089 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Sun, 26 Apr 2020 06:09:57 +0200 Subject: KB54 Put posts in date buckets for templating. Change-Id: Ic17b2dede722f5962a55b9c4d3b4663a71480e9c --- .../java/eu/mulk/mulkcms2/benki/posts/Post.java | 28 +++++++++++++++++++++- .../eu/mulk/mulkcms2/benki/posts/PostResource.java | 7 +++--- 2 files changed, 31 insertions(+), 4 deletions(-) (limited to 'src/main/java/eu') 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 3a02e4e..bbfafa2 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java @@ -6,11 +6,15 @@ import eu.mulk.mulkcms2.benki.lazychat.LazychatMessage; import eu.mulk.mulkcms2.benki.users.User; import eu.mulk.mulkcms2.benki.users.User_; import io.quarkus.hibernate.orm.panache.PanacheEntityBase; +import java.time.LocalDate; import java.time.OffsetDateTime; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.Objects; import java.util.Set; +import java.util.TimeZone; +import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.json.bind.annotation.JsonbTransient; @@ -173,7 +177,9 @@ public abstract class Post extends PanacheEntityBase { public @CheckForNull final Integer nextCursor; public final List posts; - private PostPage( + private static final TimeZone timeZone = TimeZone.getDefault(); + + public PostPage( @CheckForNull Integer c0, @CheckForNull Integer c1, @CheckForNull Integer c2, @@ -183,6 +189,26 @@ public abstract class Post extends PanacheEntityBase { this.nextCursor = c2; this.posts = resultList; } + + public class Day { + public final @CheckForNull LocalDate date; + public final List posts; + + private Day(LocalDate date, List posts) { + this.date = date; + this.posts = posts; + } + } + + public List 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()) + .collect(Collectors.toUnmodifiableList()); + } } public static List findViewable( 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 5f3c8b4..92e2f4e 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java @@ -13,6 +13,7 @@ import com.rometools.rome.io.FeedException; import com.rometools.rome.io.WireFeedOutput; import eu.mulk.mulkcms2.benki.accesscontrol.PageKey; import eu.mulk.mulkcms2.benki.accesscontrol.Role; +import eu.mulk.mulkcms2.benki.posts.Post.PostPage; import eu.mulk.mulkcms2.benki.users.User; import io.quarkus.qute.Template; import io.quarkus.qute.TemplateExtension; @@ -116,7 +117,7 @@ public abstract class PostResource { } return postList - .data("posts", q.posts) + .data("postDays", q.days()) .data("feedUri", feedUri) .data("pageTitle", pageTitle) .data("showBookmarkForm", showBookmarkForm()) @@ -150,7 +151,7 @@ public abstract class PostResource { } return postList - .data("posts", q.posts) + .data("postDays", q.days()) .data("feedUri", feedUri) .data("pageTitle", pageTitle) .data("showBookmarkForm", showBookmarkForm()) @@ -191,7 +192,7 @@ public abstract class PostResource { var post = getPostIfVisible(id); return postList - .data("posts", List.of(post)) + .data("postDays", new PostPage<>(null, null, null, List.of(post)).days()) .data("pageTitle", pageTitle) .data("showBookmarkForm", false) .data("showLazychatForm", false) -- cgit v1.2.3