-
Notifications
You must be signed in to change notification settings - Fork 12
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
Feat/blog tag schema #185
Feat/blog tag schema #185
Changes from 6 commits
16d233d
f64dffe
cd43ada
5070208
b32867e
34eea36
32159a3
6ab7fd2
8c3dfd0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule Pescarte.Blog.Blog do | ||
@moduledoc """ | ||
O contexto Blog é responsável por gerenciar as operações relacionadas aos Posts e as Tags. | ||
""" | ||
|
||
alias Pescarte.Blog.Entity.Tag | ||
alias Pescarte.Database.Repo | ||
import Ecto.Query | ||
|
||
@spec list_tags() :: {:ok, list(Tag.t())} | {:error, term()} | ||
def list_tags do | ||
Repo.replica().all(Tag) | ||
end | ||
|
||
@spec fetch_tag_by_id(String.t()) :: {:ok, Tag.t()} | {:error, term()} | ||
def fetch_tag_by_id(id) do | ||
Pescarte.Database.fetch(Tag, id) | ||
end | ||
|
||
@spec fetch_tag_by_name(String.t()) :: {:ok, Tag.t()} | {:error, term()} | ||
def fetch_tag_by_name(nome) do | ||
Pescarte.Database.fetch_by(Tag, nome: nome) | ||
end | ||
|
||
@spec create_tag(map()) :: {:ok, Tag.t()} | {:error, Ecto.Changeset.t()} | ||
def create_tag(attrs) do | ||
%Tag{} | ||
|> Tag.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@spec update_tag(String.t(), map()) :: {:ok, :updated} | {:error, :not_found} | ||
def update_tag(id, attrs) do | ||
query = from(t in Tag, where: t.id == ^id) | ||
|
||
query | ||
|> Repo.update_all(set: Map.to_list(attrs)) | ||
|> case do | ||
{0, _} -> {:error, :not_found} | ||
{_, _} -> {:ok, :updated} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uma tupla de ok necessariamente retorna um valor concreto. na query de update e delete vc pode usar a diretiva
no caso do delete a gente não precisa da entidade, então pode retornar apenas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. além disso a função |
||
end | ||
end | ||
|
||
@spec delete_tag(String.t()) :: {:ok, :deleted} | {:error, :not_found} | ||
def delete_tag(id) do | ||
query = from(t in Tag, where: t.id == ^id) | ||
|
||
query | ||
|> Repo.delete_all() | ||
|> case do | ||
{0, _} -> {:error, :not_found} | ||
{_, _} -> {:ok, :deleted} | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule Pescarte.Blog.Entity.Tag do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seria interessante ter uma documentação simples do que essa entidade representa no sistema |
||
use Pescarte, :model | ||
|
||
alias Pescarte.Database.Types.PublicId | ||
|
||
@type t :: %Tag{nome: binary, id: binary} | ||
|
||
@required_fields ~w(nome)a | ||
|
||
@primary_key {:id, PublicId, autogenerate: true} | ||
schema "blog_tag" do | ||
field :nome, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@spec changeset(Tag.t(), map) :: changeset | ||
def changeset(%Tag{} = tag, attrs) do | ||
tag | ||
|> cast(attrs, @required_fields) | ||
|> validate_required(@required_fields) | ||
|> unique_constraint(:nome) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule Pescarte.Database.Repo.Migrations.CreateBlogTag do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:blog_tag, primary_key: false) do | ||
add :id, :string, primary_key: true | ||
add :nome, :string, null: false | ||
|
||
timestamps() | ||
end | ||
create unique_index(:blog_tag, [:nome]) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
repito, o contexto blog só expõe a API pública que será usada no frontend ou em outros contextos. Funções de CRUD não deveriam ir nesse contexto.
exemplo de funções do contexto blog:
funções de CRUD de uma entidade pura devem ser centralizadas no arquivo que define essa entidade, no caso
Blog.Entity.Tag