aboutsummaryrefslogtreecommitdiff
path: root/web/controllers/audio_file_api_controller.ex
blob: 0ba0b542aa1a9f90839e26090f25afb7c177f55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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, "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)

    changeset = AudioFile.changeset(%AudioFile{},
      %{audio_path: path,
        audio_name: audio_name,
        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