lila/app/controllers/Clas.scala

110 lines
3.2 KiB
Scala
Raw Normal View History

2020-01-16 07:40:33 -07:00
package controllers
import play.api.mvc._
import lila.app._
import views._
final class Clas(
env: Env
) extends LilaController(env) {
def index = Secure(_.Teacher) { implicit ctx => me =>
WithTeacher(me) { t =>
2020-01-16 10:52:20 -07:00
env.clas.api.clas.of(t.teacher) map { classes =>
Ok(views.html.clas.clas.index(classes, t))
}
}
}
def form = Secure(_.Teacher) { implicit ctx => _ =>
2020-01-16 13:25:41 -07:00
Ok(html.clas.clas.create(env.clas.forms.create)).fuccess
2020-01-16 10:52:20 -07:00
}
def create = SecureBody(_.Teacher) { implicit ctx => me =>
WithTeacher(me) { t =>
env.clas.forms.create
.bindFromRequest()(ctx.body)
.fold(
2020-01-16 13:25:41 -07:00
err => BadRequest(html.clas.clas.create(err)).fuccess,
2020-01-16 10:52:20 -07:00
setup =>
env.clas.api.clas.create(setup, t.teacher) map { clas =>
Redirect(routes.Clas.show(clas.id.value))
}
)
}
}
def show(id: String) = Secure(_.Teacher) { implicit ctx => me =>
2020-01-16 12:01:11 -07:00
WithClass(me, lila.clas.Clas.Id(id)) { t => clas =>
env.clas.api.student.of(clas) map { students =>
views.html.clas.clas.show(clas, t, students)
2020-01-16 10:52:20 -07:00
}
2020-01-16 07:40:33 -07:00
}
}
2020-01-16 12:01:11 -07:00
def edit(id: String) = Secure(_.Teacher) { implicit ctx => me =>
WithClass(me, lila.clas.Clas.Id(id)) { _ => clas =>
2020-01-16 13:25:41 -07:00
Ok(html.clas.clas.edit(clas, env.clas.forms.edit(clas))).fuccess
2020-01-16 12:01:11 -07:00
}
}
def update(id: String) = SecureBody(_.Teacher) { implicit ctx => me =>
WithClass(me, lila.clas.Clas.Id(id)) { _ => clas =>
env.clas.forms
.edit(clas)
.bindFromRequest()(ctx.body)
.fold(
2020-01-16 13:25:41 -07:00
err => BadRequest(html.clas.clas.edit(clas, err)).fuccess,
2020-01-16 12:01:11 -07:00
setup =>
env.clas.api.clas.update(clas, setup) map { clas =>
Redirect(routes.Clas.show(clas.id.value))
}
)
}
}
2020-01-16 13:25:41 -07:00
def studentForm(id: String) = Secure(_.Teacher) { implicit ctx => me =>
WithClass(me, lila.clas.Clas.Id(id)) { _ => clas =>
Ok(html.clas.student.form(clas, env.clas.forms.student.create)).fuccess
}
}
def studentCreate(id: String) = SecureBody(_.Teacher) { implicit ctx => me =>
NoTor {
Firewall {
WithClass(me, lila.clas.Clas.Id(id)) { _ => clas =>
env.clas.forms.student.create
.bindFromRequest()(ctx.body)
.fold(
err => BadRequest(html.clas.student.form(clas, err)).fuccess,
username =>
env.clas.api.student.create(clas, username)(env.user.authenticator.passEnc) flatMap {
case (user, password) =>
env.clas.api.student.get(clas, user) map {
_ ?? { student =>
Ok(html.clas.student.show(clas, student, password.some))
}
}
}
)
}
}
}
}
2020-01-16 07:40:33 -07:00
private def WithTeacher(me: lila.user.User)(
f: lila.clas.Teacher.WithUser => Fu[Result]
): Fu[Result] =
2020-01-16 10:52:20 -07:00
env.clas.api.teacher withOrCreate me flatMap f
2020-01-16 12:01:11 -07:00
private def WithClass(me: lila.user.User, clasId: lila.clas.Clas.Id)(
f: lila.clas.Teacher.WithUser => lila.clas.Clas => Fu[Result]
): Fu[Result] =
WithTeacher(me) { t =>
env.clas.api.clas.getAndView(clasId, t.teacher) flatMap {
_ ?? f(t)
}
}
2020-01-16 07:40:33 -07:00
}