From 42900cd02e046ad9d80b03a5a8ab9b8d70704332 Mon Sep 17 00:00:00 2001 From: Irere123 Date: Mon, 19 Aug 2024 21:48:51 +0200 Subject: [PATCH] feat(api): delete thread message by id --- api/lib/breeze/routes/v1/threads.ex | 17 +++++++++++++++++ api/lib/telescope/channels.ex | 1 + api/lib/telescope/messages.ex | 1 + api/lib/telescope/mutations/channels.ex | 4 ++++ api/lib/telescope/mutations/messages.ex | 4 ++++ api/lib/telescope/schemas/thread.ex | 6 ++++++ 6 files changed, 33 insertions(+) diff --git a/api/lib/breeze/routes/v1/threads.ex b/api/lib/breeze/routes/v1/threads.ex index a67f2cf..0de5d86 100644 --- a/api/lib/breeze/routes/v1/threads.ex +++ b/api/lib/breeze/routes/v1/threads.ex @@ -211,4 +211,21 @@ defmodule Breeze.Routes.V1.Threads do |> send_resp(401, Jason.encode!(%{error: "UNAUTHORIZED"})) end end + + delete "/delete-message" do + %Plug.Conn{params: %{"messageId" => message_id}} = conn + + resp = + case Messages.delete_thread_message_by_id(message_id) do + {:ok, _} -> + %{success: true} + + {:error, changeset_error} -> + error = Spek.Utils.Errors.changeset_to_first_err_message(changeset_error) + %{error: error, success: true} + end + + conn + |> send_resp(200, Jason.encode!(resp)) + end end diff --git a/api/lib/telescope/channels.ex b/api/lib/telescope/channels.ex index e55fc9b..44ea5ac 100644 --- a/api/lib/telescope/channels.ex +++ b/api/lib/telescope/channels.ex @@ -10,6 +10,7 @@ defmodule Telescope.Channels do # MUTATIONS defdelegate create_thread(data), to: Telescope.Mutations.Channels + defdelegate delete_thread_by_id(thread_id), to: Telescope.Mutations.Channels defdelegate delete_channel(channel_id, user_id), to: Telescope.Mutations.Channels defdelegate create_channel(data, user_id), to: Telescope.Mutations.Channels defdelegate join_channel(channel_id, user_id), to: Telescope.Mutations.Channels diff --git a/api/lib/telescope/messages.ex b/api/lib/telescope/messages.ex index bd4ef5b..fe76731 100644 --- a/api/lib/telescope/messages.ex +++ b/api/lib/telescope/messages.ex @@ -4,4 +4,5 @@ defmodule Telescope.Messages do # MUTATIONS defdelegate create_thread_message(data), to: Telescope.Mutations.Messages + defdelegate delete_thread_message_by_id(message_id), to: Telescope.Mutations.Messages end diff --git a/api/lib/telescope/mutations/channels.ex b/api/lib/telescope/mutations/channels.ex index 8a4f4b6..de6b7b6 100644 --- a/api/lib/telescope/mutations/channels.ex +++ b/api/lib/telescope/mutations/channels.ex @@ -118,4 +118,8 @@ defmodule Telescope.Mutations.Channels do |> Repo.insert!(returning: true) |> Repo.preload(:creator) end + + def delete_thread_by_id(thread_id) do + %Thread{id: thread_id} |> Repo.delete() + end end diff --git a/api/lib/telescope/mutations/messages.ex b/api/lib/telescope/mutations/messages.ex index 58081a6..535b758 100644 --- a/api/lib/telescope/mutations/messages.ex +++ b/api/lib/telescope/mutations/messages.ex @@ -11,4 +11,8 @@ defmodule Telescope.Mutations.Messages do |> Repo.insert!(returning: true) |> Repo.preload(:user) end + + def delete_thread_message_by_id(message_id) do + %Message{id: message_id} |> Repo.delete() + end end diff --git a/api/lib/telescope/schemas/thread.ex b/api/lib/telescope/schemas/thread.ex index 3538001..198c8fa 100644 --- a/api/lib/telescope/schemas/thread.ex +++ b/api/lib/telescope/schemas/thread.ex @@ -42,4 +42,10 @@ defmodule Telescope.Schemas.Thread do |> cast_embed(:peoplePreviewList) |> validate_required([:channelId, :creatorId, :name, :peoplePreviewList]) end + + def edit_changeset(thread, params \\ %{}) do + thread + |> cast(params, [:name]) + |> validate_required([:name]) + end end