-
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
Merged
Merged
Feat/blog tag schema #185
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
16d233d
feat: created blog_tag table migration
SoraAsc f64dffe
feat: created blog_tag model
SoraAsc cd43ada
feat: tag crud
SoraAsc 5070208
fix: code format
SoraAsc b32867e
refactor: change in tag namespace
SoraAsc 34eea36
fix: blog CRUD
SoraAsc 32159a3
fix: centralizando funções do crud na entidade tag
SoraAsc 6ab7fd2
fix: update e delete
SoraAsc 8c3dfd0
feat: documentação simples na entidade Tag
SoraAsc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
defmodule Pescarte.Blog.Entity.Tag do | ||
@moduledoc """ | ||
A entidade `Tag` fornece um CRUD básico para a mesma e é responsável por categorizar os posts do contexto Blog, para filtragem e pesquisa. | ||
""" | ||
|
||
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 | ||
|
||
@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, changeset} | ||
def create_tag(attrs) do | ||
%Tag{} | ||
|> changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@spec update_tag(String.t(), map()) :: {:ok, Tag.t()} | {:error, :not_found} | ||
def update_tag(id, attrs) do | ||
query = from(t in Tag, where: t.id == ^id, select: t) | ||
|
||
attrs_with_updated_date = Map.put(attrs, :updated_at, NaiveDateTime.utc_now()) | ||
|
||
query | ||
|> Repo.update_all(set: Map.to_list(attrs_with_updated_date)) | ||
|> case do | ||
{1, [updated_tag]} -> {:ok, updated_tag} | ||
{_, _} -> {:error, :not_found} | ||
end | ||
end | ||
|
||
@spec delete_tag(String.t()) :: :ok | {:error, :not_found} | ||
def delete_tag(id) do | ||
query = from(t in Tag, where: t.id == ^id) | ||
|
||
query | ||
|> Repo.delete_all() | ||
|> case do | ||
{1, _} -> :ok | ||
{_, _} -> {:error, :not_found} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
seria interessante ter uma documentação simples do que essa entidade representa no sistema