Skip to content

Commit

Permalink
Revert "Feat/core create post (#234)" (#245)
Browse files Browse the repository at this point in the history
This reverts commit 8cb6b44.
  • Loading branch information
zoedsoupe authored Dec 20, 2024
1 parent 8cb6b44 commit 30beabd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
2 changes: 0 additions & 2 deletions lib/pescarte/blog/entity/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 @@ -15,7 +14,6 @@ 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
33 changes: 14 additions & 19 deletions lib/pescarte/blog/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ 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
Expand All @@ -18,32 +16,33 @@ defmodule Pescarte.Blog.Post do
published_at: NaiveDateTime.t(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t(),
usuario: Usuario.t()
usuario: Usuario.t(),
usuario_id: String.t()
}

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

@primary_key {:id, PublicId, autogenerate: true}
schema "blog_posts" do
schema "posts" do
field :titulo, :string
field :conteudo, :binary
field :link_imagem_capa, :string
field :published_at, :naive_datetime

belongs_to :usuario, Usuario, type: :string
many_to_many :blog_tags, Tag, join_through: "posts_tags"
belongs_to :usuario, Usuario

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

timestamps()
end

@spec changeset(Post.t(), map) :: changeset
@spec changeset(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
Expand All @@ -56,15 +55,11 @@ defmodule Pescarte.Blog.Post do
Database.fetch(Post, id)
end

@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()
@spec create_post(Post.t()) :: {:ok, Post.t()} | {:error, Ecto.Changeset.t()}
def create_post(params) do
%Post{}
|> Post.changeset(params)
|> Repo.insert()
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 :blog_posts, Post
has_many :posts, Post

timestamps()
end
Expand Down
6 changes: 3 additions & 3 deletions priv/repo/migrations/20241025203509_create_blog_posts.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
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 :user_id, references(:usuario, type: :string), null: false
add :titulo, :string
add :conteudo, :binary
add :link_imagem_capa, :string
Expand All @@ -15,5 +14,6 @@ defmodule Pescarte.Database.Repo.Migrations.CreatePost do
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,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(:post_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

0 comments on commit 30beabd

Please sign in to comment.