Skip to content

Commit

Permalink
Merge pull request #13 from cciuenf/feature/nucleo_schema
Browse files Browse the repository at this point in the history
Feature/nucleo schema
  • Loading branch information
link2755 committed Aug 2, 2021
2 parents 4b3931a + b41a94e commit 6528fbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/fuschia/entities/nucleo.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Fuschia.Entities.Nucleo do
@moduledoc """
Nucleo schema
"""
use Fuschia.Schema
import Ecto.Changeset

@required_fields ~w(nome descricao)a
@optional_fields ~w()a

@primary_key {:nome, :string, []}
schema "nucleo" do
field :descricao, :string

timestamps()
end

@doc false
def changeset(%__MODULE__{} = struct, attrs) do
struct
|> cast(attrs, @required_fields ++ @optional_fields)
|> validate_required(@required_fields)
|> validate_length(:descricao_nucleo, max: 400)
end

defimpl Jason.Encoder, for: __MODULE__ do
def encode(struct, opts) do
%{
nome: struct.nome,
descricao_nucleo: struct.descricao_nucleo
}
|> Fuschia.Encoder.encode(opts)
end
end
end
12 changes: 12 additions & 0 deletions priv/repo/migrations/20210726191013_create_nucleo.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Fuschia.Repo.Migrations.CreateNucleos do
use Ecto.Migration

def change do
create table(:nucleo, primary_key: false) do
add :nome, :string, primary_key: true
add :descricao, :string, size: 400, null: false

timestamps()
end
end
end

0 comments on commit 6528fbe

Please sign in to comment.