philomena/lib/philomena/poll_votes.ex

173 lines
3.9 KiB
Elixir
Raw Normal View History

2019-10-01 02:50:34 +02:00
defmodule Philomena.PollVotes do
@moduledoc """
The PollVotes context.
"""
import Ecto.Query, warn: false
2019-12-20 04:41:19 +01:00
alias Ecto.Multi
2019-10-01 02:50:34 +02:00
alias Philomena.Repo
alias Philomena.Polls
2019-12-20 04:41:19 +01:00
alias Philomena.Polls.Poll
2019-10-01 02:50:34 +02:00
alias Philomena.PollVotes.PollVote
2019-12-20 04:41:19 +01:00
alias Philomena.PollOptions.PollOption
2019-10-01 02:50:34 +02:00
@doc """
Gets a single poll_vote.
Raises `Ecto.NoResultsError` if the Poll vote does not exist.
## Examples
iex> get_poll_vote!(123)
%PollVote{}
iex> get_poll_vote!(456)
** (Ecto.NoResultsError)
"""
def get_poll_vote!(id), do: Repo.get!(PollVote, id)
@doc """
Creates a poll_vote.
## Examples
iex> create_poll_vote(%{field: value})
{:ok, %PollVote{}}
iex> create_poll_vote(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
2019-12-20 04:41:19 +01:00
def create_poll_votes(user, poll, attrs) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
poll_votes = filter_options(user, poll, now, attrs)
Multi.new()
Squashed commit of the following: commit 8ea9cff4af46e24c38020652cedeff72957354fb Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:29:24 2020 -0400 remove remaining serializable aside hiding related commit 99ccf06264db6319ece2a896a104031447447a5f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:20:40 2020 -0400 interactions: remove serializable commit a63bef06a6962368f69cf83afbc3c44f2467618c Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:16:27 2020 -0400 users: remove serializable commit 8053229f6fab507c29a40f0e22dd9cab7971e34f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:11:14 2020 -0400 user_links: remove serializable commit 9b058add825b0a876a91a1cf2b1b22dc34066e42 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:09:33 2020 -0400 topics: remove serializable commit cd9ea908c34f72c0120fca1c4d581540db60db98 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:05:23 2020 -0400 tags: remove serializable commit c7563fef8fc905c32a0727a4b104222227a6bafa Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:02:22 2020 -0400 static_pages: remove serializable commit 3da661bdd1aec74e4ac5b69ec21124bc1ebc6fb4 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:00:15 2020 -0400 posts: remove serializable commit 18a50a4e5bed1ab6e4e6c13c3051a21ae7e8fbb0 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:55:55 2020 -0400 poll_votes: remove serializable commit 7d946ef23d7b27877d4bf0fb6a4db4ae64a9ffab Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:51:49 2020 -0400 galleries: remove serializable commit d8c35a0934e5394b092b050e071abdada4bdb640 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:42:43 2020 -0400 conversations: remove serializable commit 079e6dca6c8064867f2c0f90f351ea83c0f12b75 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:38:28 2020 -0400 comments: remove serializable commit 00ae38bad566fb6badeccceac2e394e65ec9428e Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:37:15 2020 -0400 commissions: remove serializable commit b3c4a4b13671ca73c58080b090dd6165552c87d6 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:17:12 2020 -0400 bans: remove serializable commit 8be9fe913ff1f6264b899e96ee38fa52032b8bda Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:02:44 2020 -0400 badges: remove serializable commit 162adda185f705b9749774c4af8c7d8db0d89790 Author: byte[] <byteslice@airmail.cc> Date: Sat Sep 5 23:56:51 2020 -0400 adverts: remove serializable
2020-09-06 07:30:53 +02:00
|> Multi.run(:lock, fn repo, _ ->
poll =
Poll
|> where(id: ^poll.id)
|> lock("FOR UPDATE")
|> repo.one()
{:ok, poll}
end)
|> Multi.run(:ended, fn _repo, _changes ->
# Bail if poll is no longer active
case Polls.active?(poll) do
false -> {:error, []}
_true -> {:ok, []}
end
end)
2019-12-20 04:41:19 +01:00
|> Multi.run(:existing_votes, fn _repo, _changes ->
# Don't proceed if any votes exist
case voted?(poll, user) do
2020-01-11 05:20:19 +01:00
true -> {:error, []}
2019-12-20 04:41:19 +01:00
_false -> {:ok, []}
end
end)
|> Multi.run(:new_votes, fn repo, _changes ->
2020-01-11 05:20:19 +01:00
{_count, votes} = repo.insert_all(PollVote, poll_votes, returning: true)
2019-12-20 04:41:19 +01:00
{:ok, votes}
end)
|> Multi.run(:update_option_counts, fn repo, %{new_votes: new_votes} ->
option_ids = Enum.map(new_votes, & &1.poll_option_id)
{count, nil} =
PollOption
|> where([po], po.id in ^option_ids)
|> repo.update_all(inc: [vote_count: 1])
{:ok, count}
end)
|> Multi.run(:update_poll_votes_count, fn repo, %{new_votes: new_votes} ->
length = length(new_votes)
{count, nil} =
Poll
|> where(id: ^poll.id)
|> repo.update_all(inc: [total_votes: length])
{:ok, count}
end)
Squashed commit of the following: commit 8ea9cff4af46e24c38020652cedeff72957354fb Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:29:24 2020 -0400 remove remaining serializable aside hiding related commit 99ccf06264db6319ece2a896a104031447447a5f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:20:40 2020 -0400 interactions: remove serializable commit a63bef06a6962368f69cf83afbc3c44f2467618c Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:16:27 2020 -0400 users: remove serializable commit 8053229f6fab507c29a40f0e22dd9cab7971e34f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:11:14 2020 -0400 user_links: remove serializable commit 9b058add825b0a876a91a1cf2b1b22dc34066e42 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:09:33 2020 -0400 topics: remove serializable commit cd9ea908c34f72c0120fca1c4d581540db60db98 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:05:23 2020 -0400 tags: remove serializable commit c7563fef8fc905c32a0727a4b104222227a6bafa Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:02:22 2020 -0400 static_pages: remove serializable commit 3da661bdd1aec74e4ac5b69ec21124bc1ebc6fb4 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:00:15 2020 -0400 posts: remove serializable commit 18a50a4e5bed1ab6e4e6c13c3051a21ae7e8fbb0 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:55:55 2020 -0400 poll_votes: remove serializable commit 7d946ef23d7b27877d4bf0fb6a4db4ae64a9ffab Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:51:49 2020 -0400 galleries: remove serializable commit d8c35a0934e5394b092b050e071abdada4bdb640 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:42:43 2020 -0400 conversations: remove serializable commit 079e6dca6c8064867f2c0f90f351ea83c0f12b75 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:38:28 2020 -0400 comments: remove serializable commit 00ae38bad566fb6badeccceac2e394e65ec9428e Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:37:15 2020 -0400 commissions: remove serializable commit b3c4a4b13671ca73c58080b090dd6165552c87d6 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:17:12 2020 -0400 bans: remove serializable commit 8be9fe913ff1f6264b899e96ee38fa52032b8bda Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:02:44 2020 -0400 badges: remove serializable commit 162adda185f705b9749774c4af8c7d8db0d89790 Author: byte[] <byteslice@airmail.cc> Date: Sat Sep 5 23:56:51 2020 -0400 adverts: remove serializable
2020-09-06 07:30:53 +02:00
|> Repo.transaction()
2019-12-20 04:41:19 +01:00
end
defp filter_options(user, poll, now, %{"option_ids" => options}) when is_list(options) do
# TODO: enforce integrity at the constraint level
votes =
options
|> Enum.map(&String.to_integer/1)
|> Enum.uniq()
|> Enum.map(&%{poll_option_id: &1, user_id: user.id, created_at: now})
case poll.vote_method do
"single" -> Enum.take(votes, 1)
2020-01-11 05:20:19 +01:00
_other -> votes
2019-12-20 04:41:19 +01:00
end
end
2020-01-11 05:20:19 +01:00
2019-12-20 04:41:19 +01:00
defp filter_options(_user, _poll, _now, _attrs), do: []
def voted?(nil, _user), do: false
def voted?(_poll, nil), do: false
2020-01-11 05:20:19 +01:00
2019-12-20 04:41:19 +01:00
def voted?(%{id: poll_id}, %{id: user_id}) do
PollVote
|> join(:inner, [pv], _ in assoc(pv, :poll_option))
|> where([pv, po], po.poll_id == ^poll_id and pv.user_id == ^user_id)
|> Repo.exists?()
2019-10-01 02:50:34 +02:00
end
@doc """
Updates a poll_vote.
## Examples
iex> update_poll_vote(poll_vote, %{field: new_value})
{:ok, %PollVote{}}
iex> update_poll_vote(poll_vote, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_poll_vote(%PollVote{} = poll_vote, attrs) do
poll_vote
|> PollVote.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a PollVote.
## Examples
iex> delete_poll_vote(poll_vote)
{:ok, %PollVote{}}
iex> delete_poll_vote(poll_vote)
{:error, %Ecto.Changeset{}}
"""
def delete_poll_vote(%PollVote{} = poll_vote) do
Repo.delete(poll_vote)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking poll_vote changes.
## Examples
iex> change_poll_vote(poll_vote)
%Ecto.Changeset{source: %PollVote{}}
"""
def change_poll_vote(%PollVote{} = poll_vote) do
PollVote.changeset(poll_vote, %{})
end
end