Created
June 18, 2015 08:03
-
-
Save pjazdzewski1990/9d0373424c0cd30f94f1 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
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