From 64e3c52421c1db729773a4d8fe556a517ddee3f0 Mon Sep 17 00:00:00 2001 From: zoeyrinha Date: Fri, 20 Dec 2024 07:25:28 -0300 Subject: [PATCH 1/6] Revert "Revert "Feat/core create post (#234)" (#245)" This reverts commit 30beabd9bd0b53984ade223e1479951ef685f3c8. --- lib/pescarte/blog/entity/tag.ex | 2 ++ lib/pescarte/blog/post.ex | 33 +++++++++++-------- lib/pescarte/identidades/models/usuario.ex | 2 +- .../20241025203509_create_blog_posts.exs | 6 ++-- ...1122115106_create_posts_tags_relation.exs} | 3 +- 5 files changed, 27 insertions(+), 19 deletions(-) rename priv/repo/migrations/{20241025203733_create_post_tag.exs => 20241122115106_create_posts_tags_relation.exs} (72%) diff --git a/lib/pescarte/blog/entity/tag.ex b/lib/pescarte/blog/entity/tag.ex index d42a6d2a..71cb09e3 100644 --- a/lib/pescarte/blog/entity/tag.ex +++ b/lib/pescarte/blog/entity/tag.ex @@ -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} @@ -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 diff --git a/lib/pescarte/blog/post.ex b/lib/pescarte/blog/post.ex index 41cbbf58..fd6dbc03 100644 --- a/lib/pescarte/blog/post.ex +++ b/lib/pescarte/blog/post.ex @@ -2,6 +2,8 @@ defmodule Pescarte.Blog.Post do @moduledoc """ Módulo que define o schema e o changeset para os posts. """ + alias Ecto.Multi + alias Pescarte.Blog.Entity.Tag alias Pescarte.Database alias Pescarte.Database.Repo alias Pescarte.Database.Types.PublicId @@ -16,33 +18,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.local_now()) |> 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 @@ -55,11 +56,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} diff --git a/lib/pescarte/identidades/models/usuario.ex b/lib/pescarte/identidades/models/usuario.ex index b252d76e..9809baa0 100644 --- a/lib/pescarte/identidades/models/usuario.ex +++ b/lib/pescarte/identidades/models/usuario.ex @@ -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 diff --git a/priv/repo/migrations/20241025203509_create_blog_posts.exs b/priv/repo/migrations/20241025203509_create_blog_posts.exs index b4db4bb7..5736ab38 100644 --- a/priv/repo/migrations/20241025203509_create_blog_posts.exs +++ b/priv/repo/migrations/20241025203509_create_blog_posts.exs @@ -1,10 +1,11 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePost do + alias Pescarte.Database.Types.PublicId use Ecto.Migration def change do - create table(:blog_posts, primary_key: false) 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 :usuario_id, references(:usuario, type: :string) add :titulo, :string add :conteudo, :binary add :link_imagem_capa, :string @@ -14,6 +15,5 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePost do end create unique_index(:blog_posts, :titulo) - create index(:blog_posts, :user_id) end end diff --git a/priv/repo/migrations/20241025203733_create_post_tag.exs b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs similarity index 72% rename from priv/repo/migrations/20241025203733_create_post_tag.exs rename to priv/repo/migrations/20241122115106_create_posts_tags_relation.exs index 748048e3..035f4ca4 100644 --- a/priv/repo/migrations/20241025203733_create_post_tag.exs +++ b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs @@ -2,7 +2,8 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePostTags do use Ecto.Migration def change do - create table(:post_tag) do + create table(:posts_tags_relation, primary_key: :false) do + add :id, :string, primary_key: :true add :tag_id, references(:blog_tag, type: :string), null: false add :post_id, references(:blog_posts, type: :string), null: false timestamps() From 005f10f9e240dbd293212c72153cc95ee83d7034 Mon Sep 17 00:00:00 2001 From: Zoey de Souza Pessanha Date: Fri, 20 Dec 2024 07:36:51 -0300 Subject: [PATCH 2/6] correctly renames blog_posts column --- .../migrations/20241025203509_create_blog_posts.exs | 12 ++++++------ .../20241220102712_rename_blog_post_relation.exs | 9 +++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 priv/repo/migrations/20241220102712_rename_blog_post_relation.exs diff --git a/priv/repo/migrations/20241025203509_create_blog_posts.exs b/priv/repo/migrations/20241025203509_create_blog_posts.exs index 5736ab38..8bf88c75 100644 --- a/priv/repo/migrations/20241025203509_create_blog_posts.exs +++ b/priv/repo/migrations/20241025203509_create_blog_posts.exs @@ -1,19 +1,19 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePost do - alias Pescarte.Database.Types.PublicId use Ecto.Migration def change do - create table(:blog_posts, primary_key: :false) do + create table(:blog_posts, primary_key: false) do add :id, :string, primary_key: true - add :usuario_id, references(:usuario, type: :string) 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) -end + create unique_index(:blog_posts, :titulo) + create index(:blog_posts, :user_id) + end end diff --git a/priv/repo/migrations/20241220102712_rename_blog_post_relation.exs b/priv/repo/migrations/20241220102712_rename_blog_post_relation.exs new file mode 100644 index 00000000..ee5a116f --- /dev/null +++ b/priv/repo/migrations/20241220102712_rename_blog_post_relation.exs @@ -0,0 +1,9 @@ +defmodule Pescarte.Database.Repo.Migrations.RenameBlogPostRelation do + use Ecto.Migration + + def change do + rename table(:blog_posts), :user_id, to: :usuario_id + drop_if_exists index(:blog_posts, [:user_id]) + create index(:blog_posts, [:usuario_id]) + end +end From b9b5331441943cba24da28a03af831830f25d4ac Mon Sep 17 00:00:00 2001 From: Zoey de Souza Pessanha Date: Fri, 20 Dec 2024 07:37:03 -0300 Subject: [PATCH 3/6] formatter --- .formatter.exs | 5 +++-- priv/repo/migrations/20240407235836_create_tag.exs | 2 +- priv/repo/migrations/20240814202453_create_blog_tag.exs | 1 + .../migrations/20241122115106_create_posts_tags_relation.exs | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.formatter.exs b/.formatter.exs index 8f31af25..6870baa4 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -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"] ] diff --git a/priv/repo/migrations/20240407235836_create_tag.exs b/priv/repo/migrations/20240407235836_create_tag.exs index 84e2bb65..7029a48b 100644 --- a/priv/repo/migrations/20240407235836_create_tag.exs +++ b/priv/repo/migrations/20240407235836_create_tag.exs @@ -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 diff --git a/priv/repo/migrations/20240814202453_create_blog_tag.exs b/priv/repo/migrations/20240814202453_create_blog_tag.exs index a0d86b0d..10fe0e73 100644 --- a/priv/repo/migrations/20240814202453_create_blog_tag.exs +++ b/priv/repo/migrations/20240814202453_create_blog_tag.exs @@ -8,6 +8,7 @@ defmodule Pescarte.Database.Repo.Migrations.CreateBlogTag do timestamps() end + create unique_index(:blog_tag, [:nome]) end end diff --git a/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs index 035f4ca4..b96fd3fc 100644 --- a/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs +++ b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs @@ -2,8 +2,8 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePostTags do use Ecto.Migration def change do - create table(:posts_tags_relation, primary_key: :false) do - add :id, :string, primary_key: :true + create table(:posts_tags_relation, primary_key: false) do + add :id, :string, primary_key: true add :tag_id, references(:blog_tag, type: :string), null: false add :post_id, references(:blog_posts, type: :string), null: false timestamps() From b8c3e12178c0d1918c1c9e5123d6f27e1d10fc40 Mon Sep 17 00:00:00 2001 From: Zoey de Souza Pessanha Date: Fri, 20 Dec 2024 07:59:34 -0300 Subject: [PATCH 4/6] minor fixes to migrations --- lib/pescarte/blog/post.ex | 3 ++- .../20240424025541_retype-_external_customer_id_type.exs | 4 +++- .../migrations/20241122115106_create_posts_tags_relation.exs | 3 +-- .../migrations/20241220102712_rename_blog_post_relation.exs | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/pescarte/blog/post.ex b/lib/pescarte/blog/post.ex index fd6dbc03..cbd5f814 100644 --- a/lib/pescarte/blog/post.ex +++ b/lib/pescarte/blog/post.ex @@ -2,13 +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(), diff --git a/priv/repo/migrations/20240424025541_retype-_external_customer_id_type.exs b/priv/repo/migrations/20240424025541_retype-_external_customer_id_type.exs index d05bd4c1..c06bba78 100644 --- a/priv/repo/migrations/20240424025541_retype-_external_customer_id_type.exs +++ b/priv/repo/migrations/20240424025541_retype-_external_customer_id_type.exs @@ -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 diff --git a/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs index b96fd3fc..39578ee4 100644 --- a/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs +++ b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs @@ -2,8 +2,7 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePostTags do use Ecto.Migration def change do - create table(:posts_tags_relation, primary_key: false) do - add :id, :string, primary_key: true + create table(:posts_tags) do add :tag_id, references(:blog_tag, type: :string), null: false add :post_id, references(:blog_posts, type: :string), null: false timestamps() diff --git a/priv/repo/migrations/20241220102712_rename_blog_post_relation.exs b/priv/repo/migrations/20241220102712_rename_blog_post_relation.exs index ee5a116f..ec9885ee 100644 --- a/priv/repo/migrations/20241220102712_rename_blog_post_relation.exs +++ b/priv/repo/migrations/20241220102712_rename_blog_post_relation.exs @@ -2,8 +2,8 @@ defmodule Pescarte.Database.Repo.Migrations.RenameBlogPostRelation do use Ecto.Migration def change do - rename table(:blog_posts), :user_id, to: :usuario_id 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 From 7735347a5e96b2161f03bbbab9255d60661dd541 Mon Sep 17 00:00:00 2001 From: zoeyrinha Date: Fri, 20 Dec 2024 08:01:34 -0300 Subject: [PATCH 5/6] Update post.ex --- lib/pescarte/blog/post.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pescarte/blog/post.ex b/lib/pescarte/blog/post.ex index cbd5f814..677d4e9d 100644 --- a/lib/pescarte/blog/post.ex +++ b/lib/pescarte/blog/post.ex @@ -40,7 +40,7 @@ defmodule Pescarte.Blog.Post do @spec changeset(Post.t(), map) :: changeset def changeset(post \\ %Post{}, params) do post - |> Map.put(:published_at, NaiveDateTime.local_now()) + |> Map.put(:published_at, NaiveDateTime.utc_now(:second)) |> cast(params, @required_params) |> validate_required(@required_params) |> unique_constraint(:titulo) From 55f1edcd04db456b83024f7adca98c20ac576cbf Mon Sep 17 00:00:00 2001 From: Zoey de Souza Pessanha Date: Fri, 20 Dec 2024 08:06:22 -0300 Subject: [PATCH 6/6] correctly rename N-N posts_tags --- .../20241122115106_create_posts_tags_relation.exs | 2 +- .../migrations/20241220110342_rename_post_tag_table.exs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20241220110342_rename_post_tag_table.exs diff --git a/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs index 39578ee4..50cff9d3 100644 --- a/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs +++ b/priv/repo/migrations/20241122115106_create_posts_tags_relation.exs @@ -2,7 +2,7 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePostTags do use Ecto.Migration def change do - create table(:posts_tags) 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() diff --git a/priv/repo/migrations/20241220110342_rename_post_tag_table.exs b/priv/repo/migrations/20241220110342_rename_post_tag_table.exs new file mode 100644 index 00000000..45597da0 --- /dev/null +++ b/priv/repo/migrations/20241220110342_rename_post_tag_table.exs @@ -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