diff options
author | neodarz <neodarz@neodarz.net> | 2017-02-14 14:52:09 +0100 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2017-02-14 14:52:09 +0100 |
commit | 146ff0f004d2baa9dbdeafacda4bde7dbb7b0e33 (patch) | |
tree | 0abe79d3e3b0a026530f3f0530926b3fff718607 /web | |
parent | b139d6fa3a34b4bff014a7e7ccfa7a7b1f0ffa5f (diff) | |
download | the_transcriber_backend-146ff0f004d2baa9dbdeafacda4bde7dbb7b0e33.tar.xz the_transcriber_backend-146ff0f004d2baa9dbdeafacda4bde7dbb7b0e33.zip |
Rewritting the upload code that copies the file to be uploaded after the insertion in the database
Diffstat (limited to 'web')
-rw-r--r-- | web/controllers/audio_file_api_controller.ex | 33 | ||||
-rw-r--r-- | web/controllers/audio_file_controller.ex | 44 |
2 files changed, 14 insertions, 63 deletions
diff --git a/web/controllers/audio_file_api_controller.ex b/web/controllers/audio_file_api_controller.ex index 0ba0b54..11ae1a7 100644 --- a/web/controllers/audio_file_api_controller.ex +++ b/web/controllers/audio_file_api_controller.ex @@ -10,43 +10,16 @@ defmodule TheTranscriberBackend.AudioFileAPIController do end def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "audio_name" => audio_name}}) do - - query_table_is_empty = "select * from audio_file;" - result_table_is_empty = Ecto.Adapters.SQL.query!(Repo, query_table_is_empty, []) - result_table_is_empty_rows = result_table_is_empty.rows - case [] do - ^result_table_is_empty_rows -> - repo_last_id = 1 - - # Don't ask why, but it would seem that you must select nextval - # BEFORE currval... Yes ! Seriously... - query_nextval = "select nextval('audio_file_id_seq');" - result_nextval = Ecto.Adapters.SQL.query!(Repo, query_nextval, []) - - query_setval = "select setval('audio_file_id_seq', 1);" - result_setval = Ecto.Adapters.SQL.query!(Repo, query_setval, []) - - query_currval= "select currval('audio_file_id_seq');" - result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) - - _ -> - query_currval = "select max(id) from audio_file;" - result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) - [[repo_last_id]] = result_currval.rows # A beautiful pattern match :) - end - - path = "/media/phoenix_test/#{repo_last_id + 1}_#{upload.filename}" - File.cp(upload.path, path) + path = "/media/phoenix_test/" changeset = AudioFile.changeset(%AudioFile{}, - %{audio_path: path, + %{audio_path: upload.filename, audio_name: audio_name, audio_duration: audio_duration}) - IO.inspect path - case Repo.insert(changeset) do {:ok, audio_file_api} -> + File.cp(upload.path, "#{path}#{audio_file_api.id}_#{upload.filename}") conn |> put_status(:created) |> put_resp_header("location", audio_file_api_path(conn, :show, audio_file_api)) diff --git a/web/controllers/audio_file_controller.ex b/web/controllers/audio_file_controller.ex index e0f32e6..95bc763 100644 --- a/web/controllers/audio_file_controller.ex +++ b/web/controllers/audio_file_controller.ex @@ -14,48 +14,26 @@ defmodule TheTranscriberBackend.AudioFileController do end def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "audio_name" => audio_name}}) do - - query_table_is_empty = "select * from audio_file;" - result_table_is_empty = Ecto.Adapters.SQL.query!(Repo, query_table_is_empty, []) - result_table_is_empty_rows = result_table_is_empty.rows - case [] do - ^result_table_is_empty_rows -> - repo_last_id = 1 - - # Don't ask why, but it would seem that you must select nextval - # BEFORE currval... Yes ! Seriously... - query_nextval = "select nextval('audio_file_id_seq');" - result_nextval = Ecto.Adapters.SQL.query!(Repo, query_nextval, []) - - query_setval = "select setval('audio_file_id_seq', 1);" - result_setval = Ecto.Adapters.SQL.query!(Repo, query_setval, []) - - query_currval= "select currval('audio_file_id_seq');" - result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) - - _ -> - query_currval = "select max(id) from audio_file;" - result_currval = Ecto.Adapters.SQL.query!(Repo, query_currval, []) - [[repo_last_id]] = result_currval.rows # A beautiful pattern match :) - end - - path = "/media/phoenix_test/#{repo_last_id}_#{upload.filename}" - File.cp(upload.path, path) + path = "/media/phoenix_test/" changeset = AudioFile.changeset(%AudioFile{}, - %{audio_path: path, + %{audio_path: upload.filename, audio_name: audio_name, audio_duration: audio_duration}) case Repo.insert(changeset) do - {:ok, _audio_file} -> + {:ok, audio_file_api} -> + File.cp(upload.path, "#{path}#{audio_file_api.id}_#{upload.filename}") conn - |> put_flash(:info, "Audio file uploaded successfully.") - |> redirect(to: audio_file_path(conn, :index)) + |> 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} -> - render(conn, "new.html", changeset: changeset) + conn + |> put_status(:unprocessable_entity) + |> render(TheTranscriberBackend.ChangesetView, "error.json", changeset: changeset) + end end - end |