aboutsummaryrefslogtreecommitdiff
path: root/web/controllers
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2017-02-13 13:17:10 +0100
committerneodarz <neodarz@neodarz.net>2017-02-13 13:17:10 +0100
commit1747a25c0c6eb6eab50a194da6ecef25f4fb5c9e (patch)
tree8338caf50b4d28e0452203dd1ddbcfd730beef75 /web/controllers
parent47f6e0792b6e4769fb11072e40271ba7e78a9c29 (diff)
downloadthe_transcriber_backend-1747a25c0c6eb6eab50a194da6ecef25f4fb5c9e.tar.xz
the_transcriber_backend-1747a25c0c6eb6eab50a194da6ecef25f4fb5c9e.zip
Add fix for the id when you add the first entry in the database
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{},