diff --git a/resources/views/all.dt b/resources/views/all.dt index bdbf4b1..cbcd09e 100644 --- a/resources/views/all.dt +++ b/resources/views/all.dt @@ -2,7 +2,7 @@ extends ./layout block content - import citesystem.rest : FullCiteData; - p#desc Alle #{llen} bekannten Zitate. + p#desc Alle #{llen} bekannten Zitate. – #[a(href="/all_paginated?pagesize=10&page=1") Seitenweise] div#citebody - foreach(FullCiteData cite; cites) include rendercite diff --git a/source/app.d b/source/app.d index 6fc9df5..b1ae35b 100644 --- a/source/app.d +++ b/source/app.d @@ -5,10 +5,10 @@ import vibe.http.router : URLRouter; import poodinis; -public: // Dependenciy injection container -auto dependencies = new shared DependencyContainer(); +private auto dependencies = new shared DependencyContainer(); +public: static this() { dependencies.register!(CiteApiSpecs, CiteApi); } diff --git a/source/citesystem/server/paginationinfo.d b/source/citesystem/server/paginationinfo.d index 34173c7..e961ba4 100644 --- a/source/citesystem/server/paginationinfo.d +++ b/source/citesystem/server/paginationinfo.d @@ -2,17 +2,32 @@ module citesystem.server.paginationinfo; private import diet.dom : Node; +/** + * Provides information necessary to display and perform + * pagination. + */ public struct PaginationInfo { + /// How many elements should be on one page const size_t pagesize; + /// Which page we are currently on const size_t currentPage; - const size_t numberOfElements; - this(size_t page, size_t pagesize,size_t count) { + private const size_t numberOfElements; + + /** + * Initialize using information necessary at page call. + * Params: + * page = the current page number + * pagesize = elements on one page + * numberOfElements = elements in total + */ + this(size_t page, size_t pagesize, size_t numberOfElements) { this.currentPage = page; this.pagesize = pagesize; - this.numberOfElements = count; + this.numberOfElements = numberOfElements; } + /// The page number of the last page containing at least on item. @property size_t lastPage() const { auto numberOfFullPages = numberOfElements / pagesize; @@ -23,6 +38,7 @@ public struct PaginationInfo { } } + /// The pagenumbers of all pages that should be shown in the pagination section. @property size_t[] pagesToShow() const { import std.range : iota; @@ -35,20 +51,27 @@ public struct PaginationInfo { return iota(firstPageLabel, lastPageLabel + 1).array(); } + /// Pagenumber of the first page to show in the pagination template. private ulong firstPageLabel() const { return (currentPage > 3) ? currentPage - 3 : 1; } + /// Pagenumber of the last page to include in the pagination template. private ulong lastPageLabel() const { import std.algorithm.comparison : min; return min(currentPage + 3, lastPage); } + /// The element number of the first element to show for the current page. @property size_t firstCiteOffset() const { return (pagesize * (currentPage - 1)); } + /** + * The element number of the last element to show for the current page. + * This also accounts for "last" pages that are not completely filled. + */ @property size_t lastCite() const { import std.algorithm.comparison : min; @@ -58,30 +81,23 @@ public struct PaginationInfo { ); } - @property - Node[] allMarks() const { - import diet.parser : parseDietRaw, identity; - import diet.input : InputFile; - InputFile inputFile = InputFile("pageinationInfo-footer", q{a(href="https://pheerai.de/") HP}); - - return parseDietRaw!identity(inputFile); - } - + /// Return whether front- respectively back-truncation marks are necessary @property bool needsFrontTruncation() const { return firstPageLabel != 1; } - + /// ditto @property bool needsBackTruncation() const { return lastPageLabel != lastPage; } + /// Return whether next- respectively previous-page-links are necessary. @property bool needsNextPage() const { return currentPage != lastPage; } - + /// ditto @property bool needsPreviousPage() const { return currentPage != 1; diff --git a/source/citesystem/server/system.d b/source/citesystem/server/system.d index 8728be3..ea5c15c 100644 --- a/source/citesystem/server/system.d +++ b/source/citesystem/server/system.d @@ -25,7 +25,7 @@ final class CiteSystem { * Default view. */ public void get() const { - string title = "Index"; + const title = "Index"; render!("index.dt", title); } @@ -42,9 +42,9 @@ final class CiteSystem { * id = The numerical id of the citation to view. */ public void getCite(long id) { - string title = "Number " ~ id.to!string; - FullCiteData cite = this.db.get(id); - string desc = "Zitat Nr %d".format(id); + const title = "Number " ~ id.to!string; + const cite = this.db.get(id); + const desc = "Zitat Nr %d".format(id); render!("cite.dt", title, cite, desc); } @@ -52,9 +52,9 @@ final class CiteSystem { * Renders a random citation. */ public void getRandom() { - string title = "Zufälliges Zitat"; - string desc = title; - FullCiteData cite = this.db.getRandomCite(); + const title = "Zufälliges Zitat"; + const desc = title; + const cite = this.db.getRandomCite(); render!("cite.dt", title, cite, desc); } @@ -62,9 +62,9 @@ final class CiteSystem { * Renders all citations. */ public void getAll() { - string title = "Alle Zitate"; + const title = "Alle Zitate"; // Sort with descending key, e.g. newest quote in front - FullCiteData[] cites = this.db.getAll(); + const cites = this.db.getAll(); const llen = cites.length; const start = llen; render!("all.dt", title, cites, llen, start); @@ -74,18 +74,15 @@ final class CiteSystem { * Get pageinated results (mock only) */ public void getAllPaginated(size_t page, size_t pagesize) { - string title = "Erste Zitate"; + const title = "Erste Zitate"; const count = this.db.count(); - const count2 = this.db.count(); const paginationInfo = PaginationInfo(page, pagesize, count); - FullCiteData[] cites = this.db.getPaginated(paginationInfo); + const cites = this.db.getPaginated(paginationInfo); render!("all_paginated.dt", title, cites, paginationInfo); } - public void getAdd() - // const - { - string title = "Zitat hinzufügen"; + public void getAdd() { + const title = "Zitat hinzufügen"; render!("add.dt", title); } @@ -95,8 +92,8 @@ final class CiteSystem { */ public void getModify(long id) { const FullCiteData cite = this.db.get(id); - string citetext = cite.cite; - string title = "Zitat Nr. %d bearbeiten".format(id); + const citetext = cite.cite; + const title = "Zitat Nr. %d bearbeiten".format(id); render!("modify.dt", id, title, citetext); } diff --git a/source/citesystem/util.d b/source/citesystem/util.d index f7a71af..37c8a19 100644 --- a/source/citesystem/util.d +++ b/source/citesystem/util.d @@ -22,6 +22,5 @@ auto toJson(T)(T input) { * The JSON string representation of the input object. */ string toJsonString(T)(T input) { - import vibe.data.json; return input.toJson.to!string; }