Skip to content

Commit

Permalink
#14 show full list
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Sep 16, 2016
1 parent fa8693d commit 7702009
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/main/java/io/jare/dynamo/DyBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public Iterator<Domain> domain(final String name) {
.iterator();
}

@Override
public Iterable<Domain> all() {
return this.table()
.frame()
.stream()
.map(DyDomain::new)
.collect(Collectors.<Domain>toList());
}

/**
* Table to work with.
* @return Table
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/jare/fake/FkBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public Iterator<Domain> domain(final String name) {
return Collections.<Domain>singleton(new FkDomain()).iterator();
}

@Override
public Iterable<Domain> all() {
return Collections.<Domain>singleton(new FkDomain());
}

}
6 changes: 6 additions & 0 deletions src/main/java/io/jare/model/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ public interface Base {
*/
Iterator<Domain> domain(String name);

/**
* All domains.
* @return Full list of all domains
*/
Iterable<Domain> all();

}
2 changes: 1 addition & 1 deletion src/main/java/io/jare/tk/TkApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private static Take regex(final Base base) throws IOException {
"image/png"
)
),
new FkRegex("/", new TkIndex()),
new FkRegex("/", new TkIndex(base)),
new FkAuthenticated(
new TkSecure(
new TkFork(
Expand Down
45 changes: 44 additions & 1 deletion src/main/java/io/jare/tk/TkIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@
*/
package io.jare.tk;

import io.jare.model.Base;
import io.jare.model.Domain;
import java.io.IOException;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rs.xe.XeAppend;
import org.takes.rs.xe.XeDirectives;
import org.takes.rs.xe.XeSource;
import org.takes.rs.xe.XeTransform;
import org.xembly.Directives;

/**
* Index page, for anonymous users.
Expand All @@ -36,11 +43,47 @@
*/
final class TkIndex implements Take {

/**
* Base.
*/
private final transient Base base;

/**
* Ctor.
* @param bse Base
*/
TkIndex(final Base bse) {
this.base = bse;
}

@Override
public Response act(final Request req) throws IOException {
return new RsPage(
"/xsl/index.xsl",
req
req,
new XeAppend(
"domains",
new XeTransform<>(
this.base.all(),
TkIndex::source
)
)
);
}

/**
* Convert event to Xembly.
* @param domain The event
* @return Xembly
* @throws IOException If fails
*/
private static XeSource source(final Domain domain) throws IOException {
return new XeDirectives(
new Directives()
.add("domain")
.add("name").set(domain.name()).up()
.add("owner").set(domain.owner()).up()
.up()
);
}

Expand Down
26 changes: 26 additions & 0 deletions src/main/xsl/index.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,31 @@
<xsl:text>In the nearest future we will charge you, if the traffic is too high.</xsl:text>
<xsl:text> In the mean time, please, don't abuse the system, keep your traffic to a reasonable limit.</xsl:text>
</p>
<xsl:if test="domains/domain">
<p>There are </p>
<xsl:value-of select="count(domains/domain)"/>
<p> domains registered now:</p>
<xsl:apply-templates select="domains"/>
</xsl:if>
<xsl:if test="not(domains/domain)">
<p>
<xsl:text>There are no registered domains yet.</xsl:text>
</p>
</xsl:if>
</xsl:template>
<xsl:template match="domains">
<ul>
<xsl:apply-templates select="domain"/>
</ul>
</xsl:template>
<xsl:template match="domain">
<li>
<xsl:value-of select="name"/>
<xsl:text> by </xsl:text>
<a href="https://github.com/">
<xsl:text>@</xsl:text>
<xsl:value-of select="owner"/>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
3 changes: 2 additions & 1 deletion src/test/java/io/jare/tk/TkIndexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package io.jare.tk;

import com.jcabi.matchers.XhtmlMatchers;
import io.jare.fake.FkBase;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
import org.takes.Take;
Expand All @@ -44,7 +45,7 @@ public final class TkIndexTest {
*/
@Test
public void rendersHomePage() throws Exception {
final Take take = new TkAppAuth(new TkIndex());
final Take take = new TkAppAuth(new TkIndex(new FkBase()));
MatcherAssert.assertThat(
XhtmlMatchers.xhtml(
new RsPrint(
Expand Down

0 comments on commit 7702009

Please sign in to comment.