<commit_before>package controllers

import java.util.UUID
import javax.inject.Inject

import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import forms.OrganisationForm
import models.daos.OrganisationDAO
import models.{Organisation, User}
import play.api.i18n.{Messages, MessagesApi}
import play.api.libs.json.Json

import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent.Future

class Organisations @Inject()(organisationDAO: OrganisationDAO,
                              val messagesApi: MessagesApi,
                              val env: Environment[User, SessionAuthenticator])
  extends Silhouette[User, SessionAuthenticator] {

  implicit val organisationWrites = Json.writes[Organisation]

  def organisations = SecuredAction.async { implicit request =>
    organisationDAO.all.map(organisations =>
      Ok(views.html.organisations(organisations.sortBy(_.name), request.identity)))
  }

  def organisation(id: String) = SecuredAction.async { implicit request =>
    try {
      val uuid = UUID.fromString(id)
      for {
        groups <- organisationDAO.groups(uuid)
        organisation <- organisationDAO.get(uuid)
      } yield {
        organisation match {
          case Some(organisation) => Ok(views.html.organisation(organisation, groups.sortBy(_.name), request.identity))
          case None => NotFound(views.html.notFound(id, Some(request.identity)))
        }
      }
    } catch {
      case _: IllegalArgumentException =>
        Future(BadRequest(views.html.badRequest(Messages("uuid.invalid"), Some(request.identity))))
    }
  }

  def add = SecuredAction.async { implicit request =>
    Future(Ok(views.html.organisationAdd(OrganisationForm.form, request.identity)))
  }

  def save = SecuredAction.async { implicit request =>
    OrganisationForm.form.bindFromRequest.fold(
      form => Future.successful(BadRequest(views.html.organisationAdd(form, request.identity))),
      data => {
        val organisation = Organisation(UUID.randomUUID, data.name)
        for {
          org <- organisationDAO.save(organisation)
          groups <- organisationDAO.groups(organisation.id)
        } yield {
          Ok(views.html.organisation(organisation, groups, request.identity))
        }
      }
    )
  }

  def all = SecuredAction.async {
    organisationDAO.all.map(organisations => Ok(Json.toJson(organisations)))
  }

  def get(id: String) = SecuredAction.async {
    try {
      val uuid = UUID.fromString(id)
      organisationDAO.get(uuid).map {
        case Some(organisation) => Ok(Json.toJson(organisation))
        case None => NotFound(Json.toJson(Messages("organisation.not_found")))
      }
    } catch {
      case _: IllegalArgumentException => Future(BadRequest(Json.toJson(Messages("uuid.invalid"))))
    }
  }
}<commit_msg>Retreive groups as JSON for an organisation<commit_after>package controllers

import java.util.UUID
import javax.inject.Inject

import com.mohiva.play.silhouette.api.{Environment, Silhouette}
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import forms.OrganisationForm
import models.daos.OrganisationDAO
import models._
import play.api.i18n.{Messages, MessagesApi}
import play.api.libs.json.Json

import scala.concurrent.ExecutionContext.Implicits._
import scala.concurrent.Future

class Organisations @Inject()(organisationDAO: OrganisationDAO,
                              val messagesApi: MessagesApi,
                              val env: Environment[User, SessionAuthenticator])
  extends Silhouette[User, SessionAuthenticator] {

  implicit val organisationWrites = Json.writes[Organisation]

  def organisations = SecuredAction.async { implicit request =>
    organisationDAO.all.map(organisations =>
      Ok(views.html.organisations(organisations.sortBy(_.name), request.identity)))
  }

  def organisation(id: String) = SecuredAction.async { implicit request =>
    try {
      val uuid = UUID.fromString(id)
      for {
        groups <- organisationDAO.groups(uuid)
        organisation <- organisationDAO.get(uuid)
      } yield {
        organisation match {
          case Some(organisation) => Ok(views.html.organisation(organisation, groups.sortBy(_.name), request.identity))
          case None => NotFound(views.html.notFound(id, Some(request.identity)))
        }
      }
    } catch {
      case _: IllegalArgumentException =>
        Future(BadRequest(views.html.badRequest(Messages("uuid.invalid"), Some(request.identity))))
    }
  }

  def add = SecuredAction.async { implicit request =>
    Future(Ok(views.html.organisationAdd(OrganisationForm.form, request.identity)))
  }

  def save = SecuredAction.async { implicit request =>
    OrganisationForm.form.bindFromRequest.fold(
      form => Future.successful(BadRequest(views.html.organisationAdd(form, request.identity))),
      data => {
        val organisation = Organisation(UUID.randomUUID, data.name)
        for {
          org <- organisationDAO.save(organisation)
          groups <- organisationDAO.groups(organisation.id)
        } yield {
          Ok(views.html.organisation(organisation, groups, request.identity))
        }
      }
    )
  }

  def all = SecuredAction.async {
    organisationDAO.all.map(organisations => Ok(Json.toJson(organisations)))
  }

  def get(id: String) = SecuredAction.async {
    try {
      val uuid = UUID.fromString(id)
      organisationDAO.get(uuid).map {
        case Some(organisation) => Ok(Json.toJson(organisation))
        case None => NotFound(Json.toJson(Messages("organisation.not_found")))
      }
    } catch {
      case _: IllegalArgumentException => Future(BadRequest(Json.toJson(Messages("uuid.invalid"))))
    }
  }

  def groups(id: String) = UserAwareAction.async {
    try {
      val uuid = UUID.fromString(id)
      organisationDAO.groups(uuid).map(groups => Ok(Json.toJson(groupsMapped(groups))))
    } catch {
      case _: IllegalArgumentException => Future(BadRequest(Json.toJson(Messages("uuid.invalid"))))
    }
  }
}
{'fraction_non_alphanumeric': 0.112134166392634, 'fraction_numerical': 0.0006576783952647156, 'mean_word_length': 3.3731128684399714, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3610721', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1752, 'n_words': 297}