forked from playframework/play-slick
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Models.scala
154 lines (123 loc) · 4.04 KB
/
Models.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package models
import java.util.Date
import play.api.Play.current
import play.api.db.slick.DB
import play.api.db.slick.Config.driver.simple._
import slick.lifted.{Join, MappedTypeMapper}
case class Page[A](items: Seq[A], page: Int, offset: Long, total: Long) {
lazy val prev = Option(page - 1).filter(_ >= 0)
lazy val next = Option(page + 1).filter(_ => (offset + items.size) < total)
}
case class Company(id: Option[Long], name: String)
case class Computer(id: Option[Long] = None, name: String, introduced: Option[Date]= None, discontinued: Option[Date]= None, companyId: Option[Long]=None)
class Companies extends Table[Company]("COMPANY") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name", O.NotNull)
def * = id.? ~ name <>(Company.apply _, Company.unapply _)
def autoInc = * returning id
/**
* Construct the Map[String,String] needed to fill a select options set
*/
def options: Seq[(String, String)] = DB.withSession { implicit session =>
val query = (for {
company <- Companies
} yield (company.id, company.name)
).sortBy(_._2)
query.list.map(row => (row._1.toString, row._2))
}
/**
* Insert a new company
* @param company
*/
def insert(company: Company) {
DB.withSession { implicit session =>
Companies.autoInc.insert(company)
}
}
}
class Computers extends Table[Computer]("COMPUTER") {
implicit val javaUtilDateTypeMapper = MappedTypeMapper.base[java.util.Date, java.sql.Date](
x => new java.sql.Date(x.getTime),
x => new java.util.Date(x.getTime)
)
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name", O.NotNull)
def introduced = column[Date]("introduced", O.Nullable)
def discontinued = column[Date]("discontinued", O.Nullable)
def companyId = column[Long]("companyId", O.Nullable)
def * = id.? ~ name ~ introduced.? ~ discontinued.? ~ companyId.? <>(Computer.apply _, Computer.unapply _)
def autoInc = * returning id
val byId = createFinderBy(_.id)
/**
* Retrieve a computer from the id
* @param id
*/
def findById(id: Long): Option[Computer] = DB.withSession { implicit session =>
Computers.byId(id).firstOption
}
/**
* Count all computers
*/
def count: Int = DB.withSession { implicit session =>
Query(Computers.length).first
}
/**
* Count computers with a filter
* @param filter
*/
def count(filter: String) : Int = DB.withSession { implicit session =>
Query(Computers.where(_.name.toLowerCase like filter.toLowerCase).length).first
}
/**
* Return a page of (Computer,Company)
* @param page
* @param pageSize
* @param orderBy
* @param filter
*/
def list(page: Int = 0, pageSize: Int = 10, orderBy: Int = 1, filter: String = "%"): Page[(Computer, Option[Company])] = {
val offset = pageSize * page
DB.withSession { implicit session =>
val query =
(for {
(computer, company) <- Computers leftJoin Companies on (_.companyId === _.id)
if computer.name.toLowerCase like filter.toLowerCase()
}
yield (computer, company.id.?, company.name.?))
.drop(offset)
.take(pageSize)
val totalRows = count(filter)
val result = query.list.map(row => (row._1, row._2.map(value => Company(Option(value), row._3.get))))
Page(result, page, offset, totalRows)
}
}
/**
* Insert a new computer
* @param computer
*/
def insert(computer: Computer) {
DB.withSession { implicit session =>
Computers.autoInc.insert(computer)
}
}
/**
* Update a computer
* @param id
* @param computer
*/
def update(id: Long, computer: Computer) {
DB.withSession { implicit session =>
val computerToUpdate: Computer = computer.copy(Some(id))
Computers.where(_.id === id).update(computerToUpdate)
}
}
/**
* Delete a computer
* @param id
*/
def delete(id: Long) {
DB.withSession { implicit session =>
Computers.where(_.id === id).delete
}
}
}