aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/controllers/audio_file_api_controller_test.exs60
-rw-r--r--test/models/audio_file_api_test.exs18
-rw-r--r--web/controllers/audio_file_api_controller.ex67
-rw-r--r--web/controllers/audio_file_controller.ex2
-rw-r--r--web/models/audio_file_api.ex23
-rw-r--r--web/router.ex13
-rw-r--r--web/views/audio_file_api_view.ex20
-rw-r--r--web/views/audio_file_view.ex5
-rw-r--r--web/views/changeset_view.ex19
-rw-r--r--web/web.ex1
10 files changed, 222 insertions, 6 deletions
diff --git a/test/controllers/audio_file_api_controller_test.exs b/test/controllers/audio_file_api_controller_test.exs
new file mode 100644
index 0000000..af08cd7
--- /dev/null
+++ b/test/controllers/audio_file_api_controller_test.exs
@@ -0,0 +1,60 @@
+defmodule TheTranscriberBackend.AudioFileAPIControllerTest do
+ use TheTranscriberBackend.ConnCase
+
+ alias TheTranscriberBackend.AudioFileAPI
+ @valid_attrs %{}
+ @invalid_attrs %{}
+
+ setup %{conn: conn} do
+ {:ok, conn: put_req_header(conn, "accept", "application/json")}
+ end
+
+ test "lists all entries on index", %{conn: conn} do
+ conn = get conn, audio_file_api_path(conn, :index)
+ assert json_response(conn, 200)["data"] == []
+ end
+
+ test "shows chosen resource", %{conn: conn} do
+ audio_file_api = Repo.insert! %AudioFileAPI{}
+ conn = get conn, audio_file_api_path(conn, :show, audio_file_api)
+ assert json_response(conn, 200)["data"] == %{"id" => audio_file_api.id,
+ "audio_path_id" => audio_file_api.audio_path_id}
+ end
+
+ test "renders page not found when id is nonexistent", %{conn: conn} do
+ assert_error_sent 404, fn ->
+ get conn, audio_file_api_path(conn, :show, -1)
+ end
+ end
+
+ test "creates and renders resource when data is valid", %{conn: conn} do
+ conn = post conn, audio_file_api_path(conn, :create), audio_file_api: @valid_attrs
+ assert json_response(conn, 201)["data"]["id"]
+ assert Repo.get_by(AudioFileAPI, @valid_attrs)
+ end
+
+ test "does not create resource and renders errors when data is invalid", %{conn: conn} do
+ conn = post conn, audio_file_api_path(conn, :create), audio_file_api: @invalid_attrs
+ assert json_response(conn, 422)["errors"] != %{}
+ end
+
+ test "updates and renders chosen resource when data is valid", %{conn: conn} do
+ audio_file_api = Repo.insert! %AudioFileAPI{}
+ conn = put conn, audio_file_api_path(conn, :update, audio_file_api), audio_file_api: @valid_attrs
+ assert json_response(conn, 200)["data"]["id"]
+ assert Repo.get_by(AudioFileAPI, @valid_attrs)
+ end
+
+ test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
+ audio_file_api = Repo.insert! %AudioFileAPI{}
+ conn = put conn, audio_file_api_path(conn, :update, audio_file_api), audio_file_api: @invalid_attrs
+ assert json_response(conn, 422)["errors"] != %{}
+ end
+
+ test "deletes chosen resource", %{conn: conn} do
+ audio_file_api = Repo.insert! %AudioFileAPI{}
+ conn = delete conn, audio_file_api_path(conn, :delete, audio_file_api)
+ assert response(conn, 204)
+ refute Repo.get(AudioFileAPI, audio_file_api.id)
+ end
+end
diff --git a/test/models/audio_file_api_test.exs b/test/models/audio_file_api_test.exs
new file mode 100644
index 0000000..028befa
--- /dev/null
+++ b/test/models/audio_file_api_test.exs
@@ -0,0 +1,18 @@
+defmodule TheTranscriberBackend.AudioFileAPITest do
+ use TheTranscriberBackend.ModelCase
+
+ alias TheTranscriberBackend.AudioFileAPI
+
+ @valid_attrs %{}
+ @invalid_attrs %{}
+
+ test "changeset with valid attributes" do
+ changeset = AudioFileAPI.changeset(%AudioFileAPI{}, @valid_attrs)
+ assert changeset.valid?
+ end
+
+ test "changeset with invalid attributes" do
+ changeset = AudioFileAPI.changeset(%AudioFileAPI{}, @invalid_attrs)
+ refute changeset.valid?
+ end
+end
diff --git a/web/controllers/audio_file_api_controller.ex b/web/controllers/audio_file_api_controller.ex
new file mode 100644
index 0000000..65b968c
--- /dev/null
+++ b/web/controllers/audio_file_api_controller.ex
@@ -0,0 +1,67 @@
+defmodule TheTranscriberBackend.AudioFileAPIController do
+ use TheTranscriberBackend.Web, :controller
+
+ alias TheTranscriberBackend.AudioFileAPI
+ alias TheTranscriberBackend.AudioFile
+
+ def index(conn, _params) do
+ audio_file_api = Repo.all(AudioFile)
+ render(conn, "index.json", audio_file_api: audio_file_api)
+ end
+
+ def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "transcription_file_path" => transcription_file_path}}) do
+
+ repo_last_id = (Repo.one(from x in AudioFileAPI, order_by: [desc: x.id], limit: 1)).id
+
+ path = "/media/phoenix_test/#{repo_last_id}_#{upload.filename}"
+ File.cp(upload.path, path)
+
+ changeset = AudioFile.changeset(%AudioFile{},
+ %{audio_path: path,
+ transcription_file_path: transcription_file_path,
+ audio_duration: audio_duration})
+
+ IO.inspect path
+
+ case Repo.insert(changeset) do
+ {:ok, audio_file_api} ->
+ conn
+ |> put_status(:created)
+ |> put_resp_header("location", audio_file_api_path(conn, :show, audio_file_api))
+ |> render("show.json", audio_file_api: audio_file_api)
+ {:error, changeset} ->
+ conn
+ |> put_status(:unprocessable_entity)
+ |> render(TheTranscriberBackend.ChangesetView, "error.json", changeset: changeset)
+ end
+ end
+
+ def show(conn, %{"id" => id}) do
+ audio_file_api = Repo.get!(AudioFileAPI, id)
+ render(conn, "show.json", audio_file_api: audio_file_api)
+ end
+
+ def update(conn, %{"id" => id, "audio_file_api" => audio_file_api_params}) do
+ audio_file_api = Repo.get!(AudioFileAPI, id)
+ changeset = AudioFileAPI.changeset(audio_file_api, audio_file_api_params)
+
+ case Repo.update(changeset) do
+ {:ok, audio_file_api} ->
+ render(conn, "show.json", audio_file_api: audio_file_api)
+ {:error, changeset} ->
+ conn
+ |> put_status(:unprocessable_entity)
+ |> render(TheTranscriberBackend.ChangesetView, "error.json", changeset: changeset)
+ end
+ end
+
+ def delete(conn, %{"id" => id}) do
+ audio_file_api = Repo.get!(AudioFileAPI, id)
+
+ # Here we use delete! (with a bang) because we expect
+ # it to always work (and if it does not, it will raise).
+ Repo.delete!(audio_file_api)
+
+ send_resp(conn, :no_content, "")
+ end
+end
diff --git a/web/controllers/audio_file_controller.ex b/web/controllers/audio_file_controller.ex
index db90da7..1c2acb9 100644
--- a/web/controllers/audio_file_controller.ex
+++ b/web/controllers/audio_file_controller.ex
@@ -23,8 +23,6 @@ defmodule TheTranscriberBackend.AudioFileController do
transcription_file_path: transcription_file_path,
audio_duration: audio_duration})
- IO.inspect changeset
-
case Repo.insert(changeset) do
{:ok, _audio_file} ->
conn
diff --git a/web/models/audio_file_api.ex b/web/models/audio_file_api.ex
new file mode 100644
index 0000000..15f2e42
--- /dev/null
+++ b/web/models/audio_file_api.ex
@@ -0,0 +1,23 @@
+defmodule TheTranscriberBackend.AudioFileAPI do
+ use TheTranscriberBackend.Web, :model
+
+ #schema "audio_file_api" do
+ #belongs_to :audio_path, TheTranscriberBackend.AudioPath
+
+ schema "audio_file" do
+ field :audio_path, :string
+ field :transcription_file_path, :string
+ field :audio_duration, :string
+
+ timestamps()
+ end
+
+ @doc """
+ Builds a changeset based on the `struct` and `params`.
+ """
+ def changeset(struct, params \\ %{}) do
+ struct
+ |> cast(params, [])
+ |> validate_required([])
+ end
+end
diff --git a/web/router.ex b/web/router.ex
index 453b1f7..0fd11d5 100644
--- a/web/router.ex
+++ b/web/router.ex
@@ -5,7 +5,7 @@ defmodule TheTranscriberBackend.Router do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
- plug :protect_from_forgery
+ #plug :protect_from_forgery
plug :put_secure_browser_headers
end
@@ -18,10 +18,15 @@ defmodule TheTranscriberBackend.Router do
get "/", PageController, :index
resources "/audio_file", AudioFileController
+
+ post "/", PageController, :index
+ resources "/audio_file", AudioFileController
end
# Other scopes may use custom stacks.
- # scope "/api", TheTranscriberBackend do
- # pipe_through :api
- # end
+ scope "/api", TheTranscriberBackend do
+ pipe_through :api
+
+ resources "/audio_file", AudioFileAPIController
+ end
end
diff --git a/web/views/audio_file_api_view.ex b/web/views/audio_file_api_view.ex
new file mode 100644
index 0000000..f5dbba9
--- /dev/null
+++ b/web/views/audio_file_api_view.ex
@@ -0,0 +1,20 @@
+defmodule TheTranscriberBackend.AudioFileAPIView do
+ use TheTranscriberBackend.Web, :view
+
+
+
+ def render("index.json", %{audio_file_api: audio_file_api}) do
+ %{data: render_many(audio_file_api, TheTranscriberBackend.AudioFileAPIView, "audio_file_api.json")}
+ end
+
+ def render("show.json", %{audio_file_api: audio_file_api}) do
+ %{data: render_one(audio_file_api, TheTranscriberBackend.AudioFileAPIView, "audio_file_api.json")}
+ end
+
+ def render("audio_file_api.json", %{audio_file_api: audio_file_api}) do
+ %{id: audio_file_api.id,
+ audio_path: audio_file_api.audio_path,
+ audio_transcription_file_path: audio_file_api.transcription_file_path,
+ audio_duration: audio_file_api.audio_duration}
+ end
+end
diff --git a/web/views/audio_file_view.ex b/web/views/audio_file_view.ex
index b2b2c26..8cefe83 100644
--- a/web/views/audio_file_view.ex
+++ b/web/views/audio_file_view.ex
@@ -1,3 +1,8 @@
defmodule TheTranscriberBackend.AudioFileView do
use TheTranscriberBackend.Web, :view
+
+ #attributes [:id, :audio_path, :transcription_file_path, :audio_duration]
+
+ #has_many :audio_file_api, link
+
end
diff --git a/web/views/changeset_view.ex b/web/views/changeset_view.ex
new file mode 100644
index 0000000..767756e
--- /dev/null
+++ b/web/views/changeset_view.ex
@@ -0,0 +1,19 @@
+defmodule TheTranscriberBackend.ChangesetView do
+ use TheTranscriberBackend.Web, :view
+
+ @doc """
+ Traverses and translates changeset errors.
+
+ See `Ecto.Changeset.traverse_errors/2` and
+ `TheTranscriberBackend.ErrorHelpers.translate_error/1` for more details.
+ """
+ def translate_errors(changeset) do
+ Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
+ end
+
+ def render("error.json", %{changeset: changeset}) do
+ # When encoded, the changeset returns its errors
+ # as a JSON object. So we just pass it forward.
+ %{errors: translate_errors(changeset)}
+ end
+end
diff --git a/web/web.ex b/web/web.ex
index e919778..f04ce9d 100644
--- a/web/web.ex
+++ b/web/web.ex
@@ -42,6 +42,7 @@ defmodule TheTranscriberBackend.Web do
def view do
quote do
use Phoenix.View, root: "web/templates"
+ #use JaSerializer.PhoenixView
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]