<commit_before>package controllers

import java.util.UUID

import dao._
import models.Role._
import models.User.UserProtocol
import models._
import play.api.libs.json.{JsValue, Json, Reads, Writes}
import services._
import store.{TableFilter, UserTable}
import utils.LwmMimeType

import scala.concurrent.Future
import scala.util.{Failure, Try}

object UserControllerPostgres {
  lazy val statusAttribute = "status"
  lazy val degreeAttribute = "degree"
  lazy val systemIdAttribute = "systemId"
  lazy val firstnameAttribute = "firstname"
  lazy val lastnameAttribute = "lastname"
}

final class UserControllerPostgres(val authorityDao: AuthorityDao, val sessionService: SessionHandlingService, val ldapService: LdapService, val abstractDao: UserDao)
  extends AbstractCRUDControllerPostgres[UserProtocol, UserTable, DbUser, User] {

  import scala.concurrent.ExecutionContext.Implicits.global

  override implicit val mimeType = LwmMimeType.userV1Json

  override def create(secureContext: SecureContext) = secureContext asyncContentTypedAction { request =>
    import models.User.writes

    (for {
      userProtocol <- Future.fromTry(parse[UserProtocol](request))
      ldapUser <- ldapService.user2(userProtocol.systemId)
      userWithAuth <- abstractDao.createOrUpdate(ldapUser)
    } yield userWithAuth).jsonResult { userWithAuth =>
      val (user, maybeAuth) = userWithAuth
      val userJson = Json.toJson(user)

      maybeAuth.fold {
        Ok(userJson)
      } { auth =>
        Created(Json.obj(
          "user" -> userJson,
          "authority" -> Json.toJson(auth)
        ))
      }
    }
  }

  def buddy(labwork: String, student: String, buddy: String) = contextFrom(Get) asyncAction { _ =>
    abstractDao.buddyResult(student, buddy, labwork).jsonResult { buddyResult =>
      buddyResult match {
        case Allowed(b) => Ok(Json.obj(
          "status" -> "OK",
          "type" -> buddyResult.toString,
          "buddy" -> Json.toJson(b),
          "message" -> s"Dein Partner ${b.systemId} hat Dich ebenfalls referenziert."
        ))
        case Almost(b) => Ok(Json.obj(
          "status" -> "OK",
          "type" -> buddyResult.toString,
          "buddy" -> Json.toJson(b),
          "message" -> s"Dein Partner ${b.systemId} muss Dich ebenfalls referenzieren, ansonsten wird dieser Partnerwunsch nicht berücksichtigt."
        ))
        case Denied(b) => BadRequest(Json.obj(
          "status" -> "KO",
          "type" -> buddyResult.toString,
          "buddy" -> Json.toJson(b),
          "message" -> s"Dein Partner ${b.systemId} und Du sind nicht im selben Studiengang."
        ))
        case NotExisting(b) => BadRequest(Json.obj(
          "status" -> "KO",
          "type" -> buddyResult.toString,
          "buddy" -> None,
          "message" -> s"Dein Partner $b existiert nicht oder hat sich noch nicht im Praktikumstool angemeldet."
        ))
      }
    }
  }

  override protected def contextFrom: PartialFunction[Rule, SecureContext] = {
    case Get => PartialSecureBlock(List(Student, Employee, CourseAssistant))
    case GetAll => PartialSecureBlock(List(Student, Employee, CourseAssistant))
    case Create => PartialSecureBlock(List(Admin))
    case _ => PartialSecureBlock(List(God))
  }

  override protected implicit val writes: Writes[User] = User.writes

  override protected implicit val reads: Reads[UserProtocol] = User.reads
<commit_msg>changed http return from BadRequest to Ok for buddy requests (#60)<commit_after>package controllers

import java.util.UUID

import dao._
import models.Role._
import models.User.UserProtocol
import models._
import play.api.libs.json.{Json, Reads, Writes}
import services._
import store.{TableFilter, UserTable}
import utils.LwmMimeType

import scala.concurrent.Future
import scala.util.{Failure, Try}

object UserControllerPostgres {
  lazy val statusAttribute = "status"
  lazy val degreeAttribute = "degree"
  lazy val systemIdAttribute = "systemId"
  lazy val firstnameAttribute = "firstname"
  lazy val lastnameAttribute = "lastname"
}

final class UserControllerPostgres(val authorityDao: AuthorityDao, val sessionService: SessionHandlingService, val ldapService: LdapService, val abstractDao: UserDao)
  extends AbstractCRUDControllerPostgres[UserProtocol, UserTable, DbUser, User] {

  import scala.concurrent.ExecutionContext.Implicits.global

  override implicit val mimeType = LwmMimeType.userV1Json

  override def create(secureContext: SecureContext) = secureContext asyncContentTypedAction { request =>
    import models.User.writes

    (for {
      userProtocol <- Future.fromTry(parse[UserProtocol](request))
      ldapUser <- ldapService.user2(userProtocol.systemId)
      userWithAuth <- abstractDao.createOrUpdate(ldapUser)
    } yield userWithAuth).jsonResult { userWithAuth =>
      val (user, maybeAuth) = userWithAuth
      val userJson = Json.toJson(user)

      maybeAuth.fold {
        Ok(userJson)
      } { auth =>
        Created(Json.obj(
          "user" -> userJson,
          "authority" -> Json.toJson(auth)
        ))
      }
    }
  }

  def buddy(labwork: String, student: String, buddy: String) = contextFrom(Get) asyncAction { _ =>
    abstractDao.buddyResult(student, buddy, labwork).jsonResult { buddyResult =>
      buddyResult match {
        case Allowed(b) => Ok(Json.obj(
          "status" -> "OK",
          "type" -> buddyResult.toString,
          "buddy" -> Json.toJson(b),
          "message" -> s"Dein Partner ${b.systemId} hat Dich ebenfalls referenziert."
        ))
        case Almost(b) => Ok(Json.obj(
          "status" -> "OK",
          "type" -> buddyResult.toString,
          "buddy" -> Json.toJson(b),
          "message" -> s"Dein Partner ${b.systemId} muss Dich ebenfalls referenzieren, ansonsten wird dieser Partnerwunsch nicht berücksichtigt."
        ))
        case Denied(b) => Ok(Json.obj(
          "status" -> "KO",
          "type" -> buddyResult.toString,
          "buddy" -> Json.toJson(b),
          "message" -> s"Dein Partner ${b.systemId} und Du sind nicht im selben Studiengang."
        ))
        case NotExisting(b) => Ok(Json.obj(
          "status" -> "KO",
          "type" -> buddyResult.toString,
          "buddy" -> None,
          "message" -> s"Dein Partner $b existiert nicht oder hat sich noch nicht im Praktikumstool angemeldet."
        ))
      }
    }
  }

  override protected def contextFrom: PartialFunction[Rule, SecureContext] = {
    case Get => PartialSecureBlock(List(Student, Employee, CourseAssistant))
    case GetAll => PartialSecureBlock(List(Student, Employee, CourseAssistant))
    case Create => PartialSecureBlock(List(Admin))
    case _ => PartialSecureBlock(List(God))
  }

  override protected implicit val writes: Writes[User] = User.writes

  override protected implicit val reads: Reads[UserProtocol] = User.reads

{'fraction_non_alphanumeric': 0.10948905109489052, 'fraction_numerical': 0.0008759124087591241, 'mean_word_length': 3.1979166666666665, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3423738', 'n_tokens_mistral': 2039, 'n_tokens_neox': 2033, 'n_words': 497}