<commit_before>package vep.app.production.company.show

import scalikejdbc._
import vep.app.production.company.Company
import vep.app.production.company.show.play.PlayService
import vep.framework.database.DatabaseContainer
import vep.framework.validation.{Valid, Validation}

class ShowService(
  playService: PlayService
) extends DatabaseContainer {
  def findByCompany(company: Company): Seq[Show] = withQueryConnection { implicit session =>
    findShowsByCompany(company)
  }

  private def findShowsByCompany(company: Company)(implicit session: DBSession): Seq[Show] = {
    sql"""
      SELECT * FROM show
      WHERE company = ${company.id}
    """
      .map(Show.apply)
      .list()
      .apply()
  }

  def findFromCompany(company: Company, id: String): Option[Show] = withQueryConnection { implicit session =>
    findShowFromCompany(company, id)
  }

  private def findShowFromCompany(company: Company, id: String)(implicit session: DBSession): Option[Show] = {
    sql"""
      SELECT * FROM show
      WHERE id = ${id}
      AND   company = ${company.id}
    """
      .map(Show.apply)
      .single()
      .apply()
  }

  def findNext(): Seq[ShowMeta] = withQueryConnection { implicit session =>
    findNextShows()
  }

  private def findNextShows()(implicit session: DBSession): Seq[ShowMeta] = {
    sql"""
      SELECT s.* FROM show s
      JOIN play p ON p.show = s.id
      WHERE p.date > CURRENT_TIMESTAMP
      AND NOT EXISTS (
        SELECT 1 FROM play p2
        WHERE p2.show = p.show
        AND p2.date > p.date
      )
      ORDER BY p.date ASC
    """
      .map(ShowMeta.apply)
      .list()
      .apply()
  }

  def findAllWithDependencies(): Seq[ShowWithDependencies] = withQueryConnection { implicit session =>
    findAllShows().flatMap { show =>
      findCompanyByShow(show).map { company =>
        ShowWithDependencies(
          show = show,
          company = company,
          plays = playService.findAllFromShow(show)
        )
      }
    }
  }

  private def findAllShows()(implicit session: DBSession): Seq[Show] = {
    sql"""
      SELECT * FROM show
    """
      .map(Show.apply)
      .list()
      .apply()
  }

  private def findCompanyByShow(show: Show)(implicit session: DBSession): Option[Company] = {
    sql"""
      SELECT * FROM company c
      WHERE EXISTS (
        SELECT 1 FROM show s
        WHERE s.company = c.id
      )
    """
      .map(Company.apply)<commit_msg>Correct internal server error on Show page<commit_after>package vep.app.production.company.show

import scalikejdbc._
import vep.app.production.company.Company
import vep.app.production.company.show.play.PlayService
import vep.framework.database.DatabaseContainer
import vep.framework.validation.{Valid, Validation}

class ShowService(
  playService: PlayService
) extends DatabaseContainer {
  def findByCompany(company: Company): Seq[Show] = withQueryConnection { implicit session =>
    findShowsByCompany(company)
  }

  private def findShowsByCompany(company: Company)(implicit session: DBSession): Seq[Show] = {
    sql"""
      SELECT * FROM show
      WHERE company = ${company.id}
    """
      .map(Show.apply)
      .list()
      .apply()
  }

  def findFromCompany(company: Company, id: String): Option[Show] = withQueryConnection { implicit session =>
    findShowFromCompany(company, id)
  }

  private def findShowFromCompany(company: Company, id: String)(implicit session: DBSession): Option[Show] = {
    sql"""
      SELECT * FROM show
      WHERE id = ${id}
      AND   company = ${company.id}
    """
      .map(Show.apply)
      .single()
      .apply()
  }

  def findNext(): Seq[ShowMeta] = withQueryConnection { implicit session =>
    findNextShows()
  }

  private def findNextShows()(implicit session: DBSession): Seq[ShowMeta] = {
    sql"""
      SELECT s.* FROM show s
      JOIN play p ON p.show = s.id
      WHERE p.date > CURRENT_TIMESTAMP
      AND NOT EXISTS (
        SELECT 1 FROM play p2
        WHERE p2.show = p.show
        AND p2.date > p.date
      )
      ORDER BY p.date ASC
    """
      .map(ShowMeta.apply)
      .list()
      .apply()
  }

  def findAllWithDependencies(): Seq[ShowWithDependencies] = withQueryConnection { implicit session =>
    findAllShows().flatMap { show =>
      findCompanyByShow(show).map { company =>
        ShowWithDependencies(
          show = show,
          company = company,
          plays = playService.findAllFromShow(show)
        )
      }
    }
  }

  private def findAllShows()(implicit session: DBSession): Seq[Show] = {
    sql"""
      SELECT * FROM show
    """
      .map(Show.apply)
      .list()
      .apply()
  }

  private def findCompanyByShow(show: Show)(implicit session: DBSession): Option[Company] = {
    sql"""
      SELECT * FROM company c
      WHERE EXISTS (
        SELECT 1 FROM show s
        WHERE s.company = c.id
        AND s.id = ${show.id}
      )
    """
      .map(Company.apply)
{'fraction_non_alphanumeric': 0.11374215745800445, 'fraction_numerical': 0.0020238818053025702, 'mean_word_length': 2.6311535635562087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '7391370', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1483, 'n_words': 366}