Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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