<commit_before>package controllers

import play.api.libs.json._
import play.api.mvc._
import scala.concurrent.duration._

import lila.api.Context
import lila.app._
import lila.chat.Chat
import lila.common.{ IpAddress, HTTPRequest }
import lila.common.paginator.{ Paginator, PaginatorJson }
import lila.study.JsonView.JsData
import lila.study.Study.WithChapter
import lila.study.{ Chapter, Order, Study => StudyModel }
import lila.tree.Node.partitionTreeJsonWriter
import views._

object Study extends LilaController {

  type ListUrl = String => Call

  private def env = Env.study

  def search(text: String, page: Int) = OpenBody { implicit ctx =>
    Reasonable(page) {
      if (text.trim.isEmpty)
        env.pager.all(ctx.me, Order.default, page) map { pag =>
          Ok(html.study.all(pag, Order.default))
        }
      else Env.studySearch(ctx.me)(text, page) map { pag =>
        Ok(html.study.search(pag, text))
      }
    }
  }

  def allDefault(page: Int) = all(Order.Hot.key, page)

  def all(o: String, page: Int) = Open { implicit ctx =>
    Reasonable(page) {
      Order(o) match {
        case Order.Oldest => Redirect(routes.Study.allDefault(page)).fuccess
        case order =>
          env.pager.all(ctx.me, order, page) flatMap { pag =>
            negotiate(
              html = Ok(html.study.all(pag, order)).fuccess,
              api = _ => apiStudies(pag)
            )
          }
      }
    }<commit_msg>Negotiate search endpoint too<commit_after>package controllers

import play.api.libs.json._
import play.api.mvc._
import scala.concurrent.duration._

import lila.api.Context
import lila.app._
import lila.chat.Chat
import lila.common.{ IpAddress, HTTPRequest }
import lila.common.paginator.{ Paginator, PaginatorJson }
import lila.study.JsonView.JsData
import lila.study.Study.WithChapter
import lila.study.{ Chapter, Order, Study => StudyModel }
import lila.tree.Node.partitionTreeJsonWriter
import views._

object Study extends LilaController {

  type ListUrl = String => Call

  private def env = Env.study

  def search(text: String, page: Int) = OpenBody { implicit ctx =>
    Reasonable(page) {
      if (text.trim.isEmpty)
        env.pager.all(ctx.me, Order.default, page) flatMap { pag =>
          negotiate(
            html = Ok(html.study.all(pag, Order.default)).fuccess,
            api = _ => apiStudies(pag)
          )
        }
      else Env.studySearch(ctx.me)(text, page) flatMap { pag =>
        negotiate(
          html = Ok(html.study.search(pag, text)).fuccess,
          api = _ => apiStudies(pag)
        )
      }
    }
  }

  def allDefault(page: Int) = all(Order.Hot.key, page)

  def all(o: String, page: Int) = Open { implicit ctx =>
    Reasonable(page) {
      Order(o) match {
        case Order.Oldest => Redirect(routes.Study.allDefault(page)).fuccess
        case order =>
          env.pager.all(ctx.me, order, page) flatMap { pag =>
            negotiate(
              html = Ok(html.study.all(pag, order)).fuccess,
              api = _ => apiStudies(pag)
            )
          }
      }
    }
{'fraction_non_alphanumeric': 0.12077922077922078, 'fraction_numerical': 0.0, 'mean_word_length': 2.5659722222222223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '212118', 'n_tokens_mistral': 1014, 'n_tokens_neox': 966, 'n_words': 221}