-
-
Save timothyklim/3163503 to your computer and use it in GitHub Desktop.
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.util.{Date} | |
import play.db.sql._ | |
import play.db.sql.SqlParser._ | |
// User | |
case class User(id: Pk[Long], | |
email: String, | |
password: String, | |
fullname: String, | |
isAdmin: Boolean) | |
object User extends Magic[User] { | |
def connect(email: String, password: String) = | |
SQL(""" select * from User | |
where email = {email} | |
and password = {password}""" ) | |
.on("email" -> email, "password" -> password) | |
.as(User?) | |
} | |
// Post | |
case class Post(id: Pk[Long], | |
title: String, | |
content: String, | |
postedAt: Date, | |
author_id: Long) | |
object Post extends Magic[Post] { | |
private val postWithAuthor = Post ~< User | |
def allWithAuthor = | |
SQL(""" select * from Post p | |
join User u on p.author_id = u.id | |
order by p.postedAt desc """) | |
.as(postWithAuthor*) | |
def allWithAuthorAndComments = | |
SQL(""" select * from Post p | |
join User u on p.author_id = u.id | |
left join Comment c on c.post_id = p.id | |
order by p.postedAt desc """) | |
.as( ( Post ~< User spanM( Comment ) ) ^^ flatten *) | |
def byIdWithAuthorAndComments(id: Long) = | |
SQL(""" select * from Post p | |
join User u on p.author_id = u.id | |
left join Comment c on c.post_id = p.id | |
where p.id = {id} """) | |
.on("id" -> id) | |
.as((postWithAuthor ~< (Comment*))?) | |
} | |
// Comment | |
case class Comment(id: Pk[Long], | |
author: String, | |
content: String, | |
postedAt: Date, | |
post_id: Long) | |
object Comment extends Magic[Comment] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment