<commit_before>package models.daos

import play.api.mvc.Controller

import com.mohiva.play.silhouette.api.LoginInfo
import models.User

import scala.concurrent.Future

// Reactive Mongo imports
import reactivemongo.api._

// Reactive Mongo plugin, including the JSON-specialized collection
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection.JSONCollection
import play.api.libs.json._

import scala.concurrent.ExecutionContext.Implicits.global

import java.util.UUID
import play.api.Logger


class UserDAOMongoImpl extends UserDAO {
  def find(loginInfo: LoginInfo) = UserDAOMongoImpl.find(loginInfo)
  def find(userID: UUID) = UserDAOMongoImpl.find(userID)
  def save(user: User) = UserDAOMongoImpl.save(user)
}

/**
 * The companion object.
 */
object UserDAOMongoImpl extends Controller with MongoController {

  /**
   * The list of users.
   */
  def users: JSONCollection = db.collection[JSONCollection]("users")
  
  /**
   * Finds a user by its login info.
   *
   * @param loginInfo The login info of the user to find.
   * @return The found user or None if no user for the given login info could be found.
   */
  def find(loginInfo: LoginInfo) = {
    Logger.debug("Looking for: "+loginInfo.providerKey)
    val cursor: Cursor[User] = users.
      // find all people with loginInfo `loginInfo`
      find(Json.obj(
            "loginInfo" -> loginInfo
          )).
      // perform the query and get a cursor of JsObject
      cursor[User]

    // gather all the JsObjects in a list
    val futureUsersList: Future[List[User]] = cursor.collect[List]()

    // everything's ok! Let's reply with the user
    
    futureUsersList.map { persons =>
      if(persons.size > 0)
        Some(persons(0))
      else
        None
    }
  }

  /**
   * Finds a user by its user ID.
   *
   * @param userID The ID of the user to find.
   * @return The found user or None if no user for the given ID could be found.
   */
  def find(userID: UUID) = {
    Logger.debug("Looking for: "+userID)
    val cursor: Cursor[User] = users.
      // find all people with name `name`
      find(Json.obj("userID" -> userID)).
      // sort them by creation date
      // perform the query and get a cursor of JsObject
      cursor[User]

    // gather all the JsObjects in a list
    val futureUsersList: Future[List[User]] = cursor.collect[List]()

    // everything's ok! Let's reply with the array
    futureUsersList.map { persons =>
     if(persons.size > 0)
        Some(persons(0))
      else
        None
    }
  }

  /**
   * Saves a user.
   *
   * @param user The user to save.
   * @return The saved user.
   */
  def save(user: User) = {
    Logger.debug("On veut sauvegarder: "+user.fullName)
    val result = users.insert(user)
    Logger.debug("On arrive jusque là?")
    result.map(_ => user)
  }
}<commit_msg>Fix issue duplicating an user each time one's logs in<commit_after>package models.daos

import play.api.mvc.Controller

import com.mohiva.play.silhouette.api.LoginInfo
import models.User

import scala.concurrent.Future

// Reactive Mongo imports
import reactivemongo.api._

// Reactive Mongo plugin, including the JSON-specialized collection
import play.modules.reactivemongo.MongoController
import play.modules.reactivemongo.json.collection.JSONCollection
import play.api.libs.json._

import scala.concurrent.ExecutionContext.Implicits.global

import java.util.UUID
import play.api.Logger


class UserDAOMongoImpl extends UserDAO {
  def find(loginInfo: LoginInfo) = UserDAOMongoImpl.find(loginInfo)
  def find(userID: UUID) = UserDAOMongoImpl.find(userID)
  def save(user: User) = UserDAOMongoImpl.save(user)
}

/**
 * The companion object.
 */
object UserDAOMongoImpl extends Controller with MongoController {

  /**
   * The list of users.
   */
  def users: JSONCollection = db.collection[JSONCollection]("users")
  
  /**
   * Finds a user by its login info.
   *
   * @param loginInfo The login info of the user to find.
   * @return The found user or None if no user for the given login info could be found.
   */
  def find(loginInfo: LoginInfo) = {
    Logger.debug("Looking for: "+loginInfo.providerKey)
    val cursor: Cursor[User] = users.
      // find all people with loginInfo `loginInfo`
      find(Json.obj(
            "loginInfo" -> loginInfo
          )).
      // perform the query and get a cursor of JsObject
      cursor[User]

    // gather all the JsObjects in a list
    val futureUsersList: Future[List[User]] = cursor.collect[List]()

    // everything's ok! Let's reply with the user
    
    futureUsersList.map { persons =>
      if(persons.size > 0)
        Some(persons(0))
      else
        None
    }
  }

  /**
   * Finds a user by its user ID.
   *
   * @param userID The ID of the user to find.
   * @return The found user or None if no user for the given ID could be found.
   */
  def find(userID: UUID) = {
    Logger.debug("Looking for: "+userID)
    val cursor: Cursor[User] = users.
      // find all people with name `name`
      find(Json.obj("userID" -> userID)).
      // sort them by creation date
      // perform the query and get a cursor of JsObject
      cursor[User]

    // gather all the JsObjects in a list
    val futureUsersList: Future[List[User]] = cursor.collect[List]()

    // everything's ok! Let's reply with the array
    futureUsersList.map { persons =>
     if(persons.size > 0)
        Some(persons(0))
      else
        None
    }
  }

  /**
   * Saves a user.
   *
   * @param user The user to save.
   * @return The saved user.
   */
  def save(user: User) = {
    find(user.userID).flatMap {
      case Some(userSaved) =>
        Logger.debug("On veut update: "+user.fullName)
        users.update(Json.obj("userID" -> user.userID), user)
      case None =>
        Logger.debug("On veut sauvegarder: "+user.fullName)
        users.insert(user)
    }
    Future.successful(user)
  }
}
{'fraction_non_alphanumeric': 0.10496118798515018, 'fraction_numerical': 0.0013499831252109348, 'mean_word_length': 2.7631746031746034, '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': '1543888', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1832, 'n_words': 562}