aboutsummaryrefslogtreecommitdiff
path: root/web
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--web/controllers/audio_file_api_controller.ex28
-rw-r--r--web/controllers/audio_file_controller.ex28
2 files changed, 48 insertions, 8 deletions
diff --git a/web/controllers/audio_file_api_controller.ex b/web/controllers/audio_file_api_controller.ex
index 3dc278e..0ba0b54 100644
--- a/web/controllers/audio_file_api_controller.ex
+++ b/web/controllers/audio_file_api_controller.ex
@@ -11,11 +11,31 @@ defmodule TheTranscriberBackend.AudioFileAPIController do
def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "audio_name" => audio_name}}) do
- query = "select nextval('audio_file_id_seq')"
- result = Ecto.Adapters.SQL.query!(Repo, query, [])
- [[repo_last_id]] = result.rows # A beautiful pattern match :)
+ 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
- path = "/media/phoenix_test/#{repo_last_id}_#{upload.filename}"
+ # 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)
changeset = AudioFile.changeset(%AudioFile{},
diff --git a/web/controllers/audio_file_controller.ex b/web/controllers/audio_file_controller.ex
index c4e1af0..e0f32e6 100644
--- a/web/controllers/audio_file_controller.ex
+++ b/web/controllers/audio_file_controller.ex
@@ -15,11 +15,31 @@ defmodule TheTranscriberBackend.AudioFileController do
def create(conn, %{"audio_file" => %{"audio_duration" => audio_duration, "audio_path" => upload, "audio_name" => audio_name}}) do
- query = "select nextval('audio_file_id_seq')"
- result = Ecto.Adapters.SQL.query!(Repo, query, [])
- [[repo_last_id]] = result.rows # A beautiful pattern match :)
+ 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}"
+ path = "/media/phoenix_test/#{repo_last_id}_#{upload.filename}"
File.cp(upload.path, path)
changeset = AudioFile.changeset(%AudioFile{},