<commit_before>package com.lateralthoughts.points.controllers

import java.time.OffsetDateTime
import java.util.UUID

import com.lateralthoughts.points.model.inputs.{InnerRewardingActionCategoryInput, UpdateRewardingActionInput, RewardingActionInput, NewRewardingActionInput}
import com.lateralthoughts.points.model.records.{RewardingAction, RewardingActionCategory}
import com.lateralthoughts.points.repositories.{RewardingActionCategoryRepository, RewardingActionRepository}
import org.scalatra._

import scala.util.{Try, Failure, Success}

trait RewardingActionController extends HandlingJson {

  get("/actions/") {
    val now = OffsetDateTime.now()
    val rewardingActionCategory = RewardingActionCategory(UUID.randomUUID(), "myCategory", "my category description", now, now)
    List(RewardingAction(UUID.randomUUID(), "myAction", rewardingActionCategory, "my action description", 1, now, now))
  }

  post("/actions/") {
    retrievePostedJsonAnd(saveRewardingAction, "rewardingAction")(request)
  }

  put("/actions/:actionId") {
    Try {
      UUID.fromString(params("actionId"))
    } match {
      case Success(actionId) => retrievePostedJsonAnd(updateRewardingAction(actionId), "rewardingAction")(request)
      case Failure(e) => BadRequest(e.getMessage)
    }
  }

  def saveRewardingAction(input: NewRewardingActionInput) = {
    retrieveCategory(input) match {
      case Left(x) => x
      case Right(rewardingActionCategory) => save(input.generate(rewardingActionCategory), Created.apply)
    }
  }

  def updateRewardingAction(actionId: UUID)(input: UpdateRewardingActionInput) = {
    NotImplemented("Not yet implemented")
  }

  private def save(rewarding: RewardingAction, successStatus: (Any, Map[String, String], String) => ActionResult): ActionResult = {
    RewardingActionRepository.save(rewarding) match {
      case Success(savedRewarding) => successStatus(savedRewarding, Map.empty, "")
      case Failure(exception) => InternalServerError(exception.getMessage)
    }
  }

  private def retrieveCategory(rewardingActionInput: RewardingActionInput, rewardingAction: Option[RewardingAction] = None): Either[ActionResult, RewardingActionCategory] = rewardingActionInput match {
    case NewRewardingActionInput(_, category, _, _) => retrieveCategoryFromInput(category)
    case UpdateRewardingActionInput(_, optionalCategory, _, _) => optionalCategory match {
      case None => Right(rewardingAction.map(_.category).get) // Should never be None at this level
      case Some(category) => retrieveCategoryFromInput(category)
    }
  }

  private def retrieveCategoryFromInput(category: InnerRewardingActionCategoryInput): Either[ActionResult, RewardingActionCategory] = {
    RewardingActionCategoryRepository.retrieve(category.id) match {
      case Some(retrievedCategory) => Right(category.update(retrievedCategory))
      case None => category.create match {
        case Left(message) => Left(BadRequest(s"unable to create category due to : $message"))
        case Right(createdCategory) => Right(createdCategory)
      }
    }
  }
}
<commit_msg>complete comment<commit_after>package com.lateralthoughts.points.controllers

import java.time.OffsetDateTime
import java.util.UUID

import com.lateralthoughts.points.model.inputs.{InnerRewardingActionCategoryInput, UpdateRewardingActionInput, RewardingActionInput, NewRewardingActionInput}
import com.lateralthoughts.points.model.records.{RewardingAction, RewardingActionCategory}
import com.lateralthoughts.points.repositories.{RewardingActionCategoryRepository, RewardingActionRepository}
import org.scalatra._

import scala.util.{Try, Failure, Success}

trait RewardingActionController extends HandlingJson {

  get("/actions/") {
    val now = OffsetDateTime.now()
    val rewardingActionCategory = RewardingActionCategory(UUID.randomUUID(), "myCategory", "my category description", now, now)
    List(RewardingAction(UUID.randomUUID(), "myAction", rewardingActionCategory, "my action description", 1, now, now))
  }

  post("/actions/") {
    retrievePostedJsonAnd(saveRewardingAction, "rewardingAction")(request)
  }

  put("/actions/:actionId") {
    Try {
      UUID.fromString(params("actionId"))
    } match {
      case Success(actionId) => retrievePostedJsonAnd(updateRewardingAction(actionId), "rewardingAction")(request)
      case Failure(e) => BadRequest(e.getMessage)
    }
  }

  def saveRewardingAction(input: NewRewardingActionInput) = {
    retrieveCategory(input) match {
      case Left(x) => x
      case Right(rewardingActionCategory) => save(input.generate(rewardingActionCategory), Created.apply)
    }
  }

  def updateRewardingAction(actionId: UUID)(input: UpdateRewardingActionInput) = {
    NotImplemented("Not yet implemented")
  }

  private def save(rewarding: RewardingAction, successStatus: (Any, Map[String, String], String) => ActionResult): ActionResult = {
    RewardingActionRepository.save(rewarding) match {
      case Success(savedRewarding) => successStatus(savedRewarding, Map.empty, "")
      case Failure(exception) => InternalServerError(exception.getMessage)
    }
  }

  private def retrieveCategory(rewardingActionInput: RewardingActionInput, rewardingAction: Option[RewardingAction] = None): Either[ActionResult, RewardingActionCategory] = rewardingActionInput match {
    case NewRewardingActionInput(_, category, _, _) => retrieveCategoryFromInput(category)
    case UpdateRewardingActionInput(_, optionalCategory, _, _) => optionalCategory match {
      case None => Right(rewardingAction.map(_.category).get) // rewardingAction.category should never be None at this level
      case Some(category) => retrieveCategoryFromInput(category)
    }
  }

  private def retrieveCategoryFromInput(category: InnerRewardingActionCategoryInput): Either[ActionResult, RewardingActionCategory] = {
    RewardingActionCategoryRepository.retrieve(category.id) match {
      case Some(retrievedCategory) => Right(category.update(retrievedCategory))
      case None => category.create match {
        case Left(message) => Left(BadRequest(s"unable to create category due to : $message"))
        case Right(createdCategory) => Right(createdCategory)
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.10352067183462532, 'fraction_numerical': 0.00032299741602067185, 'mean_word_length': 5.505252100840337, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1379192', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1687, 'n_words': 326}