Skip to content

Instantly share code, notes, and snippets.

@benshaw
Created July 5, 2018 17:01
Show Gist options
  • Save benshaw/b494b59537842e8b0962c8c3514872fd to your computer and use it in GitHub Desktop.
Save benshaw/b494b59537842e8b0962c8c3514872fd to your computer and use it in GitHub Desktop.
Refactor Attempt 1
Routes -> GET /v1/reports/:reportId v1.Controller.ListReturn(action = GetReportById(reportId:Long))
trait DbAction[T] {
def query(): DBIOAction[T, NoStream, Nothing]
}
case class GetReportById(id: Report.Id)(implicit reports: TableQuery[ReportsTable]) extends DbAction[Seq[Report]] {
override def query(): DBIOAction[Seq[Report], NoStream, Effect.Read] = {
val query = reports //.filter(reportFilter)
query.result
.map(_.sortWith((a, b) => a.date.validFrom.compareTo(b.date.validFrom) > 0))
}
}
class Controller @Inject()(protected implicit val dbConfigProvider: DatabaseConfigProvider,
cc: ControllerComponents) extends AbstractController(cc) with play.api.i18n.I18nSupport {
class DAO(protected val dbConfigProvider: DatabaseConfigProvider)(implicit executionContext: ExecutionContext)
extends HasDatabaseConfigProvider[MyPostgresProfile] {
implicit val Reports = TableQuery[ReportsTable]
def run[R](action: DbAction[R]): Future[R] = db.run(action.query())
}
val dao = new DAO(dbConfigProvider)
val logger: Logger = Logger(this.getClass)
implicit val ec: ExecutionContext = defaultExecutionContext
def listReturn[R](action: DbAction[R]) = Action.async {
logger.trace(s"Listing")
dao.run(action)
.map(f => Ok(Json.toJson(f)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment