timgremore commited on
Commit
8488f43
1 Parent(s): dc67aef

feat: Generate Transcriptions context and schema

Browse files
lib/medical_transcription/transcriptions.ex ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ defmodule MedicalTranscription.Transcriptions do
2
+ @moduledoc """
3
+ The Transcriptions context.
4
+ """
5
+
6
+ import Ecto.Query, warn: false
7
+ alias MedicalTranscription.Repo
8
+
9
+ alias MedicalTranscription.Transcriptions.Transcription
10
+
11
+ @doc """
12
+ Returns the list of transcriptions.
13
+
14
+ ## Examples
15
+
16
+ iex> list_transcriptions()
17
+ [%Transcription{}, ...]
18
+
19
+ """
20
+ def list_transcriptions do
21
+ Repo.all(Transcription)
22
+ end
23
+
24
+ @doc """
25
+ Gets a single transcription.
26
+
27
+ Raises `Ecto.NoResultsError` if the Transcription does not exist.
28
+
29
+ ## Examples
30
+
31
+ iex> get_transcription!(123)
32
+ %Transcription{}
33
+
34
+ iex> get_transcription!(456)
35
+ ** (Ecto.NoResultsError)
36
+
37
+ """
38
+ def get_transcription!(id), do: Repo.get!(Transcription, id)
39
+
40
+ @doc """
41
+ Creates a transcription.
42
+
43
+ ## Examples
44
+
45
+ iex> create_transcription(%{field: value})
46
+ {:ok, %Transcription{}}
47
+
48
+ iex> create_transcription(%{field: bad_value})
49
+ {:error, %Ecto.Changeset{}}
50
+
51
+ """
52
+ def create_transcription(attrs \\ %{}) do
53
+ %Transcription{}
54
+ |> Transcription.changeset(attrs)
55
+ |> Repo.insert()
56
+ end
57
+
58
+ @doc """
59
+ Updates a transcription.
60
+
61
+ ## Examples
62
+
63
+ iex> update_transcription(transcription, %{field: new_value})
64
+ {:ok, %Transcription{}}
65
+
66
+ iex> update_transcription(transcription, %{field: bad_value})
67
+ {:error, %Ecto.Changeset{}}
68
+
69
+ """
70
+ def update_transcription(%Transcription{} = transcription, attrs) do
71
+ transcription
72
+ |> Transcription.changeset(attrs)
73
+ |> Repo.update()
74
+ end
75
+
76
+ @doc """
77
+ Deletes a transcription.
78
+
79
+ ## Examples
80
+
81
+ iex> delete_transcription(transcription)
82
+ {:ok, %Transcription{}}
83
+
84
+ iex> delete_transcription(transcription)
85
+ {:error, %Ecto.Changeset{}}
86
+
87
+ """
88
+ def delete_transcription(%Transcription{} = transcription) do
89
+ Repo.delete(transcription)
90
+ end
91
+
92
+ @doc """
93
+ Returns an `%Ecto.Changeset{}` for tracking transcription changes.
94
+
95
+ ## Examples
96
+
97
+ iex> change_transcription(transcription)
98
+ %Ecto.Changeset{data: %Transcription{}}
99
+
100
+ """
101
+ def change_transcription(%Transcription{} = transcription, attrs \\ %{}) do
102
+ Transcription.changeset(transcription, attrs)
103
+ end
104
+ end
lib/medical_transcription/transcriptions/transcription.ex ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ defmodule MedicalTranscription.Transcriptions.Transcription do
2
+ @moduledoc """
3
+ Transcription schema
4
+ """
5
+
6
+ use Ecto.Schema
7
+ import Ecto.Changeset
8
+
9
+ @primary_key {:id, :binary_id, autogenerate: true}
10
+ @foreign_key_type :binary_id
11
+ schema "transcriptions" do
12
+ field :filename, :string
13
+ field :recording_length_in_seconds, :integer
14
+
15
+ belongs_to :user, MedicalTranscription.Accounts.User
16
+
17
+ timestamps(type: :utc_datetime)
18
+ end
19
+
20
+ @doc false
21
+ def changeset(transcription, attrs) do
22
+ transcription
23
+ |> cast(attrs, [:user_id, :filename, :recording_length_in_seconds])
24
+ |> validate_required([:user_id, :filename])
25
+ end
26
+ end
priv/repo/migrations/20240202211853_create_transcriptions.exs ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ defmodule MedicalTranscription.Repo.Migrations.CreateTranscriptions do
2
+ use Ecto.Migration
3
+
4
+ def change do
5
+ create table(:transcriptions, primary_key: false) do
6
+ add :id, :binary_id, primary_key: true
7
+ add :filename, :string, null: false
8
+ add :recording_length_in_seconds, :integer, null: true
9
+ add :user_id, references(:users, type: :binary_id, on_delete: :delete_all), null: false
10
+
11
+ timestamps(type: :utc_datetime)
12
+ end
13
+ end
14
+ end
test/medical_transcription/transcriptions_test.exs ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ defmodule MedicalTranscription.TranscriptionsTest do
2
+ use MedicalTranscription.DataCase
3
+
4
+ alias MedicalTranscription.Transcriptions
5
+
6
+ describe "transcriptions" do
7
+ alias MedicalTranscription.Transcriptions.Transcription
8
+
9
+ import MedicalTranscription.TranscriptionsFixtures
10
+ import MedicalTranscription.AccountsFixtures
11
+
12
+ @invalid_attrs %{filename: nil, recording_length_in_seconds: nil}
13
+
14
+ test "list_transcriptions/0 returns all transcriptions" do
15
+ transcription = transcription_fixture()
16
+ assert Transcriptions.list_transcriptions() == [transcription]
17
+ end
18
+
19
+ test "get_transcription!/1 returns the transcription with given id" do
20
+ transcription = transcription_fixture()
21
+ assert Transcriptions.get_transcription!(transcription.id) == transcription
22
+ end
23
+
24
+ test "create_transcription/1 with valid data creates a transcription" do
25
+ user = user_fixture()
26
+
27
+ valid_attrs = %{
28
+ user_id: user.id,
29
+ filename: "some filename",
30
+ recording_length_in_seconds: 42
31
+ }
32
+
33
+ assert {:ok, %Transcription{} = transcription} =
34
+ Transcriptions.create_transcription(valid_attrs)
35
+
36
+ assert transcription.filename == "some filename"
37
+ assert transcription.recording_length_in_seconds == 42
38
+ end
39
+
40
+ test "create_transcription/1 with invalid data returns error changeset" do
41
+ assert {:error, %Ecto.Changeset{}} = Transcriptions.create_transcription(@invalid_attrs)
42
+ end
43
+
44
+ test "update_transcription/2 with valid data updates the transcription" do
45
+ transcription = transcription_fixture()
46
+ update_attrs = %{filename: "some updated filename", recording_length_in_seconds: 43}
47
+
48
+ assert {:ok, %Transcription{} = transcription} =
49
+ Transcriptions.update_transcription(transcription, update_attrs)
50
+
51
+ assert transcription.filename == "some updated filename"
52
+ assert transcription.recording_length_in_seconds == 43
53
+ end
54
+
55
+ test "update_transcription/2 with invalid data returns error changeset" do
56
+ transcription = transcription_fixture()
57
+
58
+ assert {:error, %Ecto.Changeset{}} =
59
+ Transcriptions.update_transcription(transcription, @invalid_attrs)
60
+
61
+ assert transcription == Transcriptions.get_transcription!(transcription.id)
62
+ end
63
+
64
+ test "delete_transcription/1 deletes the transcription" do
65
+ transcription = transcription_fixture()
66
+ assert {:ok, %Transcription{}} = Transcriptions.delete_transcription(transcription)
67
+
68
+ assert_raise Ecto.NoResultsError, fn ->
69
+ Transcriptions.get_transcription!(transcription.id)
70
+ end
71
+ end
72
+
73
+ test "change_transcription/1 returns a transcription changeset" do
74
+ transcription = transcription_fixture()
75
+ assert %Ecto.Changeset{} = Transcriptions.change_transcription(transcription)
76
+ end
77
+ end
78
+ end
test/support/fixtures/transcriptions_fixtures.ex ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ defmodule MedicalTranscription.TranscriptionsFixtures do
2
+ @moduledoc """
3
+ This module defines test helpers for creating
4
+ entities via the `MedicalTranscription.Transcriptions` context.
5
+ """
6
+
7
+ import MedicalTranscription.AccountsFixtures
8
+
9
+ @doc """
10
+ Generate a transcription.
11
+ """
12
+ def transcription_fixture(attrs \\ %{}) do
13
+ user_id =
14
+ cond do
15
+ Map.has_key?(attrs, :user_id) -> attrs.user_id
16
+ Map.has_key?(attrs, :user) -> attrs.user.id
17
+ true -> user_fixture().id
18
+ end
19
+
20
+ {:ok, transcription} =
21
+ attrs
22
+ |> Enum.into(%{
23
+ user_id: user_id,
24
+ filename: "some filename",
25
+ recording_length_in_seconds: 42
26
+ })
27
+ |> MedicalTranscription.Transcriptions.create_transcription()
28
+
29
+ transcription
30
+ end
31
+ end