-
Notifications
You must be signed in to change notification settings - Fork 40
/
Company.scala
142 lines (126 loc) · 3.92 KB
/
Company.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
package programmerlist
import scalikejdbc._, async._, FutureImplicits._
import scalikejdbc.jodatime.JodaParameterBinderFactory._
import scalikejdbc.jodatime.JodaTypeBinder._
import scala.concurrent._
import org.joda.time.DateTime
case class Company(
id: Long,
name: String,
url: Option[String] = None,
createdAt: DateTime,
deletedAt: Option[DateTime] = None
) extends ShortenedNames {
def save()(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Company] = Company.save(this)(session, cxt)
def destroy()(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Int] = Company.destroy(id)(session, cxt)
}
object Company extends SQLSyntaxSupport[Company] with ShortenedNames {
override val columnNames: Seq[String] =
Seq("id", "name", "url", "created_at", "deleted_at")
def apply(c: SyntaxProvider[Company])(rs: WrappedResultSet): Company =
apply(c.resultName)(rs)
def apply(c: ResultName[Company])(rs: WrappedResultSet): Company =
new Company(
id = rs.get[Long](c.id),
name = rs.get[String](c.name),
url = rs.get[Option[String]](c.url),
createdAt = rs.get[DateTime](c.createdAt),
deletedAt = rs.get[Option[DateTime]](c.deletedAt)
)
lazy val c = Company.syntax("c")
private val isNotDeleted = sqls.isNull(c.deletedAt)
def find(id: Long)(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Option[Company]] = withSQL {
select.from(Company as c).where.eq(c.id, id).and.append(isNotDeleted)
}.map(Company(c))
def findAll()(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[List[Company]] = withSQL {
select
.from(Company as c)
.where
.append(isNotDeleted)
.orderBy(c.id)
}.map(Company(c))
def countAll()(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Long] = withSQL {
select(sqls.count).from(Company as c).where.append(isNotDeleted)
}.map(rs => rs.long(1)).single.future().map(_.get)
def findAllBy(where: SQLSyntax)(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[List[Company]] = withSQL {
select
.from(Company as c)
.where
.append(isNotDeleted)
.and
.append(sqls"${where}")
.orderBy(c.id)
}.map(Company(c))
def countBy(where: SQLSyntax)(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Long] = withSQL {
select(sqls.count)
.from(Company as c)
.where
.append(isNotDeleted)
.and
.append(sqls"${where}")
}.map(_.long(1)).single.future().map(_.get)
def create(
name: String,
url: Option[String] = None,
createdAt: DateTime = DateTime.now
)(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Company] = {
for {
id <- withSQL {
insert
.into(Company)
.namedValues(
column.name -> name,
column.url -> url,
column.createdAt -> createdAt
)
.returningId // if you run this example for MySQL, please remove this line
}.updateAndReturnGeneratedKey
} yield Company(id = id, name = name, url = url, createdAt = createdAt)
}
def save(m: Company)(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Company] = {
withSQL {
update(Company)
.set(column.name -> m.name, column.url -> m.url)
.where
.eq(column.id, m.id)
.and
.isNull(column.deletedAt)
}.update.future().map(_ => m)
}
def destroy(id: Long)(implicit
session: AsyncDBSession = AsyncDB.sharedSession,
cxt: EC = ECGlobal
): Future[Int] = {
update(Company)
.set(column.deletedAt -> DateTime.now)
.where
.eq(column.id, id)
}
}