Skip to content

Commit e1e5ec8

Browse files
committed
feat: Add Post table
1 parent 0dd6a25 commit e1e5ec8

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

lib/brasil_em_dados/accounts/user_token.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ defmodule BrasilEmDados.Accounts.UserToken do
134134
end
135135

136136
def user_and_contexts_query(user, [_ | _] = contexts) do
137-
from t in BrasilEmDados.Accounts.UserToken, where: t.user_id == ^user.id and t.context in ^contexts
137+
from t in BrasilEmDados.Accounts.UserToken,
138+
where: t.user_id == ^user.id and t.context in ^contexts
138139
end
139140
end

lib/brasil_em_dados/blog.ex

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
defmodule BrasilEmDados.Blog do
2+
@moduledoc """
3+
The Blog context.
4+
"""
5+
6+
import Ecto.Query, warn: false
7+
alias BrasilEmDados.Repo
8+
9+
alias BrasilEmDados.Blog.Post
10+
11+
@doc """
12+
Returns the list of posts.
13+
14+
## Examples
15+
16+
iex> list_posts()
17+
[%Post{}, ...]
18+
19+
"""
20+
def list_posts do
21+
Repo.all(Post)
22+
end
23+
24+
@doc """
25+
Gets a single post.
26+
27+
Raises `Ecto.NoResultsError` if the Post does not exist.
28+
29+
## Examples
30+
31+
iex> get_post!(123)
32+
%Post{}
33+
34+
iex> get_post!(456)
35+
** (Ecto.NoResultsError)
36+
37+
"""
38+
def get_post!(id), do: Repo.get!(Post, id)
39+
40+
@doc """
41+
Creates a post.
42+
43+
## Examples
44+
45+
iex> create_post(%{field: value})
46+
{:ok, %Post{}}
47+
48+
iex> create_post(%{field: bad_value})
49+
{:error, %Ecto.Changeset{}}
50+
51+
"""
52+
def create_post(attrs \\ %{}) do
53+
%Post{}
54+
|> Post.changeset(attrs)
55+
|> Repo.insert()
56+
end
57+
58+
@doc """
59+
Updates a post.
60+
61+
## Examples
62+
63+
iex> update_post(post, %{field: new_value})
64+
{:ok, %Post{}}
65+
66+
iex> update_post(post, %{field: bad_value})
67+
{:error, %Ecto.Changeset{}}
68+
69+
"""
70+
def update_post(%Post{} = post, attrs) do
71+
post
72+
|> Post.changeset(attrs)
73+
|> Repo.update()
74+
end
75+
76+
@doc """
77+
Deletes a post.
78+
79+
## Examples
80+
81+
iex> delete_post(post)
82+
{:ok, %Post{}}
83+
84+
iex> delete_post(post)
85+
{:error, %Ecto.Changeset{}}
86+
87+
"""
88+
def delete_post(%Post{} = post) do
89+
Repo.delete(post)
90+
end
91+
92+
@doc """
93+
Returns an `%Ecto.Changeset{}` for tracking post changes.
94+
95+
## Examples
96+
97+
iex> change_post(post)
98+
%Ecto.Changeset{data: %Post{}}
99+
100+
"""
101+
def change_post(%Post{} = post, attrs \\ %{}) do
102+
Post.changeset(post, attrs)
103+
end
104+
end

lib/brasil_em_dados/blog/post.ex

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule BrasilEmDados.Blog.Post do
2+
use Ecto.Schema
3+
import Ecto.Changeset
4+
5+
schema "posts" do
6+
field :body, :string
7+
field :title, :string
8+
field :slug, :string
9+
field :user_id, :id
10+
11+
timestamps()
12+
end
13+
14+
@doc false
15+
def changeset(post, attrs) do
16+
post
17+
|> cast(attrs, [:title, :slug, :body, :user_id])
18+
|> validate_required([:title, :body, :slug, :user_id])
19+
end
20+
end

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ defmodule BrasilEmDados.MixProject do
4848
{:gettext, "~> 0.11"},
4949
{:jason, "~> 1.0"},
5050
{:plug_cowboy, "~> 2.0"},
51-
{:phx_gen_auth, "~> 0.4.0", only: [:dev], runtime: false},
51+
{:phx_gen_auth, "~> 0.4.0", only: [:dev], runtime: false}
5252
]
5353
end
5454

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule BrasilEmDados.Repo.Migrations.CreatePosts do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:posts) do
6+
add :title, :string, null: false
7+
add :slug, :string, null: false
8+
add :body, :text, null: false
9+
add :user_id, references(:users, on_delete: :nothing)
10+
11+
timestamps()
12+
end
13+
14+
create index(:posts, [:user_id])
15+
create index(:posts, [:slug])
16+
end
17+
end

0 commit comments

Comments
 (0)