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

Corrigindo migrations de post/tag #246

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Used by "mix format"
[
import_deps: [:phoenix, :ecto],
import_deps: [:phoenix, :ecto, :ecto_sql],
subdirectories: ["priv/*/migrations"],
plugins: [Phoenix.LiveView.HTMLFormatter],
inputs: ["*.{ex,exs,heex}", "{lib,test}/**/*.{ex,exs,heex}"]
inputs: ["*.{ex,exs,heex}", "{config,lib,test}/**/*.{ex,exs,heex}", "priv/*/seeds.exs"]
]
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 :blog_posts, Post, join_through: "posts_tags"

timestamps()
end
Expand Down
36 changes: 21 additions & 15 deletions lib/pescarte/blog/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ defmodule Pescarte.Blog.Post do
@moduledoc """
Módulo que define o schema e o changeset para os posts.
"""
use Pescarte, :model

alias Ecto.Multi
alias Pescarte.Blog.Entity.Tag
alias Pescarte.Database
alias Pescarte.Database.Repo
alias Pescarte.Database.Types.PublicId
alias Pescarte.Identidades.Models.Usuario
use Pescarte, :model

@type t :: %Post{
id: String.t(),
Expand All @@ -16,33 +19,32 @@ defmodule Pescarte.Blog.Post do
published_at: NaiveDateTime.t(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t(),
usuario: Usuario.t(),
usuario_id: String.t()
usuario: Usuario.t()
}

@required_params [:titulo, :conteudo, :link_imagem_capa, :published_at]
@required_params [:titulo, :conteudo, :link_imagem_capa, :published_at, :usuario_id]

@primary_key {:id, PublicId, autogenerate: true}
schema "posts" do
schema "blog_posts" do
field :titulo, :string
field :conteudo, :binary
field :link_imagem_capa, :string
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]
belongs_to :usuario, Usuario, type: :string
many_to_many :blog_tags, Tag, join_through: "posts_tags"

timestamps()
end

@spec changeset(t, map) :: changeset
@spec changeset(Post.t(), map) :: changeset
def changeset(post \\ %Post{}, params) do
post
|> Map.put(:published_at, NaiveDateTime.utc_now(:second))
|> cast(params, @required_params)
|> validate_required(@required_params)
|> unique_constraint(:titulo)
|> foreign_key_constraint(:usuario_id)
end

@spec get_posts :: list(Post.t()) | Ecto.QueryError
Expand All @@ -55,11 +57,15 @@ defmodule Pescarte.Blog.Post do
Database.fetch(Post, id)
end

@spec create_post(Post.t()) :: {:ok, Post.t()} | {:error, Ecto.Changeset.t()}
def create_post(params) do
%Post{}
|> Post.changeset(params)
|> Repo.insert()
@spec create_post(Map) :: {:ok, Post.t()} | {:error, Ecto.Changeset.t()}
def create_post(%{blog_tags: tags} = params) do
Multi.new()
|> Multi.insert_all(:update_tags, Tag, tags,
on_conflict: :replace_all,
conflict_target: :nome
)
|> Multi.insert(:blog_posts, changeset(%Post{}, params))
|> Repo.transaction()
end

@spec delete_post(String.t()) :: {:ok, Post.t()} | {:error, :not_found}
Expand Down
2 changes: 1 addition & 1 deletion lib/pescarte/identidades/models/usuario.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ defmodule Pescarte.Identidades.Models.Usuario do

belongs_to :contato, Contato, type: :string

has_many :posts, Post
has_many :blog_posts, Post

timestamps()
end
Expand Down
2 changes: 1 addition & 1 deletion priv/repo/migrations/20240407235836_create_tag.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule Pescarte.Database.Repo.Migrations.CreateTag do
create table(:tag, primary_key: false) do
add :id, :string, primary_key: true
add :etiqueta, :string, null: false
add :categoria_id, references(:categoria,type: :string), null: false
add :categoria_id, references(:categoria, type: :string), null: false

timestamps()
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule :"Elixir.Pescarte.Database.Repo.Migrations.Retype-ExternalCustomerIdType" do
defmodule Elixir.Pescarte.Database.Repo.Migrations.RetypeExternalCustomerIdType do
use Ecto.Migration

# ESSA MIGRATION É IRREVERSÍVEL

def change do
alter table(:usuario) do
modify :external_customer_id, :text, from: :binary_id
Expand Down
1 change: 1 addition & 0 deletions priv/repo/migrations/20240814202453_create_blog_tag.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Pescarte.Database.Repo.Migrations.CreateBlogTag do

timestamps()
end

create unique_index(:blog_tag, [:nome])
end
end
10 changes: 5 additions & 5 deletions priv/repo/migrations/20241025203509_create_blog_posts.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePost do
def change do
create table(:blog_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
add :link_imagem_capa, :string
add :published_at, :naive_datetime
add :user_id, references(:usuario, type: :string)

timestamps()
end
end

create unique_index(:blog_posts, :titulo)
create index(:blog_posts, :user_id)
end
create unique_index(:blog_posts, :titulo)
create index(:blog_posts, :user_id)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePostTags do
use Ecto.Migration

def change do
create table(:post_tag) do
create table(:posts_tag) do
add :tag_id, references(:blog_tag, type: :string), null: false
add :post_id, references(:blog_posts, type: :string), null: false
timestamps()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Pescarte.Database.Repo.Migrations.RenameBlogPostRelation do
use Ecto.Migration

def change do
drop_if_exists index(:blog_posts, [:user_id])
rename table(:blog_posts), :user_id, to: :usuario_id
create index(:blog_posts, [:usuario_id])
end
end
7 changes: 7 additions & 0 deletions priv/repo/migrations/20241220110342_rename_post_tag_table.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Pescarte.Database.Repo.Migrations.RenamePostTagTable do
use Ecto.Migration

def change do
rename table(:posts_tag), to: table(:posts_tags)
end
end
Loading