-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.scala
36 lines (35 loc) · 2.13 KB
/
Main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import scalikejdbc._
import java.time._
import scala.collection.Seq
case class User(id: Long, name: Option[String], companyId: Option[Long] = None, company: Option[Company] = None)
object User extends SQLSyntaxSupport[User] {
override val tableName = "users"
override val columns = Seq("id", "name", "company_id")
def opt(u: SyntaxProvider[User], c: SyntaxProvider[Company])(rs: WrappedResultSet): Option[User] = opt(u.resultName, c.resultName)(rs)
def opt(u: ResultName[User], c: ResultName[Company])(rs: WrappedResultSet): Option[User] = rs.longOpt(u.id).map(_ => apply(u, c)(rs))
def apply(u: SyntaxProvider[User])(rs: WrappedResultSet): User = apply(u.resultName)(rs)
def apply(u: ResultName[User])(rs: WrappedResultSet): User = User(rs.long(u.id), rs.stringOpt(u.name), rs.longOpt(u.companyId))
def apply(u: SyntaxProvider[User], c: SyntaxProvider[Company])(rs: WrappedResultSet): User = apply(u.resultName, c.resultName)(rs)
def apply(u: ResultName[User], c: ResultName[Company])(rs: WrappedResultSet): User = {
(apply(u)(rs)).copy(company = rs.longOpt(c.id).map(_ => Company(c)(rs)))
}
}
case class Company(id: Long, name: Option[String])
object Company extends SQLSyntaxSupport[Company] {
override val tableName = "companies"
override val columns = Seq("id", "name")
def apply(c: SyntaxProvider[Company])(rs: WrappedResultSet): Company = apply(c.resultName)(rs)
def apply(c: ResultName[Company])(rs: WrappedResultSet): Company = Company(rs.long(c.id), rs.stringOpt(c.name))
}
case class Group(id: Long, name: Option[String], createdAt: ZonedDateTime, members: Seq[User] = Nil)
object Group extends SQLSyntaxSupport[Group] {
override val tableName = "groups"
override val columns = Seq("id", "name", "created_at")
def apply(g: SyntaxProvider[Group])(rs: WrappedResultSet): Group = apply(g.resultName)(rs)
def apply(g: ResultName[Group])(rs: WrappedResultSet): Group = Group(rs.get(g.id), rs.get(g.name), rs.get(g.createdAt))
}
case class GroupMember(groupId: Long, userId: Long)
object GroupMember extends SQLSyntaxSupport[GroupMember] {
override val tableName = "group_members"
override val columns = Seq("group_id", "user_id")
}