2014-05-31 11:01:43 -06:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import play.api.mvc._, Results._
|
|
|
|
|
2014-06-29 04:51:53 -06:00
|
|
|
import io.prismic.Document
|
2014-05-31 11:01:43 -06:00
|
|
|
|
|
|
|
import lila.app._
|
|
|
|
import views._
|
|
|
|
|
|
|
|
object Blog extends LilaController {
|
|
|
|
|
2014-05-31 13:03:04 -06:00
|
|
|
private def blogApi = Env.blog.api
|
|
|
|
|
2014-06-29 04:51:53 -06:00
|
|
|
import Prismic._
|
|
|
|
|
2014-05-31 11:01:43 -06:00
|
|
|
def index(ref: Option[String]) = Open { implicit ctx =>
|
2014-05-31 13:03:04 -06:00
|
|
|
blogApi context ref flatMap { implicit prismic =>
|
2015-02-05 02:47:54 -07:00
|
|
|
blogApi.recent(prismic.api, ref, 20) flatMap {
|
|
|
|
case Some(response) => fuccess(Ok(views.html.blog.index(response)))
|
|
|
|
case _ => notFound
|
2014-05-31 11:01:43 -06:00
|
|
|
}
|
2014-05-31 13:03:04 -06:00
|
|
|
}
|
2014-05-31 11:01:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
def show(id: String, slug: String, ref: Option[String]) = Open { implicit ctx =>
|
2014-05-31 13:03:04 -06:00
|
|
|
blogApi context ref flatMap { implicit prismic =>
|
2014-05-31 14:50:04 -06:00
|
|
|
blogApi.one(prismic.api, ref, id) flatMap { maybeDocument =>
|
2014-05-31 11:01:43 -06:00
|
|
|
checkSlug(maybeDocument, slug) {
|
|
|
|
case Left(newSlug) => MovedPermanently(routes.Blog.show(id, newSlug, ref).url)
|
|
|
|
case Right(doc) => Ok(views.html.blog.show(doc))
|
|
|
|
}
|
|
|
|
}
|
2014-05-31 13:03:04 -06:00
|
|
|
}
|
2014-05-31 11:01:43 -06:00
|
|
|
}
|
|
|
|
|
2014-07-09 17:56:54 -06:00
|
|
|
def atom(ref: Option[String]) = Action.async { implicit req =>
|
|
|
|
blogApi context ref flatMap { implicit prismic =>
|
2015-02-05 02:47:54 -07:00
|
|
|
blogApi.recent(prismic.api, ref, 50) map {
|
|
|
|
_ ?? (_.results)
|
|
|
|
} map { docs =>
|
2014-07-09 17:56:54 -06:00
|
|
|
Ok(views.xml.blog.atom(docs)) as XML
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-31 11:01:43 -06:00
|
|
|
// -- Helper: Check if the slug is valid and redirect to the most recent version id needed
|
2014-06-01 17:01:39 -06:00
|
|
|
private def checkSlug(document: Option[Document], slug: String)(callback: Either[String, Document] => Result)(implicit ctx: lila.api.Context) =
|
2014-05-31 11:01:43 -06:00
|
|
|
document.collect {
|
|
|
|
case document if document.slug == slug => fuccess(callback(Right(document)))
|
|
|
|
case document if document.slugs.contains(slug) => fuccess(callback(Left(document.slug)))
|
|
|
|
} getOrElse notFound
|
|
|
|
}
|