Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Player extends Temp {
- val gameId: BSONObjectID
- val personalDetails: abc.PersonalDetails // <- comes from shared library
- }
- case class FootballPlayer(var _id: Option[BSONObjectID] = None,
- gameId: BSONObjectID,
- personalDetails: abc.PersonalDetails,
- var created: Option[DateTime] = None,
- var updated: Option[DateTime] = None
- ) extends Player
- case class VideogamePlayer(var _id: Option[BSONObjectID] = None,
- gameId: BSONObjectID,
- personalDetails: abc.PersonalDetails,
- var created: Option[DateTime] = None,
- var updated: Option[DateTime] = None
- ) extends Player
- trait PlayerDAO[T <: Player] {
- def findById(_id: BSONObjectID)(implicit reads: Reads[T]): Future[Option[T]]
- def insert(t: T)(implicit writes: OWrites[T]): Future[T]
- }
- class MongoPlayerDAO[T <: Player] @Inject()(
- playerRepository: PlayerRepository[T]
- ) extends PlayerDAO[T] {
- def findById(_id: BSONObjectID)(implicit reads: Reads[T]): Future[Option[T]] = playerRepository.findById(_id)
- def insert(t: T)(implicit writes: OWrites[T]): Future[T] = playerRepository.insert(t).map(_ => t)
- }
- class PlayerService[T <: Player] @Inject()(playerDAO: PlayerDAO[T])(implicit reads: Reads[T], writes: OWrites[T]) {
- def findById(_id: BSONObjectID): Future[Option[T]] = playerDAO.findById(_id)
- def save(t: T): Future[T] = playerDAO.save(t)
- }
- class PlayerModule extends AbstractModule with ScalaModule {
- def configure() {
- bind[PlayerDAO[FootballPlayer]].to[MongoPlayerDAO[FootballPlayer]]
- bind[PlayerDAO[VideogamePlayer]].to[MongoPlayerDAO[VideogamePlayer]]
- // ...
- ()
- }
- }
- import models.FootballPlayer._
- import models.VideogamePlayer._
- class PlayerController @Inject()(
- val messagesApi: MessagesApi,
- footballPlayerService: PlayerService[FootballPlayer],
- videogamePlayerService: PlayerService[VideogamePlayer]
- ) extends Controller with I18nSupport
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement