Skip to content

Instantly share code, notes, and snippets.

@pjazdzewski1990
Created June 18, 2015 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjazdzewski1990/9d0373424c0cd30f94f1 to your computer and use it in GitHub Desktop.
Save pjazdzewski1990/9d0373424c0cd30f94f1 to your computer and use it in GitHub Desktop.
import io.scalac.inventory.db.IdWrappers._
import io.scalac.inventory.db.activate.Domain.{ItemEntity, OfficeEntity}
import io.scalac.inventory.db._
import io.scalac.inventory.db.activate.InventoryContext._
import net.fwbrasil.activate.statement.StatementSelectValue
import scala.util.Try
class ActivateRepository extends ItemRepository {
....
private def genericFinder[T <: Entity : Manifest](_id: String)(implicit tval1: (=> T) => StatementSelectValue) = Try {
transactional {
query {
(o: T) => where(o.id :== _id).select(o)(tval1)
}.head
}
}
///ITEM
override def createItem(name: String, code: Long, officeId: OfficeId): Try[ItemId] = Try {
transactional {
val item = new ItemEntity(name, code, genericFinder[OfficeEntity](officeId.v).get)
item.id
}
}
override def readItem(_id: ItemId): Try[Item] = genericFinder[ItemEntity](_id.v).map(_.canonical)
override def updateItem(itemId: ItemId, officeId: OfficeId): Try[Item] = Try {
transactional {
val original = genericFinder[ItemEntity](itemId.v)
val newOffice = genericFinder[OfficeEntity](officeId.v)
original.map(toUpdate => {
toUpdate.inOffice = newOffice.get
toUpdate
})
}.map(_.canonical)
}.flatten
override def deleteItem(_id: ItemId): Try[ItemId] = Try {
transactional {
val deleteResults = query {
(o: ItemEntity) => where(o.id :== _id.v).select(o)
}.map(_.delete)
deleteResults.headOption.map(x => _id).get /// throws on empty list, but this is what we want
}
}
....
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment