Skip to content
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 9 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/pescarte/blog/blog_tag.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule Pescarte.Blog.BlogTag do
SoraAsc marked this conversation as resolved.
Show resolved Hide resolved
SoraAsc marked this conversation as resolved.
Show resolved Hide resolved
@moduledoc """
O contexto BlogTag é responsável por gerenciar as operações relacionadas a TAG.
"""

alias Pescarte.Blog.Models.Tag
alias Pescarte.Database.Repo

@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, Tag.t()} | {:error, :not_found | Ecto.Changeset.t()}
def update_tag(id, attrs) do
case Repo.get(Tag, id) do
nil ->
{:error, :not_found}

tag ->
tag
|> Tag.changeset(attrs)
|> Repo.update()
end
SoraAsc marked this conversation as resolved.
Show resolved Hide resolved
end

@spec delete_tag_by_id(String.t()) :: {:ok, Tag.t()} | {:error, :not_found}
def delete_tag_by_id(id) do
case Repo.get(Tag, id) do
nil -> {:error, :not_found}
tag -> Repo.delete(tag)
end
end
SoraAsc marked this conversation as resolved.
Show resolved Hide resolved
end
24 changes: 24 additions & 0 deletions lib/pescarte/blog/models/tag.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Pescarte.Blog.Models.Tag do
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
13 changes: 13 additions & 0 deletions priv/repo/migrations/20240814202453_create_blog_tag.exs
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
Loading