Created
January 19, 2013 21:49
-
-
Save anonymous/4575417 to your computer and use it in GitHub Desktop.
Squeryl+Play not working so well
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers | |
import play.api._ | |
import play.api.mvc._ | |
import org.squeryl.PrimitiveTypeMode._ | |
import models.{Database, FrontPageUpdate} | |
import com.codahale.jerkson.Json | |
object Application extends Controller { | |
def index = Action { | |
val updates = from(Database.frontPageUpdates)( | |
u => select(u) | |
orderBy(u.created_on desc) | |
) | |
Ok(views.html.index(updates.all)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@(updates: List[FrontPageUpdate]) | |
@import helper._ | |
@main("Lumos Pottery") { | |
<script src="@routes.Assets.at("javascripts/index.min.js")" type="text/javascript"></script> | |
<div id="welcome">Welcome text goes here</div> | |
<div id="updates"> | |
<ul> | |
@updates.map { update => | |
<li>@update.title</li> | |
} | |
</ul> | |
</div> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import java.sql.SQLException | |
import play.api.db._ | |
import play.api.Play.current | |
import org.squeryl.KeyedEntity | |
import org.squeryl.PrimitiveTypeMode._ | |
import org.squeryl.dsl._ | |
import org.squeryl.Schema | |
import org.squeryl.annotations.Column | |
import org.squeryl.{Session, SessionFactory} | |
import scala.util.control.Exception.allCatch | |
import java.sql.Timestamp | |
import play.api.Logger | |
/** | |
* Database object | |
* 1. Defines our tables | |
* 2. Sets up our relationships | |
* 3. Defines our constraints and validations (TODO) | |
*/ | |
object Database extends Schema { | |
val galleries = table[Gallery]("galleries") | |
val images = table[Image]("images") | |
val tags = table[Tag]("tags") | |
val frontPageUpdates = table[FrontPageUpdate]("front_page_updates") | |
val users = table[User]("users") | |
val galleryImages = oneToManyRelation(galleries, images). | |
via( (g, i) => g.id === i.gallery ) | |
val imageTags = manyToManyRelation(images, tags). | |
via[ImageTag]( (i,t,it) => (it.imageId === t.id, i.id === it.tagId) ) | |
val userPosts = oneToManyRelation(users, frontPageUpdates). | |
via( (u, p) => u.id === p.author ) | |
} | |
/** | |
* Automatically update created_on and updated_on values | |
*/ | |
trait CreationTimeMonitoring { | |
val created_on: Timestamp = new Timestamp(System.currentTimeMillis) | |
val updated_on: Timestamp = new Timestamp(System.currentTimeMillis) | |
} | |
/** | |
* Provide a primary key to tables who inherit from this class | |
*/ | |
class BaseEntity extends KeyedEntity[Long] { | |
val id: Long = 0 | |
} | |
/** | |
* Table defition for galleries. | |
* Takes: Name of a gallery | |
* Relationships: images (OneToMany) | |
*/ | |
case class Gallery( var name: String ) extends BaseEntity | |
with CreationTimeMonitoring { | |
def this() = this(""); | |
lazy val images : OneToMany[Image] = Database.galleryImages.left(this) | |
} | |
case class Image( var gallery : Long, | |
var path : String, | |
var name : String ) extends BaseEntity { | |
//with CreationTimeMonitoring { | |
def this() = this(0, "", ""); | |
lazy val tags = Database.imageTags.left(this) | |
} | |
case class Tag( var name: String ) extends BaseEntity { | |
def this() = this(""); | |
} | |
case class FrontPageUpdate( var author: Long, var title: String, var body: String ) | |
extends BaseEntity | |
with CreationTimeMonitoring { | |
import Database._ | |
def this() = this(0, "", "") | |
lazy val owner : ManyToOne[User] = Database.userPosts.right(this) | |
def all = from(frontPageUpdates)( | |
u => select(u) | |
orderBy(u.created_on desc) | |
).toList | |
} | |
case class ImageTag( var imageId : Long, | |
var tagId : Long ) | |
extends KeyedEntity[CompositeKey2[Long,Long]] { | |
//with CreationTimeMonitoring { | |
def id = compositeKey( imageId, tagId ) | |
def this() = this(0, 0) | |
} | |
case class User( var email : String, | |
var password : String, | |
var name : String ) extends BaseEntity { | |
//with CreationTimeMonitoring { | |
def this() = this("", "", ""); | |
lazy val posts : OneToMany[FrontPageUpdate] = Database.userPosts.left(this) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment