lila/app/controllers/Clas.scala

218 lines
6.3 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))
2020-01-16 10:52:20 -07:00
}
}
}
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))
}
)
}
}
2020-01-16 19:45:18 -07:00
def show(id: String) = Auth { implicit ctx => me =>
if (isGranted(_.Teacher))
2020-01-17 13:05:42 -07:00
WithClass(me, id) { _ => clas =>
env.clas.api.student.allOf(clas) map { students =>
views.html.clas.clas.showToTeacher(clas, students)
2020-01-16 19:45:18 -07:00
}
} else
env.clas.api.clas.byId(lila.clas.Clas.Id(id)) flatMap {
_ ?? { clas =>
2020-01-17 13:05:42 -07:00
env.clas.api.student.activeOf(clas) flatMap { students =>
2020-01-16 19:45:18 -07:00
if (students.exists(_.student is me))
Ok(views.html.clas.clas.showToStudent(clas, students)).fuccess
else notFound
}
}
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 =>
2020-01-17 13:05:42 -07:00
WithClass(me, 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 =>
2020-01-17 13:05:42 -07:00
WithClass(me, id) { _ => clas =>
2020-01-16 12:01:11 -07:00
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 =>
2020-01-17 13:05:42 -07:00
WithClass(me, id) { _ => clas =>
2020-01-16 16:41:46 -07:00
Ok(
html.clas.student.form(
clas,
env.clas.forms.student.invite,
env.clas.forms.student.create
)
).fuccess
2020-01-16 13:25:41 -07:00
}
}
def studentCreate(id: String) = SecureBody(_.Teacher) { implicit ctx => me =>
NoTor {
Firewall {
2020-01-17 13:05:42 -07:00
WithClass(me, id) { t => clas =>
2020-01-16 13:25:41 -07:00
env.clas.forms.student.create
.bindFromRequest()(ctx.body)
.fold(
2020-01-16 16:41:46 -07:00
err =>
BadRequest(
html.clas.student.form(
clas,
env.clas.forms.student.invite,
err
)
).fuccess,
2020-01-16 13:25:41 -07:00
username =>
2020-01-17 13:05:42 -07:00
env.clas.api.student.create(clas, username, t.teacher) map {
2020-01-16 13:25:41 -07:00
case (user, password) =>
2020-01-16 16:41:46 -07:00
Redirect(routes.Clas.studentShow(clas.id.value, user.username))
.flashing("password" -> password.value)
2020-01-16 13:25:41 -07:00
}
)
}
}
}
}
2020-01-16 16:41:46 -07:00
def studentInvite(id: String) = SecureBody(_.Teacher) { implicit ctx => me =>
2020-01-17 13:05:42 -07:00
WithClass(me, id) { t => clas =>
2020-01-16 16:41:46 -07:00
env.clas.forms.student.invite
.bindFromRequest()(ctx.body)
.fold(
err =>
BadRequest(
html.clas.student.form(
clas,
err,
env.clas.forms.student.create
)
).fuccess,
username =>
env.user.repo named username flatMap {
_ ?? { user =>
2020-01-16 19:45:18 -07:00
env.clas.api.student.invite(clas, user, t) inject
Redirect(routes.Clas.studentForm(clas.id.value)).flashSuccess
2020-01-16 16:41:46 -07:00
}
}
)
}
}
2020-01-16 19:45:18 -07:00
def studentJoin(id: String, token: String) = Auth { _ => me =>
env.clas.api.invite.redeem(lila.clas.Clas.Id(id), me, token) map {
_ ?? { _ =>
Redirect(routes.Clas.show(id))
}
}
}
2020-01-16 14:59:58 -07:00
def studentShow(id: String, username: String) = Secure(_.Teacher) { implicit ctx => me =>
2020-01-17 13:05:42 -07:00
WithClass(me, id) { _ => clas =>
2020-01-16 14:59:58 -07:00
env.user.repo named username flatMap {
_ ?? { user =>
2020-01-17 13:05:42 -07:00
env.clas.api.student.get(clas, user) flatMap {
2020-01-16 14:59:58 -07:00
_ ?? { student =>
2020-01-17 13:05:42 -07:00
env.activity.read.recent(student.user, 14) map { activity =>
views.html.clas.student.show(clas, student, activity)
}
2020-01-16 14:59:58 -07:00
}
}
}
}
}
}
2020-01-17 13:05:42 -07:00
def studentArchive(id: String, username: String, v: Boolean) = Secure(_.Teacher) { _ => me =>
WithClass(me, id) { t => clas =>
WithStudent(clas, username) { s =>
env.clas.api.student.archive(s.student, t.teacher, v) inject
Redirect(routes.Clas.studentShow(clas.id.value, username)).flashSuccess
}
}
}
def studentSetKid(id: String, username: String, v: Boolean) = Secure(_.Teacher) { _ => me =>
WithClass(me, id) { _ => clas =>
WithStudent(clas, username) { s =>
(s.student.managed ?? env.user.repo.setKid(s.user, v)) inject
Redirect(routes.Clas.studentShow(clas.id.value, username)).flashSuccess
}
}
}
def studentResetPassword(id: String, username: String) = Secure(_.Teacher) { _ => me =>
WithClass(me, id) { _ => clas =>
WithStudent(clas, username) { s =>
env.clas.api.student.resetPassword(s.student) map { password =>
Redirect(routes.Clas.studentShow(clas.id.value, username))
.flashing("password" -> password.value)
}
}
}
}
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
2020-01-17 13:05:42 -07:00
private def WithClass(me: lila.user.User, clasId: String)(
2020-01-16 12:01:11 -07:00
f: lila.clas.Teacher.WithUser => lila.clas.Clas => Fu[Result]
): Fu[Result] =
WithTeacher(me) { t =>
2020-01-17 13:05:42 -07:00
env.clas.api.clas.getAndView(lila.clas.Clas.Id(clasId), t.teacher) flatMap {
2020-01-16 12:01:11 -07:00
_ ?? f(t)
}
}
2020-01-17 13:05:42 -07:00
private def WithStudent(clas: lila.clas.Clas, username: String)(
f: lila.clas.Student.WithUser => Fu[Result]
): Fu[Result] =
env.user.repo named username flatMap {
_ ?? { user =>
env.clas.api.student.get(clas, user) flatMap {
_ ?? f
}
}
}
2020-01-16 07:40:33 -07:00
}