mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-30 14:57:59 +01:00
context: polls, poll_votes
This commit is contained in:
parent
a9885190e3
commit
1d011c98e0
4 changed files with 347 additions and 0 deletions
|
@ -389,4 +389,196 @@ defmodule Philomena.Forums do
|
|||
def change_subscription(%Subscription{} = subscription) do
|
||||
Subscription.changeset(subscription, %{})
|
||||
end
|
||||
|
||||
alias Philomena.Forums.Polls
|
||||
|
||||
@doc """
|
||||
Returns the list of polls.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_polls()
|
||||
[%Polls{}, ...]
|
||||
|
||||
"""
|
||||
def list_polls do
|
||||
Repo.all(Polls)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single polls.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Polls does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_polls!(123)
|
||||
%Polls{}
|
||||
|
||||
iex> get_polls!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_polls!(id), do: Repo.get!(Polls, id)
|
||||
|
||||
@doc """
|
||||
Creates a polls.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_polls(%{field: value})
|
||||
{:ok, %Polls{}}
|
||||
|
||||
iex> create_polls(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_polls(attrs \\ %{}) do
|
||||
%Polls{}
|
||||
|> Polls.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a polls.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_polls(polls, %{field: new_value})
|
||||
{:ok, %Polls{}}
|
||||
|
||||
iex> update_polls(polls, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_polls(%Polls{} = polls, attrs) do
|
||||
polls
|
||||
|> Polls.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a Polls.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_polls(polls)
|
||||
{:ok, %Polls{}}
|
||||
|
||||
iex> delete_polls(polls)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_polls(%Polls{} = polls) do
|
||||
Repo.delete(polls)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking polls changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_polls(polls)
|
||||
%Ecto.Changeset{source: %Polls{}}
|
||||
|
||||
"""
|
||||
def change_polls(%Polls{} = polls) do
|
||||
Polls.changeset(polls, %{})
|
||||
end
|
||||
|
||||
alias Philomena.Forums.PollVote
|
||||
|
||||
@doc """
|
||||
Returns the list of poll_votes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_poll_votes()
|
||||
[%PollVote{}, ...]
|
||||
|
||||
"""
|
||||
def list_poll_votes do
|
||||
Repo.all(PollVote)
|
||||
end
|
||||
|
||||
@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{}}
|
||||
|
||||
"""
|
||||
def create_poll_vote(attrs \\ %{}) do
|
||||
%PollVote{}
|
||||
|> PollVote.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
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
|
||||
|
|
16
lib/philomena/forums/poll_vote.ex
Normal file
16
lib/philomena/forums/poll_vote.ex
Normal file
|
@ -0,0 +1,16 @@
|
|||
defmodule Philomena.Forums.PollVote do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "poll_votes" do
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(poll_vote, attrs) do
|
||||
poll_vote
|
||||
|> cast(attrs, [])
|
||||
|> validate_required([])
|
||||
end
|
||||
end
|
25
lib/philomena/forums/polls.ex
Normal file
25
lib/philomena/forums/polls.ex
Normal file
|
@ -0,0 +1,25 @@
|
|||
defmodule Philomena.Forums.Polls do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "polls" do
|
||||
belongs_to :topic, Philomena.Forums.Topic
|
||||
belongs_to :deleted_by, Philomena.Users.User
|
||||
|
||||
field :title, :string
|
||||
field :vote_method, :string
|
||||
field :active_until, :naive_datetime
|
||||
field :total_votes, :integer, default: 0
|
||||
field :hidden_from_users, :boolean, default: false
|
||||
field :deletion_reason, :string, default: ""
|
||||
|
||||
timestamps(inserted_at: :created_at)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(polls, attrs) do
|
||||
polls
|
||||
|> cast(attrs, [])
|
||||
|> validate_required([])
|
||||
end
|
||||
end
|
|
@ -230,4 +230,118 @@ defmodule Philomena.ForumsTest do
|
|||
assert %Ecto.Changeset{} = Forums.change_subscription(subscription)
|
||||
end
|
||||
end
|
||||
|
||||
describe "polls" do
|
||||
alias Philomena.Forums.Polls
|
||||
|
||||
@valid_attrs %{}
|
||||
@update_attrs %{}
|
||||
@invalid_attrs %{}
|
||||
|
||||
def polls_fixture(attrs \\ %{}) do
|
||||
{:ok, polls} =
|
||||
attrs
|
||||
|> Enum.into(@valid_attrs)
|
||||
|> Forums.create_polls()
|
||||
|
||||
polls
|
||||
end
|
||||
|
||||
test "list_polls/0 returns all polls" do
|
||||
polls = polls_fixture()
|
||||
assert Forums.list_polls() == [polls]
|
||||
end
|
||||
|
||||
test "get_polls!/1 returns the polls with given id" do
|
||||
polls = polls_fixture()
|
||||
assert Forums.get_polls!(polls.id) == polls
|
||||
end
|
||||
|
||||
test "create_polls/1 with valid data creates a polls" do
|
||||
assert {:ok, %Polls{} = polls} = Forums.create_polls(@valid_attrs)
|
||||
end
|
||||
|
||||
test "create_polls/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Forums.create_polls(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_polls/2 with valid data updates the polls" do
|
||||
polls = polls_fixture()
|
||||
assert {:ok, %Polls{} = polls} = Forums.update_polls(polls, @update_attrs)
|
||||
end
|
||||
|
||||
test "update_polls/2 with invalid data returns error changeset" do
|
||||
polls = polls_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Forums.update_polls(polls, @invalid_attrs)
|
||||
assert polls == Forums.get_polls!(polls.id)
|
||||
end
|
||||
|
||||
test "delete_polls/1 deletes the polls" do
|
||||
polls = polls_fixture()
|
||||
assert {:ok, %Polls{}} = Forums.delete_polls(polls)
|
||||
assert_raise Ecto.NoResultsError, fn -> Forums.get_polls!(polls.id) end
|
||||
end
|
||||
|
||||
test "change_polls/1 returns a polls changeset" do
|
||||
polls = polls_fixture()
|
||||
assert %Ecto.Changeset{} = Forums.change_polls(polls)
|
||||
end
|
||||
end
|
||||
|
||||
describe "poll_votes" do
|
||||
alias Philomena.Forums.PollVote
|
||||
|
||||
@valid_attrs %{}
|
||||
@update_attrs %{}
|
||||
@invalid_attrs %{}
|
||||
|
||||
def poll_vote_fixture(attrs \\ %{}) do
|
||||
{:ok, poll_vote} =
|
||||
attrs
|
||||
|> Enum.into(@valid_attrs)
|
||||
|> Forums.create_poll_vote()
|
||||
|
||||
poll_vote
|
||||
end
|
||||
|
||||
test "list_poll_votes/0 returns all poll_votes" do
|
||||
poll_vote = poll_vote_fixture()
|
||||
assert Forums.list_poll_votes() == [poll_vote]
|
||||
end
|
||||
|
||||
test "get_poll_vote!/1 returns the poll_vote with given id" do
|
||||
poll_vote = poll_vote_fixture()
|
||||
assert Forums.get_poll_vote!(poll_vote.id) == poll_vote
|
||||
end
|
||||
|
||||
test "create_poll_vote/1 with valid data creates a poll_vote" do
|
||||
assert {:ok, %PollVote{} = poll_vote} = Forums.create_poll_vote(@valid_attrs)
|
||||
end
|
||||
|
||||
test "create_poll_vote/1 with invalid data returns error changeset" do
|
||||
assert {:error, %Ecto.Changeset{}} = Forums.create_poll_vote(@invalid_attrs)
|
||||
end
|
||||
|
||||
test "update_poll_vote/2 with valid data updates the poll_vote" do
|
||||
poll_vote = poll_vote_fixture()
|
||||
assert {:ok, %PollVote{} = poll_vote} = Forums.update_poll_vote(poll_vote, @update_attrs)
|
||||
end
|
||||
|
||||
test "update_poll_vote/2 with invalid data returns error changeset" do
|
||||
poll_vote = poll_vote_fixture()
|
||||
assert {:error, %Ecto.Changeset{}} = Forums.update_poll_vote(poll_vote, @invalid_attrs)
|
||||
assert poll_vote == Forums.get_poll_vote!(poll_vote.id)
|
||||
end
|
||||
|
||||
test "delete_poll_vote/1 deletes the poll_vote" do
|
||||
poll_vote = poll_vote_fixture()
|
||||
assert {:ok, %PollVote{}} = Forums.delete_poll_vote(poll_vote)
|
||||
assert_raise Ecto.NoResultsError, fn -> Forums.get_poll_vote!(poll_vote.id) end
|
||||
end
|
||||
|
||||
test "change_poll_vote/1 returns a poll_vote changeset" do
|
||||
poll_vote = poll_vote_fixture()
|
||||
assert %Ecto.Changeset{} = Forums.change_poll_vote(poll_vote)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue