Created
August 13, 2015 19:32
-
-
Save ttldtor/46d1d01bf4b9d4ad056d 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 org.mucode.smerp.api | |
import scalikejdbc._, async._, FutureImplicits._ | |
import org.joda.time.DateTime | |
import scala.concurrent._ | |
case class Call( | |
id: Long, | |
companyId: Long, | |
clientId: Long, | |
isProcessed: Boolean, | |
dateTime: DateTime, | |
description: String, | |
createdAt: DateTime, | |
createdBy: Long, | |
deletedAt: Option[DateTime] = None, | |
deletedBy: Option[Long] = None | |
) extends ShortenedNames { | |
def save()(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Call] = Call.save(this)(session, cxt) | |
def destroy(deletedBy: Long)(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Int] = Call.destroy(id, deletedBy)(session, cxt) | |
} | |
object Call extends SQLSyntaxSupport[Call] with ShortenedNames { | |
override val columnNames = Seq("id", "company_id", "client_id", "is_processed", "date_time", "description", | |
"created_at", "created_by", "deleted_at", "deleted_by") | |
def apply(c: SyntaxProvider[Call])(rs: WrappedResultSet): Call = apply(c.resultName)(rs) | |
def apply(c: ResultName[Call])(rs: WrappedResultSet): Call = new Call( | |
id = rs.long(c.id), | |
companyId = rs.long(c.companyId), | |
clientId = rs.long(c.clientId), | |
isProcessed = rs.boolean(c.isProcessed), | |
dateTime = rs.jodaDateTime(c.dateTime), | |
description = rs.string(c.description), | |
createdAt = rs.jodaDateTime(c.createdAt), | |
createdBy = rs.long(c.createdBy), | |
deletedAt = rs.jodaDateTimeOpt(c.deletedAt), | |
deletedBy = rs.longOpt(c.deletedBy) | |
) | |
lazy val c = Call.syntax("c") | |
private val isNotDeleted = sqls.isNull(c.deletedAt) | |
def find(id: Long)(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Option[Call]] = withSQL { | |
select.from(Call as c).where.eq(c.id, id).and.append(isNotDeleted) | |
}.map(Call(c)) | |
def findAll()(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[List[Call]] = withSQL { | |
select.from(Call as c).where.append(isNotDeleted).orderBy(c.id) | |
}.map(Call(c)) | |
def countAll()(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Long] = withSQL { | |
select(sqls.count).from(Call as c).where.append(isNotDeleted) | |
}.map(_.long(1)).single().future.map(_.get) | |
def findAllBy(where: SQLSyntax)( | |
implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[List[Call]] = withSQL { | |
select.from(Call as c) | |
.where.append(isNotDeleted).and.append(sqls"$where") | |
.orderBy(c.id) | |
}.map(Call(c)) | |
def countBy(where: SQLSyntax)( | |
implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Long] = withSQL { | |
select(sqls.count).from(Call as c).where.append(isNotDeleted).and.append(sqls"$where") | |
}.map(_.long(1)).single().future.map(_.get) | |
def create(companyId: Long, | |
clientId: Long, | |
isProcessed: Boolean = false, | |
dateTime: DateTime, | |
description: String, | |
createdAt: DateTime = DateTime.now(), | |
createdBy: Long)(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Call] = { | |
for { | |
id <- withSQL { | |
insert.into(Call).namedValues( | |
column.companyId -> companyId, | |
column.clientId -> clientId, | |
column.isProcessed -> isProcessed, | |
column.dateTime -> dateTime, | |
column.description -> description, | |
column.createdAt -> createdAt, | |
column.createdBy -> createdBy).returningId | |
}.updateAndReturnGeneratedKey() | |
} yield Call(id = id, companyId = companyId, clientId = clientId, isProcessed = isProcessed, dateTime = dateTime, | |
description = description, createdAt = createdAt, createdBy = createdBy) | |
} | |
def save(m: Call)(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Call] = { | |
withSQL { | |
update(Call).set( | |
column.clientId -> m.clientId, | |
column.isProcessed -> m.isProcessed, | |
column.dateTime -> m.dateTime, | |
column.description -> m.description | |
).where.eq(column.id, m.id).and.isNull(column.deletedAt) | |
}.update().future.map(_ => m) | |
} | |
def destroy(id: Long, deletedBy: Long)(implicit session: AsyncDBSession = AsyncDB.sharedSession, cxt: EC = ECGlobal): Future[Int] = withSQL { | |
update(Call).set( | |
column.deletedAt -> DateTime.now, | |
column.deletedBy -> deletedBy | |
).where.eq(column.id, id) | |
}.update().future() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment