From 4dc1ab7e78c6978d40d4dfbf68ad597f2ec3e8af Mon Sep 17 00:00:00 2001 From: Zacck Osiemo Date: Thu, 15 Feb 2018 18:40:08 +0200 Subject: [PATCH] add function clauses to handle adding closed and reopned conversation_parts --- lib/code_corps/messages/messages.ex | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/code_corps/messages/messages.ex b/lib/code_corps/messages/messages.ex index a4bf3b731..df67d4a36 100644 --- a/lib/code_corps/messages/messages.ex +++ b/lib/code_corps/messages/messages.ex @@ -12,6 +12,9 @@ defmodule CodeCorps.Messages do Repo } alias Ecto.{Changeset, Queryable} + + @reopened "reopened" + @closed "closed" @doc ~S""" Lists pre-scoped `CodeCorps.Message` records filtered by parameters. @@ -55,19 +58,22 @@ defmodule CodeCorps.Messages do @doc ~S""" Updates a `CodeCorps.Conversation` record """ + def update_conversation(conversation, %{status: @reopened} = params) do + add_part(%{"conversation_id" => conversation.id, "body" => "reopened", "author_id" + => conversation.user_id, "part_type" => "reopened"}) + conversation |> Conversation.update_changeset(params) |> Repo.update + end + + def update_conversation(conversation, %{status: @closed} = params) do + add_part(%{"conversation_id" => conversation.id, "body" => "closed", "author_id" + => conversation.user_id, "part_type" => "closed"}) + conversation |> Conversation.update_changeset(params) |> Repo.update + end + def update_conversation(conversation, params) do - %{status: status} = params - case status do - "reopened" -> - add_part(%{"conversation_id" => conversation.id, "body" => "reopened", "author_id" - => conversation.user_id, "part_type" => "reopened"}) - "closed" -> - add_part(%{"conversation_id" => conversation.id, "body" => "closed", "author_id" - => conversation.user_id, "part_type" => "closed"}) - - end conversation |> Conversation.update_changeset(params) |> Repo.update end + @doc ~S""" Gets a `CodeCorps.ConversationPart` record