Skip to content

Commit

Permalink
feat(common): add sonarr integration
Browse files Browse the repository at this point in the history
  • Loading branch information
trueChazza committed Jan 23, 2022
1 parent 6026432 commit b23250b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/media_server/integrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ defmodule MediaServer.Integrations do
"""
def get_sonarr!(id), do: Repo.get!(Sonarr, id)

def get_first_sonarr() do
sonarr = Sonarr |> first |> Repo.one

case sonarr do

:nil ->
change_sonarr(%Sonarr{})

_ ->
change_sonarr(sonarr)
end
end

@doc """
Creates a sonarr.
Expand Down Expand Up @@ -73,6 +86,22 @@ defmodule MediaServer.Integrations do
|> Repo.update()
end

def update_or_create_sonarr(attrs \\ %{}) do

sonarr = Sonarr |> first |> Repo.one

case sonarr do

:nil ->
%Sonarr{}
|> Sonarr.changeset(attrs)
|> Repo.insert()

_ ->
update_sonarr(sonarr, attrs)
end
end

@doc """
Deletes a sonarr.
Expand Down
17 changes: 17 additions & 0 deletions lib/media_server_web/components/integration_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ defmodule MediaServerWeb.Components.IntegrationComponent do
save_radarr(socket, radarr_params)
end

def handle_event("save", %{"sonarr" => sonarr_params}, socket) do
save_sonarr(socket, sonarr_params)
end

defp save_radarr(socket, radarr_params) do
case Integrations.update_or_create_radarr(radarr_params) do
{:ok, _radarr} ->
Expand All @@ -19,4 +23,17 @@ defmodule MediaServerWeb.Components.IntegrationComponent do
{:noreply, assign(socket, :changeset, changeset)}
end
end

defp save_sonarr(socket, sonarr_params) do
case Integrations.update_or_create_sonarr(sonarr_params) do
{:ok, _sonarr} ->
{:noreply,
socket
|> put_flash(:info, "Success")
|> push_redirect(to: socket.assigns.return_to)}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
end
1 change: 1 addition & 0 deletions lib/media_server_web/live/settings_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule MediaServerWeb.SettingsLive.Index do
socket
|> assign(:page_title, "Settings")
|> assign(:radarr, Integrations.get_first_radarr())
|> assign(:sonarr, Integrations.get_first_sonarr())
}
end

Expand Down
11 changes: 11 additions & 0 deletions lib/media_server_web/live/settings_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,15 @@
description: "Fill in the information below to integrate your Radarr instance."
%>
</div>

<div>
<%= live_component MediaServerWeb.Components.IntegrationComponent,
id: "sonarr-form",
integration: "sonarr",
changeset: @sonarr,
return_to: Routes.settings_index_path(@socket, :index),
title: "Sonarr Integration",
description: "Fill in the information below to integrate your Sonarr instance."
%>
</div>
</div>
21 changes: 21 additions & 0 deletions test/media_server_web/live/settings_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defmodule MediaServerWeb.SettingsLiveTest do
{:ok, _index_live, html} = live(conn, Routes.settings_index_path(conn, :index))

assert html =~ "Radarr Integration"
assert html =~ "Sonarr Integration"
end

test "it creates radarr", %{conn: conn, user: user} do
Expand All @@ -44,5 +45,25 @@ defmodule MediaServerWeb.SettingsLiveTest do
assert html =~ "some name"
assert html =~ "some url"
end

test "it creates sonarr", %{conn: conn, user: user} do

conn =
post(conn, Routes.user_session_path(conn, :create), %{
"user" => %{"email" => user.email, "password" => valid_user_password()}
})

{:ok, index_live, _html} = live(conn, Routes.settings_index_path(conn, :index))

{:ok, _, html} =
index_live
|> form("#sonarr-form", sonarr: @create_attrs)
|> render_submit()
|> follow_redirect(conn, Routes.settings_index_path(conn, :index))

assert html =~ "some api_key"
assert html =~ "some name"
assert html =~ "some url"
end
end
end

0 comments on commit b23250b

Please sign in to comment.