Skip to content

Commit

Permalink
Merge pull request #58 from f3l/000-updateDocs
Browse files Browse the repository at this point in the history
Minor maintenance
  • Loading branch information
pheerai authored Jan 12, 2019
2 parents ea17caa + e8ce7b2 commit 2eb7d23
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion resources/views/all.dt
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
44 changes: 30 additions & 14 deletions source/citesystem/server/paginationinfo.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
33 changes: 15 additions & 18 deletions source/citesystem/server/system.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class CiteSystem {
* Default view.
*/
public void get() const {
string title = "Index";
const title = "Index";
render!("index.dt", title);
}

Expand All @@ -42,29 +42,29 @@ 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);
}

/**
* 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);
}

/**
* 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);
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion source/citesystem/util.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2eb7d23

Please sign in to comment.