Skip to content

Commit

Permalink
Feat/core create post (#234)
Browse files Browse the repository at this point in the history
* extract common tasks aliases

* feat: schema and migration created

* feat: schema and migration finished

* fix: format

* fix: migration errors

* fix: fixing id in posts table

* fix: fixing tag reference in post_tags migration

* fix: remove posttag schema and rename the migrations

* feat: retrieving code

* problem: cant create post because the user_id is not acceptable by changeset

* fix: trying to remake the project

* fix/ create post w upsert tags

* fix: format and dev settings

* pain

* feat: create core post finally finished

* Update .env.dev

* Delete logfile

* Update tag.ex

* Update post.ex

* do not leave unnecessary comments

---------

Co-authored-by: Zoey de Souza Pessanha <[email protected]>
  • Loading branch information
juanzeen and zoedsoupe authored Dec 20, 2024
1 parent 737b4ae commit 8cb6b44
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
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
33 changes: 19 additions & 14 deletions lib/pescarte/blog/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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}
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
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,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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8cb6b44

Please sign in to comment.