<commit_before>package ca.friendlyguacamole.server.providers.quill

import ca.friendlyguacamole.server.models.{PollModel, PollOptionModel}
import ca.friendlyguacamole.server.models.db.{Poll, PollOption, PollOptionVote}
import ca.friendlyguacamole.server.providers.PollsProvider

import scala.concurrent.ExecutionContext.Implicits.global
import scalaz.concurrent.Task
import delorean._
import ca.friendlyguacamole.server.Quill
import io.getquill.{PostgresAsyncContext, SnakeCase}

/**
  * Created by panagiotis on 11/06/17.
  */
object QuillPostgresPollsProvider extends PollsProvider {
  private val MaxHomePolls = 6
  import Quill.ctx._

  override def getPolls(userId: Option[Int]): Task[Seq[PollModel]] = {
    val q = quote {
      query[Poll] map(_.id) take lift(MaxHomePolls)
    }

    for {
      pollIds <- run(q).toTask
      polls <- Task.gatherUnordered(pollIds map (pid => findPoll(pid, userId)))
    } yield polls flatMap (_.toList)
  }

  override def findPoll(id: Int, userId: Option[Int]): Task[Option[PollModel]] = {
    val qPollWithOptions = quote {
      for {
        poll <- query[Poll] filter (_.id == lift(id))
        pollOption <- query[PollOption] if poll.id == pollOption.pollId
      } yield (poll, pollOption)
    }

    val qPollOptionVoteCount = quote {
      query[PollOptionVote] filter (_.pollId == lift(id)) groupBy(_.pollOptionId) map {
        case (optId, votes) => (optId, votes.size)
      }
    }

    for {
      results <- run(qPollWithOptions).toTask
      votes <- run(qPollOptionVoteCount).toTask map (_.toMap mapValues (_.toInt))
      resultsUnzipped = results.unzip
    } yield for {
      poll <- resultsUnzipped._1.headOption
      optionModels = resultsUnzipped._2.map(po => PollOptionModel(po, votes.getOrElse(po.id, 0), None))
    } yield PollModel.fromOptionModels(poll, optionModels)<commit_msg>Added selected flag to poll if user is authorized<commit_after>package ca.friendlyguacamole.server.providers.quill

import ca.friendlyguacamole.server.models.{PollModel, PollOptionModel}
import ca.friendlyguacamole.server.models.db.{Poll, PollOption, PollOptionVote}
import ca.friendlyguacamole.server.providers.PollsProvider

import scala.concurrent.ExecutionContext.Implicits.global
import scalaz.concurrent.Task
import delorean._
import ca.friendlyguacamole.server.Quill
import io.getquill.{PostgresAsyncContext, SnakeCase}

/**
  * Created by panagiotis on 11/06/17.
  */
object QuillPostgresPollsProvider extends PollsProvider {
  private val MaxHomePolls = 6
  import Quill.ctx._

  override def getPolls(userId: Option[Int]): Task[Seq[PollModel]] = {
    val q = quote {
      query[Poll] map(_.id) take lift(MaxHomePolls)
    }

    for {
      pollIds <- run(q).toTask
      polls <- Task.gatherUnordered(pollIds map (pid => findPoll(pid, userId)))
    } yield polls flatMap (_.toList)
  }

  override def findPoll(id: Int, userId: Option[Int]): Task[Option[PollModel]] = {
    val qPollWithOptions = quote {
      for {
        poll <- query[Poll] filter (_.id == lift(id))
        pollOption <- query[PollOption] if poll.id == pollOption.pollId
      } yield (poll, pollOption)
    }

    val qPollOptionVoteCount = quote {
      query[PollOptionVote] filter (_.pollId == lift(id)) groupBy(_.pollOptionId) map {
        case (optId, votes) => (optId, votes.size)
      }
    }

    val tPollOptionVoteSelected = userId match {
      case Some(uid) =>
        run(query[PollOptionVote]
          .filter(v => v.pollId == lift(id) && v.guacUserId == lift(uid))
          .map(_.pollOptionId)).toTask
      case None => Task.delay(List())
    }

    for {
      results <- run(qPollWithOptions).toTask
      votes <- run(qPollOptionVoteCount).toTask map (_.toMap mapValues (_.toInt))
      selected <- tPollOptionVoteSelected
      resultsUnzipped = results.unzip
    } yield for {
      poll <- resultsUnzipped._1.headOption
      optionModels = resultsUnzipped._2.map(po => PollOptionModel(po, votes.getOrElse(po.id, 0), userId map (_ => selected.contains(po.id))))
    } yield PollModel.fromOptionModels(poll, optionModels)
{'fraction_non_alphanumeric': 0.11646684609738195, 'fraction_numerical': 0.004893564962074872, 'mean_word_length': 3.5727069351230427, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 13, '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': '2568311', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1354, 'n_words': 275}