user blog WIP
parent
75b168673a
commit
0f7a024bcc
|
@ -8,6 +8,8 @@ import lila.ublog.UblogPost
|
|||
|
||||
final class Ublog(env: Env) extends LilaController(env) {
|
||||
|
||||
import views.html.ublog.bits.{ url => urlOf }
|
||||
|
||||
def index(username: String, page: Int) = Open { implicit ctx =>
|
||||
OptionFuOk(env.user.repo named username) { user =>
|
||||
env.ublog.api.liveByUser(user, page) map { posts =>
|
||||
|
@ -20,7 +22,7 @@ final class Ublog(env: Env) extends LilaController(env) {
|
|||
OptionFuResult(env.user.repo named username) { user =>
|
||||
env.ublog.api.find(UblogPost.Id(id)) map {
|
||||
_ ?? { post =>
|
||||
if (!user.is(post.user) || slug != post.slug) Redirect(html.ublog.bits.url(post))
|
||||
if (!user.is(post.user) || slug != post.slug) Redirect(urlOf(post))
|
||||
else {
|
||||
val markup = scalatags.Text.all.raw(env.ublog.markup(post.markdown))
|
||||
Ok(html.ublog.post(user, post, markup))
|
||||
|
@ -32,11 +34,25 @@ final class Ublog(env: Env) extends LilaController(env) {
|
|||
|
||||
def form(username: String) = Auth { implicit ctx => me =>
|
||||
if (!me.is(username)) Redirect(routes.Ublog.form(me.username)).fuccess
|
||||
else Ok(html.ublog.post.form(me, env.ublog.form.post)).fuccess
|
||||
else Ok(html.ublog.post.create(me, env.ublog.form.create)).fuccess
|
||||
}
|
||||
|
||||
def create(username: String) = AuthBody { implicit ctx => me =>
|
||||
???
|
||||
env.ublog.form.create
|
||||
.bindFromRequest()(ctx.body, formBinding)
|
||||
.fold(
|
||||
err => BadRequest(html.ublog.post.create(me, err)).fuccess,
|
||||
data =>
|
||||
env.ublog.api.create(data, me) map { post =>
|
||||
Redirect(urlOf(post))
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
def edit(username: String, id: String) = AuthBody { implicit ctx => me =>
|
||||
OptionOk(env.ublog.api.find(UblogPost.Id(id)).map(_.filter(_.isBy(me)))) { post =>
|
||||
html.ublog.post.edit(me, post, env.ublog.form.edit(post))
|
||||
}
|
||||
}
|
||||
|
||||
def update(username: String, id: String) = AuthBody { implicit ctx => me =>
|
||||
|
|
|
@ -6,7 +6,7 @@ import play.api.data.Form
|
|||
import lila.api.Context
|
||||
import lila.app.templating.Environment._
|
||||
import lila.app.ui.ScalatagsTemplate._
|
||||
import lila.ublog.UblogForm
|
||||
import lila.ublog.UblogForm.UblogPostData
|
||||
import lila.ublog.UblogPost
|
||||
import lila.user.User
|
||||
|
||||
|
@ -19,12 +19,15 @@ object post {
|
|||
) {
|
||||
main(cls := "box box-pad page ublog-post")(
|
||||
h1(cls := "ublog-post__title")(post.title),
|
||||
ctx.is(user) option div(
|
||||
a(href := routes.Ublog.edit(user.username, post.id.value))("Edit your blog post")
|
||||
),
|
||||
strong(cls := "ublog-post__intro")(post.intro),
|
||||
div(cls := "ublog-post__markup")(markup)
|
||||
)
|
||||
}
|
||||
|
||||
def form(user: User, f: Form[UblogForm.UblogPostData])(implicit ctx: Context) =
|
||||
def create(user: User, f: Form[UblogPostData])(implicit ctx: Context) =
|
||||
views.html.base.layout(
|
||||
moreCss = frag(cssTag("ublog")),
|
||||
title = s"${user.username} blog • New post"
|
||||
|
@ -35,7 +38,18 @@ object post {
|
|||
)
|
||||
}
|
||||
|
||||
private def innerForm(user: User, form: Form[UblogForm.UblogPostData], post: Option[UblogPost])(implicit
|
||||
def edit(user: User, post: UblogPost, f: Form[UblogPostData])(implicit ctx: Context) =
|
||||
views.html.base.layout(
|
||||
moreCss = frag(cssTag("ublog")),
|
||||
title = s"${user.username} blog • ${post.title}"
|
||||
) {
|
||||
main(cls := "box box-pad page ublog-post-form")(
|
||||
h1("Edit your blog post"),
|
||||
innerForm(user, f, post.some)
|
||||
)
|
||||
}
|
||||
|
||||
private def innerForm(user: User, form: Form[UblogPostData], post: Option[UblogPost])(implicit
|
||||
ctx: Context
|
||||
) =
|
||||
postForm(
|
||||
|
|
|
@ -61,6 +61,7 @@ GET /@/:username/tournaments/:path controllers.UserTournament.path(username:
|
|||
GET /@/:username/blog controllers.Ublog.index(username: String, page: Int ?= 1)
|
||||
GET /@/:username/blog/new controllers.Ublog.form(username: String)
|
||||
POST /@/:username/blog/new controllers.Ublog.create(username: String)
|
||||
GET /@/:username/blog/$id<\w{8}>/edit controllers.Ublog.edit(username: String, id: String)
|
||||
POST /@/:username/blog/$id<\w{8}>/edit controllers.Ublog.update(username: String, id: String)
|
||||
GET /@/:username/blog/:slug/:id controllers.Ublog.post(username: String, slug: String, id: String)
|
||||
|
||||
|
|
|
@ -8,13 +8,16 @@ final class UblogForm {
|
|||
|
||||
import UblogForm._
|
||||
|
||||
val post = Form(
|
||||
val create = Form(
|
||||
mapping(
|
||||
"title" -> cleanNonEmptyText(minLength = 3, maxLength = 100),
|
||||
"intro" -> cleanNonEmptyText(minLength = 0, maxLength = 2_000),
|
||||
"markdown" -> cleanNonEmptyText(minLength = 0, maxLength = 100_000)
|
||||
)(UblogPostData.apply)(UblogPostData.unapply)
|
||||
)
|
||||
|
||||
def edit(post: UblogPost) =
|
||||
create.fill(UblogPostData(title = post.title, intro = post.intro, markdown = post.markdown))
|
||||
}
|
||||
|
||||
object UblogForm {
|
||||
|
|
|
@ -22,6 +22,8 @@ case class UblogPost(
|
|||
val s = lila.common.String slugify title
|
||||
if (s.isEmpty) "-" else s
|
||||
}
|
||||
|
||||
def isBy(u: User) = user == u.id
|
||||
}
|
||||
|
||||
object UblogPost {
|
||||
|
|
Loading…
Reference in New Issue