KB66 Add comment count to post header.

Change-Id: I98fd99327fdfebaddfebe3541e6b1ccb1d78fe3c
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 d3e7712..1ddffa8 100644
--- a/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
+++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/Post.java
@@ -44,10 +44,12 @@
 import javax.persistence.ManyToOne;
 import javax.persistence.MapKey;
 import javax.persistence.OneToMany;
+import javax.persistence.OrderBy;
 import javax.persistence.SequenceGenerator;
 import javax.persistence.Table;
 import org.hibernate.annotations.Type;
 import org.hibernate.annotations.TypeDef;
+import org.hibernate.annotations.Where;
 
 @Entity
 @Table(name = "posts", schema = "benki")
@@ -113,6 +115,12 @@
   @JsonbTransient
   public Collection<LazychatMessage> referrers;
 
+  @ManyToMany(mappedBy = "referees")
+  @OrderBy("date DESC")
+  @Where(clause = "scope = 'comment'")
+  @JsonbTransient
+  public Collection<LazychatMessage> comments;
+
   @OneToMany(
       mappedBy = "post",
       fetch = FetchType.LAZY,
@@ -216,6 +224,9 @@
 
     cb = cb.where("post.scope").eq(Scope.top_level);
 
+    cb = cb.leftJoinFetch("post.comments", "comment");
+    cb = cb.fetch("comment.texts");
+
     return cb;
   }
 
@@ -393,16 +404,6 @@
     }
   }
 
-  public Collection<LazychatMessage> getComments() {
-    return referrers.stream()
-        .filter(l -> l.scope == Scope.comment)
-        .sorted(
-            Comparator.comparing(
-                    (LazychatMessage l) -> Objects.requireNonNullElse(l.date, OffsetDateTime.MIN))
-                .reversed())
-        .toList();
-  }
-
   public enum Visibility {
     PUBLIC,
     SEMIPRIVATE,
diff --git a/src/main/resources/META-INF/resources/cms2/base.css b/src/main/resources/META-INF/resources/cms2/base.css
index d68e07e..14d4f7a 100644
--- a/src/main/resources/META-INF/resources/cms2/base.css
+++ b/src/main/resources/META-INF/resources/cms2/base.css
@@ -323,7 +323,7 @@
   color: #555;
 }
 
-.post-self-link, .comment-self-link {
+.post-self-link, .comment-self-link, .comment-box-self-link {
   padding-left: 5px;
   padding-right: 5px;
 }
@@ -379,7 +379,7 @@
   line-height: 1em;
 }
 
-a.post-link, a.comment-link {
+a.post-link, a.comment-link, a.comment-box-link {
   text-decoration: none;
 }
 
diff --git a/src/main/resources/templates/PostResource/postList.html b/src/main/resources/templates/PostResource/postList.html
index 2f932ec..8c66e1d 100644
--- a/src/main/resources/templates/PostResource/postList.html
+++ b/src/main/resources/templates/PostResource/postList.html
@@ -66,6 +66,11 @@
                 <a class="post-link" href="/posts/{post.id}">
                   <span class="post-self-link">#</span>
                 </a>
+                {#if post.comments.size() > 0}
+                <a class="comment-box-link" href="/posts/{post.id}#comment-box-{post.id}">
+                  <span class="comment-box-self-link">({post.comments.size()})</span>
+                </a>
+                {/if}
 
                 <a href="{post.uri}" class="bookmark-title">
                   <h2 class="bookmark-title"><span class="bookmark-symbol">🔖 </span> {post.title}</h2>.
@@ -103,6 +108,11 @@
                 <a class="post-link" href="/posts/{post.id}">
                   <span class="post-self-link">#</span>
                 </a>
+                {#if post.comments.size() > 0}
+                <a class="comment-box-link" href="/posts/{post.id}#comment-box-{post.id}">
+                  <span class="comment-box-self-link">({post.comments.size()})</span>
+                </a>
+                {/if}
               </div>
             </header>
 
diff --git a/src/main/resources/templates/tags/commentBox.html b/src/main/resources/templates/tags/commentBox.html
index 40cec4a..abe73f5 100644
--- a/src/main/resources/templates/tags/commentBox.html
+++ b/src/main/resources/templates/tags/commentBox.html
@@ -1,7 +1,7 @@
 {@java.lang.Integer postId}
 {@java.util.List<eu.mulk.mulkcms2.benki.lazychat.LazychatMessage> comments}
 
-<div class="comment-box">
+<div class="comment-box" id="comment-box-{postId}">
   <script type="module" src="/lib.js"></script>
   <script type="module" src="/posts/commentBox.js"></script>