-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue/101 REST API: Chapter Management (#152)
* add(api/chapters): create * add(api/chapters): update * add(api/chapters): delete * refactor: contorller only accesses Directory.Editor * refactor: use :rest_controller * refactor: extract assign_audio/2 plug * make chapter start time required * doc: add chapter api doc * refactor: shorten halt call * add fixme comment * make `[:audio_id, :start]` chapter primary key * remove "set chapters" mutation * consolidate list_chapters/3 resolver * rest: include audio_id in chapter * add fixme comment for chapter image * delete previous chapter image on update * delete chapter deletes chapter image * rest: add missing `show` action * refactor: use :assign_chapter plug * permissions: check directly against audio * refactor: introduce `apply_action_fallback`
- Loading branch information
Showing
18 changed files
with
290 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
defmodule RadiatorWeb.Api.ChaptersController do | ||
use RadiatorWeb, :rest_controller | ||
|
||
alias Radiator.Directory.Editor | ||
|
||
plug :assign_audio when action in [:show, :create, :update, :delete] | ||
plug :assign_chapter when action in [:show, :update, :delete] | ||
|
||
def show(conn, _params) do | ||
render(conn, "show.json") | ||
end | ||
|
||
def create(conn, %{"chapter" => chapter_params}) do | ||
with user <- current_user(conn), | ||
{:ok, chapter} <- Editor.create_chapter(user, conn.assigns[:audio], chapter_params) do | ||
conn | ||
|> put_status(:created) | ||
|> put_resp_header( | ||
"location", | ||
Routes.api_audio_chapters_path(conn, :show, conn.assigns[:audio].id, chapter.start) | ||
) | ||
|> assign(:chapter, chapter) | ||
|> render("show.json") | ||
end | ||
end | ||
|
||
def update(conn, %{"chapter" => chapter_params}) do | ||
with user <- current_user(conn), | ||
{:ok, chapter} <- Editor.update_chapter(user, conn.assigns[:chapter], chapter_params) do | ||
conn | ||
|> assign(:chapter, chapter) | ||
|> render("show.json") | ||
end | ||
end | ||
|
||
def delete(conn, _) do | ||
with user <- current_user(conn), | ||
{:ok, _} <- Editor.delete_chapter(user, conn.assigns[:chapter]) do | ||
send_delete_resp(conn) | ||
else | ||
@not_found_match -> send_delete_resp(conn) | ||
error -> error | ||
end | ||
end | ||
|
||
defp assign_audio(conn, _) do | ||
with {:ok, audio} <- | ||
conn | ||
|> current_user() | ||
|> Editor.get_audio(conn.params["audio_id"]) do | ||
conn | ||
|> assign(:audio, audio) | ||
else | ||
response -> apply_action_fallback(conn, response) | ||
end | ||
end | ||
|
||
defp assign_chapter(conn, _) do | ||
with {:ok, chapter} <- | ||
conn | ||
|> current_user() | ||
|> Editor.get_chapter(conn.assigns[:audio], conn.params["start"]) do | ||
conn | ||
|> assign(:chapter, chapter) | ||
else | ||
response -> apply_action_fallback(conn, response) | ||
end | ||
end | ||
|
||
defp apply_action_fallback(conn, response) do | ||
case @phoenix_fallback do | ||
{:module, module} -> apply(module, :call, [conn, response]) |> halt() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule RadiatorWeb.Api.ChaptersView do | ||
use RadiatorWeb, :view | ||
|
||
alias HAL.{Document, Link} | ||
alias Radiator.AudioMeta.Chapter | ||
|
||
def render("show.json", assigns) do | ||
render(__MODULE__, "chapter.json", assigns) | ||
end | ||
|
||
def render("chapter.json", %{conn: conn, chapter: chapter, audio: audio}) do | ||
%Document{} | ||
|> Document.add_link(%Link{ | ||
rel: "self", | ||
href: Routes.api_audio_chapters_path(conn, :show, audio.id, chapter.start) | ||
}) | ||
|> Document.add_properties(%{ | ||
audio_id: chapter.audio_id, | ||
start: chapter.start, | ||
title: chapter.title, | ||
link: chapter.link, | ||
image: Chapter.image_url(chapter) | ||
}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.