Skip to content

Commit

Permalink
feat: retrieving code
Browse files Browse the repository at this point in the history
  • Loading branch information
juanzeen committed Nov 10, 2024
1 parent e6b39fa commit 0e0f4e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/pescarte/blog/entity/tag.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ defmodule Pescarte.Blog.Entity.Tag do
Pescarte.Database.fetch_by(Tag, nome: nome)
end

@spec upsert_tag(list(Tag.t())) :: {:ok, Tag.t()}
def upsert_tag(tags) do
for t <- tags do
Repo.insert!(
%Tag{nome: t},
on_conflict: [set: [nome: t]],
conflict_target: :nome
)
end
end

@spec create_tag(map()) :: {:ok, Tag.t()} | {:error, changeset}
def create_tag(attrs) do
%Tag{}
Expand Down
17 changes: 13 additions & 4 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 Ecto.Multi
alias Pescarte.Blog.Entity.Tag
alias Pescarte.Database
alias Pescarte.Database.Repo
Expand Down Expand Up @@ -54,11 +55,19 @@ 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()
# end

@spec create_post(Post.t()) :: {:ok, Post.t()} | {:error, Ecto.Changeset.t()}
def create_post(params) do
%Post{}
|> Post.changeset(params)
|> Repo.insert()
def create_post(%Post{blog_tags: tags} = params) do
Multi.new()
|> Multi.update(:blog_tag, Tag.upsert_tag(tags))
# fazer upload da capa da imagem via o Client da Supabase Storage a ser criado
|> Multi.insert(:blog_post, Post.changeset(%Post{}, params))
end

@spec delete_post(String.t()) :: {:ok, Post.t()} | {:error, :not_found}
Expand Down

0 comments on commit 0e0f4e0

Please sign in to comment.