Skip to content

Commit

Permalink
blättern zwischen den Bildern --> weiterblättern zum nächsten Bild im…
Browse files Browse the repository at this point in the history
…plementiert
  • Loading branch information
StefanPenndorf committed Oct 18, 2013
1 parent 859b251 commit c6ca3e7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/FotoVorfuehrer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FotoVorfuehrer @Inject()(
case Some(besitzer) => besitzer.fotoalbum
case None => None
}.map {
case Some(album) => Some((album, album.erstesFoto))
case Some(album) => album.fotoMitPosition(fotoPosition).map{f => (album, f)}
case None => None
}

Expand Down
41 changes: 41 additions & 0 deletions app/model/Fotoalbum.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,47 @@ case class Fotoalbum(
anzahlFotos: Long
) {

def fotoMitPosition(nummer: Long): Option[Foto] = {
if(nummer < 1) {
None
} else {
DB.withConnection { implicit connection =>
SQL(
"""
SELECT id,besitzer,foto FROM fotos f
WHERE f.besitzer = {besitzerId}
ORDER BY ID ASC
LIMIT {idx},1
"""
).on(
'besitzerId -> besitzer.id,
'idx -> (nummer - 1)
).as(Foto.simple.singleOpt)
}
}
}


def naechstePosition(foto: Foto): Option[Long] = {
val nextPos = positionVon(foto) + 1
fotoMitPosition(nextPos).map( _=> nextPos)
}

private def positionVon(foto: Foto): Long = {
DB.withConnection { implicit connection =>
SQL(
"""
SELECT COUNT(*)+1 as cnt FROM fotos f
WHERE f.besitzer = {besitzerId} AND
f.id < {fotoId}
"""
).on(
'besitzerId -> besitzer.id,
'fotoId -> foto.id.get
).as(scalar[Long].single)
}
}

lazy val erstesFoto: Foto = {
DB.withConnection { implicit connection =>
SQL(
Expand Down
14 changes: 8 additions & 6 deletions app/views/foto.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
@main(heading=heading.toString) {
<div id="foto-blaetterer">
<p>
<a id="vorhergehendesBild" href="#prev">&lt;&lt;&lt; Zur&uuml;ck</a>&nbsp;|&nbsp;<a id="naechstesBild" href="#next">Weiter &gt;&gt;&gt;</a>
<a id="vorhergehendesBild" href="#prev">&lt;&lt;&lt; Zur&uuml;ck</a>&nbsp;|&nbsp;

@album.naechstePosition(foto).map { pos =>
<a id="naechstesBild" href="@routes.FotoVorfuehrer.fotoalbum(album.besitzer.name, pos)">Weiter &gt;&gt;&gt;</a>
} getOrElse {
Weiter &gt;&gt;&gt;
}
</p>
</div>
<div>

<img style="display:block;float:right;" width="620px"
alt="Lukas hebt den Kopf: Ich will auch mit feiern!" title="Lukas hebt den Kopf: Ich will auch mit feiern!"
src="@routes.FotoVorfuehrer.foto(foto.id.get)">

<img style="display:block;float:right;" width="620px" src="@routes.FotoVorfuehrer.foto(foto.id.get)">
</div>

}
24 changes: 23 additions & 1 deletion test/net/cyphoria/weddingapp/model/FotoalbumTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,32 @@ class FotoalbumTest extends Specification {
"Fotoalbum eines Gastes" should {
"das erste Foto zurück geben" in DatenbankMit("einemGastMitEinemFoto") {
einGast.fotoalbum.get.erstesFoto must beEqualTo(Foto(Id(1), PNG_IMAGE_CONTENT))
einGast.fotoalbum.get.fotoMitPosition(1) must beSome(Foto(Id(1), PNG_IMAGE_CONTENT))
}

"das Foto mit der kleinsten ID als erstes Foto zurück geben, selbst wenn drei Fotos hochgeladen wurden" in DatenbankMit("einemGastMitDreiFotos") {
"das Foto mit der kleinsten ID als Foto mit Nummer 1 zurück geben, selbst wenn drei Fotos hochgeladen wurden" in DatenbankMit("einemGastMitDreiFotos") {
einGast.fotoalbum.get.erstesFoto must beEqualTo(Foto(Id(1), PNG_IMAGE_CONTENT))
einGast.fotoalbum.get.fotoMitPosition(1) must beSome(Foto(Id(1), PNG_IMAGE_CONTENT))
}

"das zweite Foto als Foto mit Fotonummer 2 zurück geben" in DatenbankMit("einemGastMitDreiFotos") {
einGast.fotoalbum.get.fotoMitPosition(2) must beSome(Foto(Id(2), JPEG_IMAGE_CONTENT))
}

"kein Foto mit negativer Fotonummer finden" in DatenbankMit("einemGastMitDreiFotos") {
einGast.fotoalbum.get.fotoMitPosition(-1) must beNone
}

"kein nächstes Foto für ein einziges Foto haben" in DatenbankMit("einemGastMitEinemFoto") {
einGast.fotoalbum.get.naechstePosition(Foto(Id(1), PNG_IMAGE_CONTENT)) must beNone
}

"das zweite Foto als nächstes Foto für das erste Foto haben" in DatenbankMit("einemGastMitDreiFotos") {
einGast.fotoalbum.get.naechstePosition(Foto(Id(1), PNG_IMAGE_CONTENT)) must beSome(2)
}

"kein nächstes Foto für das letzte Foto haben" in DatenbankMit("einemGastMitDreiFotos") {
einGast.fotoalbum.get.naechstePosition(Foto(Id(3), PNG_IMAGE_CONTENT)) must beNone
}

}
Expand Down
2 changes: 1 addition & 1 deletion test/resources/model/einemGastMitDreiFotos.dbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ users:

fotos:
- id: 1, besitzer: 1, foto: "89504E470D0A1A0A4142434445"
- id: 2, besitzer: 1, foto: "FFD8FF4142434445"
- id: 2, besitzer: 1, foto: "FFD8FFE000104A464946"
- id: 3, besitzer: 1, foto: "89504E470D0A1A0A4142434445"

0 comments on commit c6ca3e7

Please sign in to comment.