Skip to content

Commit

Permalink
#4 add pagination to taxonomy page
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Dec 17, 2023
1 parent e51d4c0 commit c13daa8
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.Page;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -41,4 +42,6 @@ public interface Taxonomies {
public Set<String> values (Taxonomy taxonomy);

public List<ContentNode> withValue (Taxonomy taxonomy, Object value);

public Page<ContentNode> withValue (Taxonomy taxonomy, Object value, long page, long size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.Page;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomies;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomy;
import com.github.thmarx.cms.api.eventbus.EventListener;
Expand Down Expand Up @@ -108,6 +109,18 @@ public List<ContentNode> withValue(final Taxonomy taxonomy, final Object value)
return nodes;
}

@Override
public Page<ContentNode> withValue(Taxonomy taxonomy, Object value, long page, long size) {

if (taxonomy.isArray()) {
return fileSystem.query((node, index) -> node).whereContains(taxonomy.getField(), value)
.page(page, size);
} else {
return fileSystem.query((node, index) -> node).where(taxonomy.getField(), value)
.page(page, size);
}
}

@Override
public void consum(SitePropertiesChanged event) {
reloadTaxonomies();
Expand Down
2 changes: 2 additions & 0 deletions cms-server/hosts/demo/content/blog/2023-09/new-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: New post in September
publish_date: 2023-09-10
template: blog-entry.html
taxonomy:
tags: [blue]
---

September post today.
2 changes: 1 addition & 1 deletion cms-server/hosts/demo/templates/taxonomy-single.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<div class="container">
<h2 th:text="${meta.title}"></h2>
<ul th:each="node: ${nodes}">
<ul th:each="node: ${page.items}">
<ul>
<a th:href="${node.path()}">
<span th:text="${node.name()}"></span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.thmarx.cms.api.content.ContentParser;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.db.Page;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomy;
import com.github.thmarx.cms.api.extensions.TemplateModelExtendingExtentionPoint;
import com.github.thmarx.cms.api.request.RequestContext;
Expand Down Expand Up @@ -87,13 +88,13 @@ public String render(final Path contentFile, final RequestContext context, final
});
}

public String renderTaxonomy(final Taxonomy taxonomy, final RequestContext context, final Map<String, Object> meta, final List<Node> nodes) throws IOException {
public String renderTaxonomy(final Taxonomy taxonomy, final RequestContext context, final Map<String, Object> meta, final Page<Node> page) throws IOException {
var contentFile = db.getFileSystem().resolve("content").resolve("index.md");

return render(contentFile, context, Collections.emptyMap(), meta, "", (model) -> {
model.values.put("taxonomy", taxonomy);
model.values.put("taxonomy_values", db.getTaxonomies().values(taxonomy));
model.values.put("nodes", nodes);
model.values.put("page", page);

});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.content.ContentParser;
import com.github.thmarx.cms.api.content.TaxonomyResponse;
import com.github.thmarx.cms.api.db.ContentNode;
import com.github.thmarx.cms.api.db.DB;
import com.github.thmarx.cms.api.db.Page;
import com.github.thmarx.cms.api.db.taxonomy.Taxonomy;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.api.request.RequestContext;
import com.github.thmarx.cms.api.request.features.RequestFeature;
import com.github.thmarx.cms.api.utils.NodeUtil;
Expand All @@ -52,7 +51,6 @@ public class TaxonomyResolver {

private final ContentRenderer contentRenderer;
private final ContentParser contentParser;

private final DB db;

private Optional<Taxonomy> getTaxonomy(final RequestContext context) {
Expand Down Expand Up @@ -87,18 +85,21 @@ public Optional<TaxonomyResponse> getTaxonomyResponse(final RequestContext conte
}

try {
int page = context.get(RequestFeature.class).getQueryParameterAsInt("page", Constants.DEFAULT_PAGE);
int size = context.get(RequestFeature.class).getQueryParameterAsInt("size", Constants.DEFAULT_PAGE_SIZE);

var taxonomy = taxonomyOptional.get();

String template = "taxonomy.html";
var meta = new HashMap<String, Object>();

Optional<String> value = getTaxonomyValue(context);
List<Node> nodes = List.of();
Page<Node> resultPage = Page.EMPTY;
if (value.isPresent()) {
template = "taxonomy-single.html";
meta.put(Constants.MetaFields.TITLE, taxonomy.getTitle() + " - " + value.get());
nodes = db.getTaxonomies().withValue(taxonomy, value.get()).stream().map(node -> {
var contentPage = db.getTaxonomies().withValue(taxonomy, value.get(), page, size);
var nodes = contentPage.getItems().stream().map(node -> {
try {
var name = NodeUtil.getName(node);
final Path contentBase = db.getFileSystem().resolve("content/");
Expand All @@ -112,12 +113,13 @@ public Optional<TaxonomyResponse> getTaxonomyResponse(final RequestContext conte
}
return null;
}).filter(node -> node != null).toList();
resultPage.setItems(nodes);
} else {
meta.put(Constants.MetaFields.TITLE, taxonomy.getTitle());
}
meta.put(Constants.MetaFields.TEMPLATE, template);

String content = contentRenderer.renderTaxonomy(taxonomy, context, meta, nodes);
String content = contentRenderer.renderTaxonomy(taxonomy, context, meta, resultPage);

return Optional.of(new TaxonomyResponse(content, taxonomy));
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.github.thmarx.cms.api.eventbus.EventBus;
import com.github.thmarx.cms.api.eventbus.events.SitePropertiesChanged;
import com.github.thmarx.cms.api.theme.Theme;
import com.github.thmarx.cms.filesystem.FileDB;
import com.github.thmarx.cms.media.MediaManager;
import com.github.thmarx.cms.request.RequestContextFactory;
import com.github.thmarx.cms.server.jetty.handler.JettyContentHandler;
Expand All @@ -39,7 +38,6 @@
import com.google.inject.Key;
import com.google.inject.name.Names;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.http.pathmap.PathSpec;
import org.eclipse.jetty.server.Handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,6 @@
public class JettyTaxonomyHandler extends Handler.Abstract {
private final TaxonomyResolver taxonomyResolver;
private final RequestContextFactory requestContextFactory;
private final DB db;

private Optional<Taxonomy> getTaxonomy (final Request request) {
var uri = request.getHttpURI().getPath();

var slug = uri.split("/")[1];

return db.getTaxonomies().forSlug(slug);
}

private Optional<String> getTaxonomyValue (final Request request) {
var uri = request.getHttpURI().getPath();
var uriParts = uri.split("/");

return uriParts.length == 3 ? Optional.of(uriParts[2]) : Optional.empty();
}

@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception {
Expand Down

0 comments on commit c13daa8

Please sign in to comment.