Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/posttag migration #230

Merged
merged 15 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added git
Empty file.
2 changes: 2 additions & 0 deletions lib/pescarte/blog/entity/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Pescarte.Blog.Entity.Tag do

use Pescarte, :model

alias Pescarte.Blog.Post
alias Pescarte.Database.Types.PublicId

@type t :: %Tag{nome: binary, id: binary}
Expand All @@ -14,6 +15,7 @@ defmodule Pescarte.Blog.Entity.Tag do
@primary_key {:id, PublicId, autogenerate: true}
schema "blog_tag" do
field :nome, :string
many_to_many :posts, Post, join_through: "post_tags"

timestamps()
end
Expand Down
5 changes: 2 additions & 3 deletions lib/pescarte/blog/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Pescarte.Blog.Post do
@moduledoc """
Módulo que define o schema e o changeset para os posts.
"""
alias Pescarte.Blog.Entity.Tag
alias Pescarte.Database
alias Pescarte.Database.Repo
alias Pescarte.Database.Types.PublicId
Expand Down Expand Up @@ -30,9 +31,7 @@ defmodule Pescarte.Blog.Post do
field :published_at, :naive_datetime

belongs_to :usuario, Usuario

# comentado enquanto o PR das tags não é aprovado
# many_to_many :tags, Tag, through: [:post_tags, :tag]
many_to_many :tags, Tag, join_through: "posts_tags"

timestamps()
end
Expand Down
17 changes: 17 additions & 0 deletions lib/pescarte/blog/posts_tags.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Pescarte.Blog.PostsTags do
@moduledoc """
Módulo reponsável pelo relacionamento entre posts e tags. Nesse arquivo é feito o schema que permite
esse relacionamento NxN e também o
"""
alias Pescarte.Blog.Entity.Tag
alias Pescarte.Blog.Post
alias Pescarte.Database.Types.PublicId
use Ecto.Schema

@primary_key {:id, PublicId, autogenerate: true}
schema "posts_tags" do
belongs_to :post, Post
belongs_to :tag, Tag
timestamps()
end
end
3 changes: 2 additions & 1 deletion priv/repo/migrations/20240814205931_create_post.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePost do
use Ecto.Migration

def change do
create table(:posts) do
create table(:posts, primary_key: false) do
add :id, :string, primary_key: true
add :user_id, references(:usuario, type: :string), null: false
add :titulo, :string
add :conteudo, :binary
Expand Down
11 changes: 11 additions & 0 deletions priv/repo/migrations/20241009022246_create_posts_tags.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule Pescarte.Database.Repo.Migrations.CreatePostTags do
use Ecto.Migration

def change do
create table(:posts_tags) do
add :post_id, references(:posts, type: :string), null: false
add :tag_id, references(:blog_tag, type: :string), null: false
timestamps()
end
end
end
Loading