Browse Source

add links

main
Inhji Y. 9 months ago
parent
commit
d4b32f737a
  1. 3
      lib/mirage/notes.ex
  2. 20
      lib/mirage/notes/link.ex
  3. 1
      lib/mirage/notes/note.ex
  4. 15
      priv/repo/migrations/20210203075035_create_note_links.exs

3
lib/mirage/notes.ex

@ -22,6 +22,7 @@ defmodule Mirage.Notes do
|> limit(10)
|> order_by(desc: :inserted_at)
|> Repo.all()
|> Repo.preload(:links)
end
@doc """
@ -38,7 +39,7 @@ defmodule Mirage.Notes do
** (Ecto.NoResultsError)
"""
def get_note!(id), do: Repo.get!(Note, id)
def get_note!(id), do: Repo.get!(Note, id) |> Repo.preload(:links)
@doc """
Creates a note.

20
lib/mirage/notes/link.ex

@ -0,0 +1,20 @@
defmodule Mirage.Notes.Link do
use Ecto.Schema
import Ecto.Changeset
schema "note_links" do
field :title, :string
field :url, :string
belongs_to :note, Mirage.Notes.Note
timestamps()
end
@doc false
def changeset(link, attrs) do
link
|> cast(attrs, [:title, :url])
|> validate_required([:title, :url])
end
end

1
lib/mirage/notes/note.ex

@ -6,6 +6,7 @@ defmodule Mirage.Notes.Note do
field :title, :string
field :content, :string
has_many :links, Mirage.Notes.Link
timestamps()
end

15
priv/repo/migrations/20210203075035_create_note_links.exs

@ -0,0 +1,15 @@
defmodule Mirage.Repo.Migrations.CreateNoteLinks do
use Ecto.Migration
def change do
create table(:note_links) do
add :title, :string
add :url, :string
add :note_id, references(:notes, on_delete: :nothing)
timestamps()
end
create index(:note_links, [:note_id])
end
end
Loading…
Cancel
Save