<commit_before>package com.ubirch.user.core.manager

import java.util.UUID

import com.typesafe.scalalogging.slf4j.StrictLogging

import com.ubirch.user.config.Config
import com.ubirch.user.model.db.Group
import com.ubirch.util.mongo.connection.MongoUtil
import com.ubirch.util.uuid.UUIDUtil

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/**
  * author: cvandrei
  * since: 2017-03-30
  */
object GroupManager extends StrictLogging {

  private val collection = Config.mongoCollectionGroup

  def create(group: Group)(implicit mongo: MongoUtil): Future[Option[Group]] = {

    // TODO implement
    Future(Some(group))

  }

  def update(group: Group)(implicit mongo: MongoUtil): Future[Option[Group]] = {

    // TODO implement
    Future(Some(group))

  }

  def findById(id: UUID)(implicit mongo: MongoUtil): Future[Option[Group]] = {

    // TODO implement
    Future(
      Some(
        Group(
          displayName = "displayName-find",
          ownerId = UUIDUtil.uuid,
          contextId = UUIDUtil.uuid,
          allowedUsers = Seq.empty
        )
      )
    )

  }

  def delete(id: UUID)(implicit mongo: MongoUtil): Future[Boolean] = {

    // TODO implement
    Future(
      true
    )

  }

  def addAllowedUsers(groupId: UUID, allowedUsers: Seq[UUID]): Future[Boolean] = {

    // TODO implement
    Future(true)

  }

  def deleteAllowedUsers(groupId: UUID, allowedUsers: Seq[UUID]): Future[Boolean] = {

    // TODO implement
    Future(true)

  }

}<commit_msg>UBI-77: implemented CRUD methods<commit_after>package com.ubirch.user.core.manager

import java.util.UUID

import com.typesafe.scalalogging.slf4j.StrictLogging

import com.ubirch.user.config.Config
import com.ubirch.user.model.db.Group
import com.ubirch.util.mongo.connection.MongoUtil
import com.ubirch.util.mongo.format.MongoFormats

import reactivemongo.bson.{BSONDocumentReader, BSONDocumentWriter, Macros, document}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.{Failure, Success}

/**
  * author: cvandrei
  * since: 2017-03-30
  */
object GroupManager extends StrictLogging
  with MongoFormats {

  private val collection = Config.mongoCollectionGroup

  implicit protected def groupWriter: BSONDocumentWriter[Group] = Macros.writer[Group]

  implicit protected def groupReader: BSONDocumentReader[Group] = Macros.reader[Group]

  def create(group: Group)(implicit mongo: MongoUtil): Future[Option[Group]] = {

    // TODO automated tests
    mongo.collection(collection) map { collection =>

      collection.insert[Group](group) onComplete {

        case Failure(e) =>
          logger.error("failed to create group", e)
          throw e

        case Success(_) => logger.debug(s"created new group: $group")

      }
      Some(group)

    }

  }

  def update(group: Group)(implicit mongo: MongoUtil): Future[Option[Group]] = {

    // TODO automated tests
    val selector = document("id" -> group.id)
    mongo.collection(collection) flatMap {

      _.update(selector, group) map { writeResult =>

        if (writeResult.ok && writeResult.n == 1) {
          logger.info(s"updated group: id=${group.id}")
          Some(group)
        } else {
          logger.error(s"failed to update group: group=$group")
          None
        }

      }

    }

  }

  def findById(id: UUID)(implicit mongo: MongoUtil): Future[Option[Group]] = {

    // TODO automated tests
    val selector = document("id" -> id)

    mongo.collection(collection) flatMap {
      _.find(selector).one[Group]
    }

  }

  def delete(id: UUID)(implicit mongo: MongoUtil): Future[Boolean] = {

    // TODO automated tests
    val selector = document("id" -> id)

    mongo.collection(collection) flatMap {
      _.remove(selector) map { writeResult =>

        if (writeResult.ok && writeResult.n == 1) {
          logger.info(s"deleted group: id=$id")
          true
        } else {
          logger.error(s"failed to delete group: id=$id (writeResult=$writeResult)")
          false
        }

      }
    }

  }

  def addAllowedUsers(groupId: UUID, allowedUsers: Seq[UUID]): Future[Boolean] = {

    // TODO implement
    Future(true)

  }

  def deleteAllowedUsers(groupId: UUID, allowedUsers: Seq[UUID]): Future[Boolean] = {

    // TODO implement
    Future(true)

  }

}
{'fraction_non_alphanumeric': 0.1179440110142267, 'fraction_numerical': 0.005048187241854061, 'mean_word_length': 3.0286506469500925, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3109729', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1351, 'n_words': 282}