<commit_before>package com.softwaremill.bootzooka.dao.user

import java.util.UUID

import com.softwaremill.bootzooka.dao.sql.SqlDatabase
import com.softwaremill.bootzooka.domain.User

import scala.concurrent.{ExecutionContext, Future}
import com.softwaremill.bootzooka.common.FutureHelpers._

class UserDao(protected val database: SqlDatabase)(implicit val ec: ExecutionContext) extends SqlUserSchema {

  import database._
  import database.driver.api._

  type UserId = UUID

  def add(user: User): Future[Unit] = {
    val action = (for {
      userByLoginOpt <- findByLowerCasedLoginAction(user.login)
      userByEmailOpt <- findByEmailAction(user.email)
      _ <- addOrThrowAction(userByLoginOpt, userByEmailOpt, user)
    } yield ()).transactionally

    db.run(action)
  }

  private def addOrThrowAction(userByLoginOpt: Option[User], userByEmailOpt: Option[User], user: User) = {
    if (userByLoginOpt.isDefined || userByEmailOpt.isDefined)
      DBIO.failed(new IllegalArgumentException("User with given e-mail or login already exists"))
    else
      users += user
  }

  def loadAll() = db.run(users.result)

  def remove(userId: UserId): Future[Unit] = {
    db.run(users.filter(_.id === userId).delete).mapToUnit
  }

  def load(userId: UserId): Future[Option[User]] =
    findOneWhere(_.id === userId)

  private def findOneWhereAction(condition: Users => Rep[Boolean]) = {
    users.filter(condition).result.headOption
  }<commit_msg>[minor] Rename function - The exception will make the Future[] fail, but technically it's not "throwing"<commit_after>package com.softwaremill.bootzooka.dao.user

import java.util.UUID

import com.softwaremill.bootzooka.dao.sql.SqlDatabase
import com.softwaremill.bootzooka.domain.User

import scala.concurrent.{ExecutionContext, Future}
import com.softwaremill.bootzooka.common.FutureHelpers._

class UserDao(protected val database: SqlDatabase)(implicit val ec: ExecutionContext) extends SqlUserSchema {

  import database._
  import database.driver.api._

  type UserId = UUID

  def add(user: User): Future[Unit] = {
    val action = (for {
      userByLoginOpt <- findByLowerCasedLoginAction(user.login)
      userByEmailOpt <- findByEmailAction(user.email)
      _ <- addOrFailOnExistingAction(userByLoginOpt, userByEmailOpt, user)
    } yield ()).transactionally

    db.run(action)
  }

  private def addOrFailOnExistingAction(userByLoginOpt: Option[User], userByEmailOpt: Option[User], user: User) = {
    if (userByLoginOpt.isDefined || userByEmailOpt.isDefined)
      DBIO.failed(new IllegalArgumentException("User with given e-mail or login already exists"))
    else
      users += user
  }

  def loadAll() = db.run(users.result)

  def remove(userId: UserId): Future[Unit] = {
    db.run(users.filter(_.id === userId).delete).mapToUnit
  }

  def load(userId: UserId): Future[Option[User]] =
    findOneWhere(_.id === userId)

  private def findOneWhereAction(condition: Users => Rep[Boolean]) = {
    users.filter(condition).result.headOption
  }
{'fraction_non_alphanumeric': 0.11177446102819237, 'fraction_numerical': 0.0, 'mean_word_length': 4.473684210526316, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '4557026', 'n_tokens_mistral': 955, 'n_tokens_neox': 938, 'n_words': 196}