diff --git a/.gitignore b/.gitignore index f62cb5125fe65c55e7123f52285e8409f235928b..b81560905c60fb0c36e01a59b55cbc0304f9c08f 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,4 @@ diskuy-*.tar # this depending on your deployment strategy. /priv/static/ +.DS_Store diff --git a/config/dev.exs b/config/dev.exs index b56ce179f9c58dacdfea5c04b8b79a74004322ff..fccccc122b116a4451d15cffa40d366f23376dce 100755 --- a/config/dev.exs +++ b/config/dev.exs @@ -2,12 +2,13 @@ use Mix.Config # Configure your database config :diskuy, Diskuy.Repo, - username: "rafif", - password: "rafif123", - database: "diskuy_dev_new", + username: "diskuy", + password: "diskuy", + database: "diskuy_dev", hostname: "localhost", show_sensitive_data_on_connection_error: true, - pool_size: 10 + pool_size: 10, + log: false # For development, we disable any cache and enable # debugging and code reloading. diff --git a/lib/diskuy/survey.ex b/lib/diskuy/survey.ex new file mode 100644 index 0000000000000000000000000000000000000000..7f32cad428c727239bdec23a8d5f1612765f7dad --- /dev/null +++ b/lib/diskuy/survey.ex @@ -0,0 +1,411 @@ +defmodule Diskuy.Survey do + @moduledoc """ + The Survey context. + """ + + import Ecto.Query, warn: false + alias Diskuy.Repo + + alias Diskuy.Survey.Poll + + @doc """ + Returns the list of polls. + + ## Examples + + iex> list_polls() + [%Poll{}, ...] + + """ + def list_polls do + Repo.all(Poll) + end + + @doc """ + Gets a single poll. + + Raises `Ecto.NoResultsError` if the Poll does not exist. + + ## Examples + + iex> get_poll!(123) + %Poll{} + + iex> get_poll!(456) + ** (Ecto.NoResultsError) + + """ + def get_poll!(id), do: Repo.get!(Poll, id) + + @doc """ + Creates a poll. + + ## Examples + + iex> create_poll(%{field: value}) + {:ok, %Poll{}} + + iex> create_poll(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_poll(attrs \\ %{}) do + %Poll{} + |> Poll.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a poll. + + ## Examples + + iex> update_poll(poll, %{field: new_value}) + {:ok, %Poll{}} + + iex> update_poll(poll, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_poll(%Poll{} = poll, attrs) do + poll + |> Poll.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a poll. + + ## Examples + + iex> delete_poll(poll) + {:ok, %Poll{}} + + iex> delete_poll(poll) + {:error, %Ecto.Changeset{}} + + """ + def delete_poll(%Poll{} = poll) do + Repo.delete(poll) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking poll changes. + + ## Examples + + iex> change_poll(poll) + %Ecto.Changeset{data: %Poll{}} + + """ + def change_poll(%Poll{} = poll, attrs \\ %{}) do + Poll.changeset(poll, attrs) + end + + alias Diskuy.Survey.Comment + + @doc """ + Returns the list of survey_comments. + + ## Examples + + iex> list_survey_comments() + [%Comment{}, ...] + + """ + def list_survey_comments do + Repo.all(Comment) + end + + @doc """ + Gets a single comment. + + Raises `Ecto.NoResultsError` if the Comment does not exist. + + ## Examples + + iex> get_comment!(123) + %Comment{} + + iex> get_comment!(456) + ** (Ecto.NoResultsError) + + """ + def get_comment!(id), do: Repo.get!(Comment, id) + + @doc """ + Creates a comment. + + ## Examples + + iex> create_comment(%{field: value}) + {:ok, %Comment{}} + + iex> create_comment(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_comment(attrs \\ %{}) do + %Comment{} + |> Comment.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a comment. + + ## Examples + + iex> update_comment(comment, %{field: new_value}) + {:ok, %Comment{}} + + iex> update_comment(comment, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_comment(%Comment{} = comment, attrs) do + comment + |> Comment.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a comment. + + ## Examples + + iex> delete_comment(comment) + {:ok, %Comment{}} + + iex> delete_comment(comment) + {:error, %Ecto.Changeset{}} + + """ + def delete_comment(%Comment{} = comment) do + Repo.delete(comment) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking comment changes. + + ## Examples + + iex> change_comment(comment) + %Ecto.Changeset{data: %Comment{}} + + """ + def change_comment(%Comment{} = comment, attrs \\ %{}) do + Comment.changeset(comment, attrs) + end + + def list_poll_comments(%Poll{} = poll) do + query = from(Comment, where: [poll_id: ^poll.id]) + Repo.all(query) + end + + alias Diskuy.Survey.Choice + + @doc """ + Returns the list of choices. + + ## Examples + + iex> list_choices() + [%Choice{}, ...] + + """ + def list_choices do + Repo.all(Choice) + end + + @doc """ + Gets a single choice. + + Raises `Ecto.NoResultsError` if the Choice does not exist. + + ## Examples + + iex> get_choice!(123) + %Choice{} + + iex> get_choice!(456) + ** (Ecto.NoResultsError) + + """ + def get_choice!(id), do: Repo.get!(Choice, id) + + @doc """ + Creates a choice. + + ## Examples + + iex> create_choice(%{field: value}) + {:ok, %Choice{}} + + iex> create_choice(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_choice(attrs \\ %{}) do + %Choice{} + |> Choice.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a choice. + + ## Examples + + iex> update_choice(choice, %{field: new_value}) + {:ok, %Choice{}} + + iex> update_choice(choice, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_choice(%Choice{} = choice, attrs) do + choice + |> Choice.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a choice. + + ## Examples + + iex> delete_choice(choice) + {:ok, %Choice{}} + + iex> delete_choice(choice) + {:error, %Ecto.Changeset{}} + + """ + def delete_choice(%Choice{} = choice) do + Repo.delete(choice) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking choice changes. + + ## Examples + + iex> change_choice(choice) + %Ecto.Changeset{data: %Choice{}} + + """ + def change_choice(%Choice{} = choice, attrs \\ %{}) do + Choice.changeset(choice, attrs) + end + + def list_poll_choices(%Poll{} = poll) do + query = from(Choice, where: [poll_id: ^poll.id]) + Repo.all(query) + end + + alias Diskuy.Survey.Vote + + @doc """ + Returns the list of votes. + + ## Examples + + iex> list_votes() + [%Vote{}, ...] + + """ + def list_votes do + Repo.all(Vote) + end + + @doc """ + Gets a single vote. + + Raises `Ecto.NoResultsError` if the Vote does not exist. + + ## Examples + + iex> get_vote!(123) + %Vote{} + + iex> get_vote!(456) + ** (Ecto.NoResultsError) + + """ + def get_vote!(id), do: Repo.get!(Vote, id) + + @doc """ + Creates a vote. + + ## Examples + + iex> create_vote(%{field: value}) + {:ok, %Vote{}} + + iex> create_vote(%{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def create_vote(attrs \\ %{}) do + %Vote{} + |> Vote.changeset(attrs) + |> Repo.insert() + end + + @doc """ + Updates a vote. + + ## Examples + + iex> update_vote(vote, %{field: new_value}) + {:ok, %Vote{}} + + iex> update_vote(vote, %{field: bad_value}) + {:error, %Ecto.Changeset{}} + + """ + def update_vote(%Vote{} = vote, attrs) do + vote + |> Vote.changeset(attrs) + |> Repo.update() + end + + @doc """ + Deletes a vote. + + ## Examples + + iex> delete_vote(vote) + {:ok, %Vote{}} + + iex> delete_vote(vote) + {:error, %Ecto.Changeset{}} + + """ + def delete_vote(%Vote{} = vote) do + Repo.delete(vote) + end + + @doc """ + Returns an `%Ecto.Changeset{}` for tracking vote changes. + + ## Examples + + iex> change_vote(vote) + %Ecto.Changeset{data: %Vote{}} + + """ + def change_vote(%Vote{} = vote, attrs \\ %{}) do + Vote.changeset(vote, attrs) + end + + def get_vote_by_user_and_choice(user_id, choice_id) do + Repo.get_by(Vote, [user_id: user_id, choice_id: choice_id]) + end + + def count_choice_votes(%Choice{} = choice) do + query = from(Vote, where: [choice_id: ^choice.id]) + Repo.aggregate(query, :count, :id) + end +end diff --git a/lib/diskuy/survey/choice.ex b/lib/diskuy/survey/choice.ex new file mode 100644 index 0000000000000000000000000000000000000000..ef1a2b61ce6ee328003f3b7d78294936b7a5cc4b --- /dev/null +++ b/lib/diskuy/survey/choice.ex @@ -0,0 +1,18 @@ +defmodule Diskuy.Survey.Choice do + use Ecto.Schema + import Ecto.Changeset + + schema "choices" do + field :choice, :string + field :poll_id, :id + + timestamps() + end + + @doc false + def changeset(choice, attrs) do + choice + |> cast(attrs, [:choice, :poll_id]) + |> validate_required([:choice, :poll_id]) + end +end diff --git a/lib/diskuy/survey/comment.ex b/lib/diskuy/survey/comment.ex new file mode 100644 index 0000000000000000000000000000000000000000..1ceb906c9a5a6aca939ab23d6a11bb9b0b85c09d --- /dev/null +++ b/lib/diskuy/survey/comment.ex @@ -0,0 +1,19 @@ +defmodule Diskuy.Survey.Comment do + use Ecto.Schema + import Ecto.Changeset + + schema "survey_comments" do + field :comment, :string + field :user_id, :id + field :poll_id, :id + + timestamps() + end + + @doc false + def changeset(comment, attrs) do + comment + |> cast(attrs, [:comment, :user_id, :poll_id]) + |> validate_required([:comment, :user_id, :poll_id]) + end +end diff --git a/lib/diskuy/survey/poll.ex b/lib/diskuy/survey/poll.ex new file mode 100644 index 0000000000000000000000000000000000000000..6dbbeb4b551f248baef643eeb310805c359845a4 --- /dev/null +++ b/lib/diskuy/survey/poll.ex @@ -0,0 +1,21 @@ +defmodule Diskuy.Survey.Poll do + use Ecto.Schema + import Ecto.Changeset + + schema "polls" do + field :end_time, :utc_datetime + field :points, :integer + field :title, :string + field :topic_id, :id + field :user_id, :id + + timestamps() + end + + @doc false + def changeset(poll, attrs) do + poll + |> cast(attrs, [:points, :title, :end_time, :topic_id, :user_id]) + |> validate_required([:points, :title, :end_time, :topic_id, :user_id]) + end +end diff --git a/lib/diskuy/survey/vote.ex b/lib/diskuy/survey/vote.ex new file mode 100644 index 0000000000000000000000000000000000000000..b4a2188449afc721c27e4b29a223e115ae35c349 --- /dev/null +++ b/lib/diskuy/survey/vote.ex @@ -0,0 +1,21 @@ +defmodule Diskuy.Survey.Vote do + use Ecto.Schema + import Ecto.Changeset + + schema "votes" do + field :user_id, :id + field :choice_id, :id + field :poll_id, :id + + timestamps() + end + + @doc false + def changeset(vote, attrs) do + vote + |> cast(attrs, [:user_id,:choice_id, :poll_id]) + |> validate_required([:user_id,:choice_id, :poll_id]) + |> unique_constraint(:unique_user, name: :vote_unique_index) + |> unique_constraint(:unique_choice, name: :vote_unique_choice) + end +end diff --git a/lib/diskuy_web/controllers/choice_controller.ex b/lib/diskuy_web/controllers/choice_controller.ex new file mode 100644 index 0000000000000000000000000000000000000000..3a3255b07fe65b4543556f31a1562149f266a845 --- /dev/null +++ b/lib/diskuy_web/controllers/choice_controller.ex @@ -0,0 +1,63 @@ +defmodule DiskuyWeb.ChoiceController do + use DiskuyWeb, :controller + require Logger + + alias Diskuy.Survey + alias Diskuy.Survey.Choice + + action_fallback DiskuyWeb.FallbackController + + def index(conn, %{"id_survey" => id_survey}) do + poll = Survey.get_poll!(id_survey) + choices = Survey.list_poll_choices(poll) + render(conn, "index.json", choices: choices) + end + + def create(conn, %{"choice" => choice_params, "id_survey" => id_survey}) do + choice_params = put_poll_id(%{"choice" => choice_params, "id_survey" => id_survey}) + with {:ok, %Choice{} = choice} <- Survey.create_choice(choice_params) do + Logger.info("choice = #{inspect(choice)}") + conn + |> put_status(:created) + |> put_resp_header("location", Routes.choice_path(conn, :show, id_survey, choice)) + |> render("show.json", choice: choice) + end + end + + def show(conn, %{"id" => id}) do + choice = Survey.get_choice!(id) + Logger.info("choice = #{inspect(choice)}") + render(conn, "show.json", choice: choice) + end + + def alt_show(conn, %{"id" => id}) do + choice = Survey.get_choice!(id) + render(conn, "show.json", choice: choice) + end + + def update(conn, %{"id" => id, "choice" => choice_params}) do + choice = Survey.get_choice!(id) + + with {:ok, %Choice{} = choice} <- Survey.update_choice(choice, choice_params) do + render(conn, "show.json", choice: choice) + end + end + + def delete(conn, %{"id" => id}) do + choice = Survey.get_choice!(id) + + with {:ok, %Choice{}} <- Survey.delete_choice(choice) do + send_resp(conn, :no_content, "") + end + end + + def list_all(conn, _params) do + choices = Survey.list_choices() + render(conn, "index.json", choices: choices) + end + + defp put_poll_id(%{"choice" => choice_params, "id_survey" => id_survey}) do + new_params = Map.put(choice_params, "poll_id", id_survey) + new_params + end +end diff --git a/lib/diskuy_web/controllers/comment_controller.ex b/lib/diskuy_web/controllers/comment_controller.ex new file mode 100644 index 0000000000000000000000000000000000000000..622d7e06dd90c8469e3eb68db608aa1c04717c7f --- /dev/null +++ b/lib/diskuy_web/controllers/comment_controller.ex @@ -0,0 +1,56 @@ +defmodule DiskuyWeb.CommentController do + use DiskuyWeb, :controller + + alias Diskuy.Survey + alias Diskuy.Survey.Comment + + action_fallback DiskuyWeb.FallbackController + + def index(conn, _params) do + survey_comments = Survey.list_survey_comments() + render(conn, "index.json", survey_comments: survey_comments) + end + + def create(conn, %{"comment" => comment_params}) do + new_params = put_info(conn, %{"comment" => comment_params}) + with {:ok, %Comment{} = comment} <- Survey.create_comment(new_params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.comment_path(conn, :show, comment)) + |> render("show.json", comment: comment) + end + end + + def show(conn, %{"id" => id}) do + comment = Survey.get_comment!(id) + render(conn, "show.json", comment: comment) + end + + def update(conn, %{"id" => id, "comment" => comment_params}) do + comment = Survey.get_comment!(id) + + with {:ok, %Comment{} = comment} <- Survey.update_comment(comment, comment_params) do + render(conn, "show.json", comment: comment) + end + end + + def delete(conn, %{"id" => id}) do + comment = Survey.get_comment!(id) + + with {:ok, %Comment{}} <- Survey.delete_comment(comment) do + send_resp(conn, :no_content, "") + end + end + + def get_poll_comments(conn, %{"id" => id}) do + poll = Survey.get_poll!(id) + comments = Survey.list_poll_comments(poll) + render(conn, "index.json", survey_comments: comments) + end + + defp put_info(conn, %{"comment" => comment_params}) do + current_user = Guardian.Plug.current_resource(conn) + new_params = Map.put(comment_params, "user_id", current_user.id) + new_params + end +end diff --git a/lib/diskuy_web/controllers/poll_controller.ex b/lib/diskuy_web/controllers/poll_controller.ex new file mode 100644 index 0000000000000000000000000000000000000000..c209cf7a1634758c5f3b649e32f45060c561f602 --- /dev/null +++ b/lib/diskuy_web/controllers/poll_controller.ex @@ -0,0 +1,50 @@ +defmodule DiskuyWeb.PollController do + use DiskuyWeb, :controller + + alias Diskuy.Survey + alias Diskuy.Survey.Poll + + action_fallback DiskuyWeb.FallbackController + + def index(conn, _params) do + polls = Survey.list_polls() + render(conn, "index.json", polls: polls) + end + + def create(conn, %{"poll" => poll_params}) do + new_params = put_user_id(conn, %{"poll" => poll_params}) + with {:ok, %Poll{} = poll} <- Survey.create_poll(new_params) do + conn + |> put_status(:created) + |> put_resp_header("location", Routes.poll_path(conn, :show, poll)) + |> render("show.json", poll: poll) + end + end + + def show(conn, %{"id" => id}) do + poll = Survey.get_poll!(id) + render(conn, "show.json", poll: poll) + end + + def update(conn, %{"id" => id, "poll" => poll_params}) do + poll = Survey.get_poll!(id) + + with {:ok, %Poll{} = poll} <- Survey.update_poll(poll, poll_params) do + render(conn, "show.json", poll: poll) + end + end + + def delete(conn, %{"id" => id}) do + poll = Survey.get_poll!(id) + + with {:ok, %Poll{}} <- Survey.delete_poll(poll) do + send_resp(conn, :no_content, "") + end + end + + defp put_user_id(conn, %{"poll" => poll_params}) do + current_user = Guardian.Plug.current_resource(conn) + new_params = Map.put(poll_params, "user_id", current_user.id) + new_params + end +end diff --git a/lib/diskuy_web/controllers/vote_controller.ex b/lib/diskuy_web/controllers/vote_controller.ex new file mode 100644 index 0000000000000000000000000000000000000000..a034b30885d395436b6290f571fbc9cf1e1ad825 --- /dev/null +++ b/lib/diskuy_web/controllers/vote_controller.ex @@ -0,0 +1,42 @@ +defmodule DiskuyWeb.VoteController do + use DiskuyWeb, :controller + require Logger + + alias Diskuy.Survey + alias Diskuy.Survey.Vote + + action_fallback DiskuyWeb.FallbackController + + def index(conn, _params) do + votes = Survey.list_votes() + render(conn, "index.json", votes: votes) + end + + def create(conn, %{"id_choice" => id_choice}) do + choice = Survey.get_choice!(id_choice) + current_user = Guardian.Plug.current_resource(conn) + params = %{"user_id" => current_user.id, "choice_id" => id_choice, "poll_id" => choice.poll_id} + with {:ok, %Vote{} = vote} <- Survey.create_vote(params) do + conn + |> put_status(:created) + |> render("show.json", vote: vote) + end + end + + def update(conn, %{"id" => id, "vote" => vote_params}) do + vote = Survey.get_vote!(id) + + with {:ok, %Vote{} = vote} <- Survey.update_vote(vote, vote_params) do + render(conn, "show.json", vote: vote) + end + end + + def delete(conn, %{"id_choice" => id_choice}) do + current_user = Guardian.Plug.current_resource(conn) + vote = Survey.get_vote_by_user_and_choice(current_user.id, id_choice) + + with {:ok, %Vote{}} <- Survey.delete_vote(vote) do + send_resp(conn, :no_content, "") + end + end +end diff --git a/lib/diskuy_web/router.ex b/lib/diskuy_web/router.ex index d74fea47d1606d150f6c32e567c6b26e70d415ea..83c08a936ff2fc3e76f6df61ce4956b1f4806b84 100644 --- a/lib/diskuy_web/router.ex +++ b/lib/diskuy_web/router.ex @@ -52,6 +52,12 @@ defmodule DiskuyWeb.Router do get "/post/checklike/:id", PostLikeController, :check_like options "/post/checklike/:id", PostLikeController, :options + resources "/survey", PollController, except: [:new, :edit, :show, :index] + resources "/comment", CommentController, except: [:new, :edit, :show, :index] + resources "/survey/:id_survey/choice", ChoiceController, except: [:new, :edit, :show, :index] + post "/survey/choice/:id_choice/vote", VoteController, :create + post "/survey/choice/:id_choice/unvote", VoteController, :delete + get "/surveys/show_questions/:survey_id", SurveyController, :show_all_survey_questions options "/surveys/show_questions/:survey_id", SurveyController, :options @@ -111,7 +117,21 @@ defmodule DiskuyWeb.Router do # post "/users/signin", UserController, :signin # options "/users/signin", PostController, :options + resources "/survey", PollController, except: [:new, :edit, :create, :update, :delete] + options "/survey", PollController, :options + options "/survey/:id", PollController, :options + + resources "/comment", CommentController, except: [:new, :edit, :create, :update, :delete] + options "/comment", CommentController, :options + options "/comment/:id", CommentController, :options + + resources "/survey/:id_survey/choice", ChoiceController, except: [:new, :edit, :create, :update, :delete] + get "/survey/choice", ChoiceController, :list_all + get "/survey/choice/:id", ChoiceController, :alt_show + options "/choice", ChoiceController, :options + options "/choice/:id", ChoiceController, :options + get "survey/choice/:id/votes", ChoiceController, :vote_count end diff --git a/lib/diskuy_web/views/choice_view.ex b/lib/diskuy_web/views/choice_view.ex new file mode 100644 index 0000000000000000000000000000000000000000..8844614eaa11e5ba5987c1d12ba13a5a3df87a26 --- /dev/null +++ b/lib/diskuy_web/views/choice_view.ex @@ -0,0 +1,22 @@ +defmodule DiskuyWeb.ChoiceView do + use DiskuyWeb, :view + alias DiskuyWeb.ChoiceView + alias Diskuy.Survey + + def render("index.json", %{choices: choices}) do + %{data: render_many(choices, ChoiceView, "choice.json")} + end + + def render("show.json", %{choice: choice}) do + %{data: render_one(choice, ChoiceView, "choice.json")} + end + + def render("choice.json", %{choice: choice}) do + vote_count = Survey.count_choice_votes(choice) + %{id: choice.id, + choice: choice.choice, + poll_id: choice.poll_id, + vote_count: vote_count + } + end +end diff --git a/lib/diskuy_web/views/comment_view.ex b/lib/diskuy_web/views/comment_view.ex new file mode 100644 index 0000000000000000000000000000000000000000..88599fff80c8a4c3e91cb7b8205d86e69ca96c01 --- /dev/null +++ b/lib/diskuy_web/views/comment_view.ex @@ -0,0 +1,24 @@ +defmodule DiskuyWeb.CommentView do + use DiskuyWeb, :view + alias DiskuyWeb.CommentView + alias Diskuy.Account + + def render("index.json", %{survey_comments: survey_comments}) do + %{data: render_many(survey_comments, CommentView, "comment.json")} + end + + def render("show.json", %{comment: comment}) do + %{data: render_one(comment, CommentView, "comment.json")} + end + + def render("comment.json", %{comment: comment}) do + user = Account.get_user!(comment.user_id) + %{id: comment.id, + user_id: comment.user_id, + poll_id: comment.poll_id, + username: user.username, + comment: comment.comment, + timestamp: comment.inserted_at + } + end +end diff --git a/lib/diskuy_web/views/poll_view.ex b/lib/diskuy_web/views/poll_view.ex new file mode 100644 index 0000000000000000000000000000000000000000..c0c859a37c4972fb1bc9852ddb62c56d6b14c560 --- /dev/null +++ b/lib/diskuy_web/views/poll_view.ex @@ -0,0 +1,34 @@ +defmodule DiskuyWeb.PollView do + use DiskuyWeb, :view + alias DiskuyWeb.PollView + alias DiskuyWeb.CommentView + alias DiskuyWeb.ChoiceView + alias Diskuy.Survey + alias Diskuy.Account + + def render("index.json", %{polls: polls}) do + %{data: render_many(polls, PollView, "poll.json")} + end + + def render("show.json", %{poll: poll}) do + %{data: render_one(poll, PollView, "poll.json")} + end + + def render("poll.json", %{poll: poll}) do + comments = Survey.list_poll_comments(poll) + comment_view = render_many(comments, CommentView, "comment.json") + choices = Survey.list_poll_choices(poll) + choice_view = render_many(choices, ChoiceView, "choice.json") + user = Account.get_user!(poll.user_id) + %{id: poll.id, + points: poll.points, + title: poll.title, + end_time: poll.end_time, + topic_id: poll.topic_id, + user_id: poll.user_id, + username: user.username, + comments: comment_view, + choices: choice_view + } + end +end diff --git a/lib/diskuy_web/views/vote_view.ex b/lib/diskuy_web/views/vote_view.ex new file mode 100644 index 0000000000000000000000000000000000000000..d0d818d60e159eebe15e9e7d8f1913f908ce1bb9 --- /dev/null +++ b/lib/diskuy_web/views/vote_view.ex @@ -0,0 +1,16 @@ +defmodule DiskuyWeb.VoteView do + use DiskuyWeb, :view + alias DiskuyWeb.VoteView + + def render("index.json", %{votes: votes}) do + %{data: render_many(votes, VoteView, "vote.json")} + end + + def render("show.json", %{vote: vote}) do + %{data: render_one(vote, VoteView, "vote.json")} + end + + def render("vote.json", %{vote: vote}) do + %{user_id: vote.user_id} + end +end diff --git a/priv/repo/migrations/20211211065116_create_polls.exs b/priv/repo/migrations/20211211065116_create_polls.exs new file mode 100644 index 0000000000000000000000000000000000000000..7cdfdf94115bdc0e71765931246f65e15172c756 --- /dev/null +++ b/priv/repo/migrations/20211211065116_create_polls.exs @@ -0,0 +1,19 @@ +defmodule Diskuy.Repo.Migrations.CreatePolls do + use Ecto.Migration + + def change do + create table(:polls) do + add :points, :integer, null: false + add :title, :string, null: false + add :end_time, :utc_datetime, null: false + add :topic_id, references(:topics, on_delete: :delete_all) + add :user_id, references(:users, on_delete: :delete_all) + + timestamps() + end + + create index(:polls, [:topic_id]) + create index(:polls, [:user_id]) + create unique_index(:polls, [:title]) + end +end diff --git a/priv/repo/migrations/20211211075121_create_survey_comments.exs b/priv/repo/migrations/20211211075121_create_survey_comments.exs new file mode 100644 index 0000000000000000000000000000000000000000..05ee3b0b49082a41b672637a13a1fa730ede1256 --- /dev/null +++ b/priv/repo/migrations/20211211075121_create_survey_comments.exs @@ -0,0 +1,16 @@ +defmodule Diskuy.Repo.Migrations.CreateSurveyComments do + use Ecto.Migration + + def change do + create table(:survey_comments) do + add :comment, :text + add :user_id, references(:users, on_delete: :delete_all) + add :poll_id, references(:polls, on_delete: :delete_all) + + timestamps() + end + + create index(:survey_comments, [:user_id]) + create index(:survey_comments, [:poll_id]) + end +end diff --git a/priv/repo/migrations/20211229120501_create_choices.exs b/priv/repo/migrations/20211229120501_create_choices.exs new file mode 100644 index 0000000000000000000000000000000000000000..5cad5f5e0646fa69078ca0ae6ce0ae228fefeeec --- /dev/null +++ b/priv/repo/migrations/20211229120501_create_choices.exs @@ -0,0 +1,14 @@ +defmodule Diskuy.Repo.Migrations.CreateChoices do + use Ecto.Migration + + def change do + create table(:choices) do + add :choice, :string + add :poll_id, references(:polls, on_delete: :delete_all) + + timestamps() + end + + create index(:choices, [:poll_id]) + end +end diff --git a/priv/repo/migrations/20211229121200_create_votes.exs b/priv/repo/migrations/20211229121200_create_votes.exs new file mode 100644 index 0000000000000000000000000000000000000000..ce31b2f2d74dcc489073150fea343847ee5b281b --- /dev/null +++ b/priv/repo/migrations/20211229121200_create_votes.exs @@ -0,0 +1,18 @@ +defmodule Diskuy.Repo.Migrations.CreateVotes do + use Ecto.Migration + + def change do + create table(:votes) do + add :user_id, references(:users, on_delete: :delete_all) + add :choice_id, references(:choices, on_delete: :delete_all) + add :poll_id, references(:polls, on_delete: :delete_all) + + timestamps() + end + + create index(:votes, [:user_id]) + create index(:votes, [:choice_id]) + create unique_index(:votes, [:user_id, :choice_id], name: :vote_unique_index) + create unique_index(:votes, [:user_id, :poll_id], name: :vote_unique_choice) + end +end diff --git a/test/diskuy/survey_test.exs b/test/diskuy/survey_test.exs new file mode 100644 index 0000000000000000000000000000000000000000..ea76d6a96029340ceb7f57e794bfef4f13407dcf --- /dev/null +++ b/test/diskuy/survey_test.exs @@ -0,0 +1,374 @@ +defmodule Diskuy.SurveyTest do + use Diskuy.DataCase + + alias Diskuy.Survey + + describe "polls" do + alias Diskuy.Survey.Poll + + @valid_attrs %{end_time: "2010-04-17T14:00:00Z", points: 42, title: "some title"} + @update_attrs %{end_time: "2011-05-18T15:01:01Z", points: 43, title: "some updated title"} + @invalid_attrs %{end_time: nil, points: nil, title: nil} + + def poll_fixture(attrs \\ %{}) do + {:ok, poll} = + attrs + |> Enum.into(@valid_attrs) + |> Survey.create_poll() + + poll + end + + test "list_polls/0 returns all polls" do + poll = poll_fixture() + assert Survey.list_polls() == [poll] + end + + test "get_poll!/1 returns the poll with given id" do + poll = poll_fixture() + assert Survey.get_poll!(poll.id) == poll + end + + test "create_poll/1 with valid data creates a poll" do + assert {:ok, %Poll{} = poll} = Survey.create_poll(@valid_attrs) + assert poll.end_time == DateTime.from_naive!(~N[2010-04-17T14:00:00Z], "Etc/UTC") + assert poll.points == 42 + assert poll.title == "some title" + end + + test "create_poll/1 with invalid data returns error change" do + assert {:error, %Ecto.Changeset{}} = Survey.create_poll(@invalid_attrs) + end + + test "update_poll/2 with valid data updates the poll" do + poll = poll_fixture() + assert {:ok, %Poll{} = poll} = Survey.update_poll(poll, @update_attrs) + assert poll.end_time == DateTime.from_naive!(~N[2011-05-18T15:01:01Z], "Etc/UTC") + assert poll.points == 43 + assert poll.title == "some updated title" + end + + test "update_poll/2 with invalid data returns error changeset" do + poll = poll_fixture() + assert {:error, %Ecto.Changeset{}} = Survey.update_poll(poll, @invalid_attrs) + assert poll == Survey.get_poll!(poll.id) + end + + test "delete_poll/1 deletes the poll" do + poll = poll_fixture() + assert {:ok, %Poll{}} = Survey.delete_poll(poll) + assert_raise Ecto.NoResultsError, fn -> Survey.get_poll!(poll.id) end + end + + test "change_poll/1 returns a poll changeset" do + poll = poll_fixture() + assert %Ecto.Changeset{} = Survey.change_poll(poll) + end + end + + describe "choices" do + alias Diskuy.Survey.Choice + + @valid_attrs %{name: "some name"} + @update_attrs %{name: "some updated name"} + @invalid_attrs %{name: nil} + + def choice_fixture(poll, attrs \\ %{}) do + attrs = Enum.into(attrs, @valid_attrs) + {:ok, choice} = Poll.create_choice(poll, attrs) + + choice + end + + setup do + poll = poll_fixture() + {:ok, poll: poll} + end + + def choice_fixture(attrs \\ %{}) do + {:ok, choice} = + attrs + |> Enum.into(@valid_attrs) + |> Survey.create_choice() + + choice + end + + test "list_choices/0 returns all choices", %{poll: poll} do + choice = choice_fixture(poll) + assert Survey.list_choices(poll) == [choice] + end + + test "get_choice!/1 returns the choice with given id" do + choice = choice_fixture() + assert Survey.get_choice!(choice.id) == choice + end + + test "create_choice/1 with valid data creates a choice" do + assert {:ok, %Choice{} = choice} = Survey.create_choice(@valid_attrs) + assert choice.name == "some name" + end + + test "create_choice/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Survey.create_choice(@invalid_attrs) + end + + test "update_choice/2 with valid data updates the choice" do + choice = choice_fixture() + assert {:ok, %Choice{} = choice} = Survey.update_choice(choice, @update_attrs) + assert choice.name == "some updated name" + end + + test "update_choice/2 with invalid data returns error changeset" do + choice = choice_fixture() + assert {:error, %Ecto.Changeset{}} = Survey.update_choice(choice, @invalid_attrs) + assert choice == Survey.get_choice!(choice.id) + end + + test "delete_choice/1 deletes the choice" do + choice = choice_fixture() + assert {:ok, %Choice{}} = Survey.delete_choice(choice) + assert_raise Ecto.NoResultsError, fn -> Survey.get_choice!(choice.id) end + end + + test "change_choice/1 returns a choice changeset" do + choice = choice_fixture() + assert %Ecto.Changeset{} = Survey.change_choice(choice) + end + end + + describe "votes" do + alias Diskuy.Survey.Vote + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def vote_fixture(choice, attrs \\ %{}) do + attrs = Enum.into(attrs, @valid_attrs) + {:ok, vote} = Choice.create_vote(choice, attrs) + + variant + end + + setup do + choice = choice_fixture() + {:ok, choice: choice} + end + + test "list_votes/0 returns all votes", %{choice: choice} do + vote = vote_fixture(choice) + assert Survey.list_votes(choice) == [vote] + end + + test "get_vote!/1 returns the vote with given id" do + vote = vote_fixture() + assert Survey.get_vote!(vote.id) == vote + end + + test "create_vote/1 with valid data creates a vote" do + assert {:ok, %Vote{} = vote} = Survey.create_vote(@valid_attrs) + end + + test "create_vote/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Survey.create_vote(@invalid_attrs) + end + + test "update_vote/2 with valid data updates the vote" do + vote = vote_fixture() + assert {:ok, %Vote{} = vote} = Survey.update_vote(vote, @update_attrs) + end + + test "update_vote/2 with invalid data returns error changeset" do + vote = vote_fixture() + assert {:error, %Ecto.Changeset{}} = Survey.update_vote(vote, @invalid_attrs) + assert vote == Survey.get_vote!(vote.id) + end + + test "delete_vote/1 deletes the vote" do + vote = vote_fixture() + assert {:ok, %Vote{}} = Survey.delete_vote(vote) + assert_raise Ecto.NoResultsError, fn -> Survey.get_vote!(vote.id) end + end + + test "change_vote/1 returns a vote changeset" do + vote = vote_fixture() + assert %Ecto.Changeset{} = Survey.change_vote(vote) + end + end + + describe "survey_comments" do + alias Diskuy.Survey.Comment + + @valid_attrs %{comment: "some comment"} + @update_attrs %{comment: "some updated comment"} + @invalid_attrs %{comment: nil} + + def comment_fixture(attrs \\ %{}) do + {:ok, comment} = + attrs + |> Enum.into(@valid_attrs) + |> Survey.create_comment() + + comment + end + + test "list_survey_comments/0 returns all survey_comments" do + comment = comment_fixture() + assert Survey.list_survey_comments() == [comment] + end + + test "get_comment!/1 returns the comment with given id" do + comment = comment_fixture() + assert Survey.get_comment!(comment.id) == comment + end + + test "create_comment/1 with valid data creates a comment" do + assert {:ok, %Comment{} = comment} = Survey.create_comment(@valid_attrs) + assert comment.comment == "some comment" + end + + test "create_comment/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Survey.create_comment(@invalid_attrs) + end + + test "update_comment/2 with valid data updates the comment" do + comment = comment_fixture() + assert {:ok, %Comment{} = comment} = Survey.update_comment(comment, @update_attrs) + assert comment.comment == "some updated comment" + end + + test "update_comment/2 with invalid data returns error changeset" do + comment = comment_fixture() + assert {:error, %Ecto.Changeset{}} = Survey.update_comment(comment, @invalid_attrs) + assert comment == Survey.get_comment!(comment.id) + end + + test "delete_comment/1 deletes the comment" do + comment = comment_fixture() + assert {:ok, %Comment{}} = Survey.delete_comment(comment) + assert_raise Ecto.NoResultsError, fn -> Survey.get_comment!(comment.id) end + end + + test "change_comment/1 returns a comment changeset" do + comment = comment_fixture() + assert %Ecto.Changeset{} = Survey.change_comment(comment) + end + end + + describe "choices" do + alias Diskuy.Survey.Choice + + @valid_attrs %{choice: "some choice"} + @update_attrs %{choice: "some updated choice"} + @invalid_attrs %{choice: nil} + + def choice_fixture(attrs \\ %{}) do + {:ok, choice} = + attrs + |> Enum.into(@valid_attrs) + |> Survey.create_choice() + + choice + end + + test "list_choices/0 returns all choices" do + choice = choice_fixture() + assert Survey.list_choices() == [choice] + end + + test "get_choice!/1 returns the choice with given id" do + choice = choice_fixture() + assert Survey.get_choice!(choice.id) == choice + end + + test "create_choice/1 with valid data creates a choice" do + assert {:ok, %Choice{} = choice} = Survey.create_choice(@valid_attrs) + assert choice.choice == "some choice" + end + + test "create_choice/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Survey.create_choice(@invalid_attrs) + end + + test "update_choice/2 with valid data updates the choice" do + choice = choice_fixture() + assert {:ok, %Choice{} = choice} = Survey.update_choice(choice, @update_attrs) + assert choice.choice == "some updated choice" + end + + test "update_choice/2 with invalid data returns error changeset" do + choice = choice_fixture() + assert {:error, %Ecto.Changeset{}} = Survey.update_choice(choice, @invalid_attrs) + assert choice == Survey.get_choice!(choice.id) + end + + test "delete_choice/1 deletes the choice" do + choice = choice_fixture() + assert {:ok, %Choice{}} = Survey.delete_choice(choice) + assert_raise Ecto.NoResultsError, fn -> Survey.get_choice!(choice.id) end + end + + test "change_choice/1 returns a choice changeset" do + choice = choice_fixture() + assert %Ecto.Changeset{} = Survey.change_choice(choice) + end + end + + describe "votes" do + alias Diskuy.Survey.Vote + + @valid_attrs %{} + @update_attrs %{} + @invalid_attrs %{} + + def vote_fixture(attrs \\ %{}) do + {:ok, vote} = + attrs + |> Enum.into(@valid_attrs) + |> Survey.create_vote() + + vote + end + + test "list_votes/0 returns all votes" do + vote = vote_fixture() + assert Survey.list_votes() == [vote] + end + + test "get_vote!/1 returns the vote with given id" do + vote = vote_fixture() + assert Survey.get_vote!(vote.id) == vote + end + + test "create_vote/1 with valid data creates a vote" do + assert {:ok, %Vote{} = vote} = Survey.create_vote(@valid_attrs) + end + + test "create_vote/1 with invalid data returns error changeset" do + assert {:error, %Ecto.Changeset{}} = Survey.create_vote(@invalid_attrs) + end + + test "update_vote/2 with valid data updates the vote" do + vote = vote_fixture() + assert {:ok, %Vote{} = vote} = Survey.update_vote(vote, @update_attrs) + end + + test "update_vote/2 with invalid data returns error changeset" do + vote = vote_fixture() + assert {:error, %Ecto.Changeset{}} = Survey.update_vote(vote, @invalid_attrs) + assert vote == Survey.get_vote!(vote.id) + end + + test "delete_vote/1 deletes the vote" do + vote = vote_fixture() + assert {:ok, %Vote{}} = Survey.delete_vote(vote) + assert_raise Ecto.NoResultsError, fn -> Survey.get_vote!(vote.id) end + end + + test "change_vote/1 returns a vote changeset" do + vote = vote_fixture() + assert %Ecto.Changeset{} = Survey.change_vote(vote) + end + end +end diff --git a/test/diskuy_web/controllers/choice_controller_test.exs b/test/diskuy_web/controllers/choice_controller_test.exs new file mode 100644 index 0000000000000000000000000000000000000000..5813871dd9726e3abda26ff879fc7a59ca53348c --- /dev/null +++ b/test/diskuy_web/controllers/choice_controller_test.exs @@ -0,0 +1,88 @@ +defmodule DiskuyWeb.ChoiceControllerTest do + use DiskuyWeb.ConnCase + + alias Diskuy.Survey + alias Diskuy.Survey.Choice + + @create_attrs %{ + choice: "some choice" + } + @update_attrs %{ + choice: "some updated choice" + } + @invalid_attrs %{choice: nil} + + def fixture(:choice) do + {:ok, choice} = Survey.create_choice(@create_attrs) + choice + end + + setup %{conn: conn} do + {:ok, conn: put_req_header(conn, "accept", "application/json")} + end + + describe "index" do + test "lists all choices", %{conn: conn} do + conn = get(conn, Routes.choice_path(conn, :index)) + assert json_response(conn, 200)["data"] == [] + end + end + + describe "create choice" do + test "renders choice when data is valid", %{conn: conn} do + conn = post(conn, Routes.choice_path(conn, :create), choice: @create_attrs) + assert %{"id" => id} = json_response(conn, 201)["data"] + + conn = get(conn, Routes.choice_path(conn, :show, id)) + + assert %{ + "id" => id, + "choice" => "some choice" + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, Routes.choice_path(conn, :create), choice: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "update choice" do + setup [:create_choice] + + test "renders choice when data is valid", %{conn: conn, choice: %Choice{id: id} = choice} do + conn = put(conn, Routes.choice_path(conn, :update, choice), choice: @update_attrs) + assert %{"id" => ^id} = json_response(conn, 200)["data"] + + conn = get(conn, Routes.choice_path(conn, :show, id)) + + assert %{ + "id" => id, + "choice" => "some updated choice" + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn, choice: choice} do + conn = put(conn, Routes.choice_path(conn, :update, choice), choice: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "delete choice" do + setup [:create_choice] + + test "deletes chosen choice", %{conn: conn, choice: choice} do + conn = delete(conn, Routes.choice_path(conn, :delete, choice)) + assert response(conn, 204) + + assert_error_sent 404, fn -> + get(conn, Routes.choice_path(conn, :show, choice)) + end + end + end + + defp create_choice(_) do + choice = fixture(:choice) + %{choice: choice} + end +end diff --git a/test/diskuy_web/controllers/comment_controller_test.exs b/test/diskuy_web/controllers/comment_controller_test.exs new file mode 100644 index 0000000000000000000000000000000000000000..bf4a64217739f52511fa6cb09887dd628febcf79 --- /dev/null +++ b/test/diskuy_web/controllers/comment_controller_test.exs @@ -0,0 +1,88 @@ +defmodule DiskuyWeb.CommentControllerTest do + use DiskuyWeb.ConnCase + + alias Diskuy.Survey + alias Diskuy.Survey.Comment + + @create_attrs %{ + comment: "some comment" + } + @update_attrs %{ + comment: "some updated comment" + } + @invalid_attrs %{comment: nil} + + def fixture(:comment) do + {:ok, comment} = Survey.create_comment(@create_attrs) + comment + end + + setup %{conn: conn} do + {:ok, conn: put_req_header(conn, "accept", "application/json")} + end + + describe "index" do + test "lists all survey_comments", %{conn: conn} do + conn = get(conn, Routes.comment_path(conn, :index)) + assert json_response(conn, 200)["data"] == [] + end + end + + describe "create comment" do + test "renders comment when data is valid", %{conn: conn} do + conn = post(conn, Routes.comment_path(conn, :create), comment: @create_attrs) + assert %{"id" => id} = json_response(conn, 201)["data"] + + conn = get(conn, Routes.comment_path(conn, :show, id)) + + assert %{ + "id" => id, + "comment" => "some comment" + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, Routes.comment_path(conn, :create), comment: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "update comment" do + setup [:create_comment] + + test "renders comment when data is valid", %{conn: conn, comment: %Comment{id: id} = comment} do + conn = put(conn, Routes.comment_path(conn, :update, comment), comment: @update_attrs) + assert %{"id" => ^id} = json_response(conn, 200)["data"] + + conn = get(conn, Routes.comment_path(conn, :show, id)) + + assert %{ + "id" => id, + "comment" => "some updated comment" + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn, comment: comment} do + conn = put(conn, Routes.comment_path(conn, :update, comment), comment: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "delete comment" do + setup [:create_comment] + + test "deletes chosen comment", %{conn: conn, comment: comment} do + conn = delete(conn, Routes.comment_path(conn, :delete, comment)) + assert response(conn, 204) + + assert_error_sent 404, fn -> + get(conn, Routes.comment_path(conn, :show, comment)) + end + end + end + + defp create_comment(_) do + comment = fixture(:comment) + %{comment: comment} + end +end diff --git a/test/diskuy_web/controllers/poll_controller_test.exs b/test/diskuy_web/controllers/poll_controller_test.exs new file mode 100644 index 0000000000000000000000000000000000000000..5f131b204d2c5a6ffcc27391fdb0aaedcf965ec5 --- /dev/null +++ b/test/diskuy_web/controllers/poll_controller_test.exs @@ -0,0 +1,96 @@ +defmodule DiskuyWeb.PollControllerTest do + use DiskuyWeb.ConnCase + + alias Diskuy.Survey + alias Diskuy.Survey.Poll + + @create_attrs %{ + end_time: "2010-04-17T14:00:00Z", + points: 42, + title: "some title" + } + @update_attrs %{ + end_time: "2011-05-18T15:01:01Z", + points: 43, + title: "some updated title" + } + @invalid_attrs %{end_time: nil, points: nil, title: nil} + + def fixture(:poll) do + {:ok, poll} = Survey.create_poll(@create_attrs) + poll + end + + setup %{conn: conn} do + {:ok, conn: put_req_header(conn, "accept", "application/json")} + end + + describe "index" do + test "lists all polls", %{conn: conn} do + conn = get(conn, Routes.poll_path(conn, :index)) + assert json_response(conn, 200)["data"] == [] + end + end + + describe "create poll" do + test "renders poll when data is valid", %{conn: conn} do + conn = post(conn, Routes.poll_path(conn, :create), poll: @create_attrs) + assert %{"id" => id} = json_response(conn, 201)["data"] + + conn = get(conn, Routes.poll_path(conn, :show, id)) + + assert %{ + "id" => id, + "end_time" => "2010-04-17T14:00:00Z", + "points" => 42, + "title" => "some title" + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, Routes.poll_path(conn, :create), poll: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "update poll" do + setup [:create_poll] + + test "renders poll when data is valid", %{conn: conn, poll: %Poll{id: id} = poll} do + conn = put(conn, Routes.poll_path(conn, :update, poll), poll: @update_attrs) + assert %{"id" => ^id} = json_response(conn, 200)["data"] + + conn = get(conn, Routes.poll_path(conn, :show, id)) + + assert %{ + "id" => id, + "end_time" => "2011-05-18T15:01:01Z", + "points" => 43, + "title" => "some updated title" + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn, poll: poll} do + conn = put(conn, Routes.poll_path(conn, :update, poll), poll: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "delete poll" do + setup [:create_poll] + + test "deletes chosen poll", %{conn: conn, poll: poll} do + conn = delete(conn, Routes.poll_path(conn, :delete, poll)) + assert response(conn, 204) + + assert_error_sent 404, fn -> + get(conn, Routes.poll_path(conn, :show, poll)) + end + end + end + + defp create_poll(_) do + poll = fixture(:poll) + %{poll: poll} + end +end diff --git a/test/diskuy_web/controllers/vote_controller_test.exs b/test/diskuy_web/controllers/vote_controller_test.exs new file mode 100644 index 0000000000000000000000000000000000000000..fba86fc7bf57a47f94f99bbaf28c054cc7197f85 --- /dev/null +++ b/test/diskuy_web/controllers/vote_controller_test.exs @@ -0,0 +1,86 @@ +defmodule DiskuyWeb.VoteControllerTest do + use DiskuyWeb.ConnCase + + alias Diskuy.Survey + alias Diskuy.Survey.Vote + + @create_attrs %{ + + } + @update_attrs %{ + + } + @invalid_attrs %{} + + def fixture(:vote) do + {:ok, vote} = Survey.create_vote(@create_attrs) + vote + end + + setup %{conn: conn} do + {:ok, conn: put_req_header(conn, "accept", "application/json")} + end + + describe "index" do + test "lists all votes", %{conn: conn} do + conn = get(conn, Routes.vote_path(conn, :index)) + assert json_response(conn, 200)["data"] == [] + end + end + + describe "create vote" do + test "renders vote when data is valid", %{conn: conn} do + conn = post(conn, Routes.vote_path(conn, :create), vote: @create_attrs) + assert %{"id" => id} = json_response(conn, 201)["data"] + + conn = get(conn, Routes.vote_path(conn, :show, id)) + + assert %{ + "id" => id + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn} do + conn = post(conn, Routes.vote_path(conn, :create), vote: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "update vote" do + setup [:create_vote] + + test "renders vote when data is valid", %{conn: conn, vote: %Vote{id: id} = vote} do + conn = put(conn, Routes.vote_path(conn, :update, vote), vote: @update_attrs) + assert %{"id" => ^id} = json_response(conn, 200)["data"] + + conn = get(conn, Routes.vote_path(conn, :show, id)) + + assert %{ + "id" => id + } = json_response(conn, 200)["data"] + end + + test "renders errors when data is invalid", %{conn: conn, vote: vote} do + conn = put(conn, Routes.vote_path(conn, :update, vote), vote: @invalid_attrs) + assert json_response(conn, 422)["errors"] != %{} + end + end + + describe "delete vote" do + setup [:create_vote] + + test "deletes chosen vote", %{conn: conn, vote: vote} do + conn = delete(conn, Routes.vote_path(conn, :delete, vote)) + assert response(conn, 204) + + assert_error_sent 404, fn -> + get(conn, Routes.vote_path(conn, :show, vote)) + end + end + end + + defp create_vote(_) do + vote = fixture(:vote) + %{vote: vote} + end +end