-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
457 additions
and
0 deletions.
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,104 @@ | ||
defmodule Pento.Blogs do | ||
@moduledoc """ | ||
The Blogs context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias Pento.Repo | ||
|
||
alias Pento.Blogs.Blog | ||
|
||
@doc """ | ||
Returns the list of blog. | ||
## Examples | ||
iex> list_blog() | ||
[%Blog{}, ...] | ||
""" | ||
def list_blog do | ||
Repo.all(Blog) | ||
end | ||
|
||
@doc """ | ||
Gets a single blog. | ||
Raises `Ecto.NoResultsError` if the Blog does not exist. | ||
## Examples | ||
iex> get_blog!(123) | ||
%Blog{} | ||
iex> get_blog!(456) | ||
** (Ecto.NoResultsError) | ||
""" | ||
def get_blog!(id), do: Repo.get!(Blog, id) | ||
|
||
@doc """ | ||
Creates a blog. | ||
## Examples | ||
iex> create_blog(%{field: value}) | ||
{:ok, %Blog{}} | ||
iex> create_blog(%{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def create_blog(attrs \\ %{}) do | ||
%Blog{} | ||
|> Blog.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
@doc """ | ||
Updates a blog. | ||
## Examples | ||
iex> update_blog(blog, %{field: new_value}) | ||
{:ok, %Blog{}} | ||
iex> update_blog(blog, %{field: bad_value}) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def update_blog(%Blog{} = blog, attrs) do | ||
blog | ||
|> Blog.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
|
||
@doc """ | ||
Deletes a blog. | ||
## Examples | ||
iex> delete_blog(blog) | ||
{:ok, %Blog{}} | ||
iex> delete_blog(blog) | ||
{:error, %Ecto.Changeset{}} | ||
""" | ||
def delete_blog(%Blog{} = blog) do | ||
Repo.delete(blog) | ||
end | ||
|
||
@doc """ | ||
Returns an `%Ecto.Changeset{}` for tracking blog changes. | ||
## Examples | ||
iex> change_blog(blog) | ||
%Ecto.Changeset{data: %Blog{}} | ||
""" | ||
def change_blog(%Blog{} = blog, attrs \\ %{}) do | ||
Blog.changeset(blog, attrs) | ||
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,18 @@ | ||
defmodule Pento.Blogs.Blog do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "blog" do | ||
field :title, :string | ||
field :content, :string | ||
|
||
timestamps(type: :utc_datetime) | ||
end | ||
|
||
@doc false | ||
def changeset(blog, attrs) do | ||
blog | ||
|> cast(attrs, [:title, :content]) | ||
|> validate_required([:title, :content]) | ||
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,62 @@ | ||
defmodule PentoWeb.BlogController do | ||
use PentoWeb, :controller | ||
|
||
alias Pento.Blogs | ||
alias Pento.Blogs.Blog | ||
|
||
def index(conn, _params) do | ||
blog = Blogs.list_blog() | ||
render(conn, :index, blog_collection: blog) | ||
end | ||
|
||
def new(conn, _params) do | ||
changeset = Blogs.change_blog(%Blog{}) | ||
render(conn, :new, changeset: changeset) | ||
end | ||
|
||
def create(conn, %{"blog" => blog_params}) do | ||
case Blogs.create_blog(blog_params) do | ||
{:ok, blog} -> | ||
conn | ||
|> put_flash(:info, "Blog created successfully.") | ||
|> redirect(to: ~p"/blog/#{blog}") | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, :new, changeset: changeset) | ||
end | ||
end | ||
|
||
def show(conn, %{"id" => id}) do | ||
blog = Blogs.get_blog!(id) | ||
render(conn, :show, blog: blog) | ||
end | ||
|
||
def edit(conn, %{"id" => id}) do | ||
blog = Blogs.get_blog!(id) | ||
changeset = Blogs.change_blog(blog) | ||
render(conn, :edit, blog: blog, changeset: changeset) | ||
end | ||
|
||
def update(conn, %{"id" => id, "blog" => blog_params}) do | ||
blog = Blogs.get_blog!(id) | ||
|
||
case Blogs.update_blog(blog, blog_params) do | ||
{:ok, blog} -> | ||
conn | ||
|> put_flash(:info, "Blog updated successfully.") | ||
|> redirect(to: ~p"/blog/#{blog}") | ||
|
||
{:error, %Ecto.Changeset{} = changeset} -> | ||
render(conn, :edit, blog: blog, changeset: changeset) | ||
end | ||
end | ||
|
||
def delete(conn, %{"id" => id}) do | ||
blog = Blogs.get_blog!(id) | ||
{:ok, _blog} = Blogs.delete_blog(blog) | ||
|
||
conn | ||
|> put_flash(:info, "Blog deleted successfully.") | ||
|> redirect(to: ~p"/blog") | ||
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 PentoWeb.BlogHTML do | ||
use PentoWeb, :html | ||
|
||
embed_templates "blog_html/*" | ||
|
||
@doc """ | ||
Renders a blog form. | ||
""" | ||
attr :changeset, Ecto.Changeset, required: true | ||
attr :action, :string, required: true | ||
|
||
def blog_form(assigns) | ||
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,10 @@ | ||
<.simple_form :let={f} for={@changeset} action={@action}> | ||
<.error :if={@changeset.action}> | ||
Oops, something went wrong! Please check the errors below. | ||
</.error> | ||
<.input field={f[:title]} type="text" label="Title" /> | ||
<.input field={f[:content]} type="text" label="Content" /> | ||
<:actions> | ||
<.button>Save Blog</.button> | ||
</:actions> | ||
</.simple_form> |
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,8 @@ | ||
<.header> | ||
Edit Blog <%= @blog.id %> | ||
<:subtitle>Use this form to manage blog records in your database.</:subtitle> | ||
</.header> | ||
|
||
<.blog_form changeset={@changeset} action={~p"/blog/#{@blog}"} /> | ||
|
||
<.back navigate={~p"/blog"}>Back to blog</.back> |
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,39 @@ | ||
<.header> | ||
Listing Blog | ||
<:actions> | ||
<.link href={~p"/blog/new"}> | ||
<.button>New Blog</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.table id="blog" rows={@blog_collection} row_click={&JS.navigate(~p"/blog/#{&1}")}> | ||
<:col :let={blog} label="Title"><%= blog.title %></:col> | ||
<:col :let={blog} label="Content"><%= blog.content %></:col> | ||
<:action :let={blog}> | ||
<div class="sr-only"> | ||
<.link navigate={~p"/blog/#{blog}"}>Show</.link> | ||
</div> | ||
<.link navigate={~p"/blog/#{blog}/edit"}>Edit</.link> | ||
</:action> | ||
<:action :let={blog}> | ||
<.link href={~p"/blog/#{blog}"} method="delete" data-confirm="Are you sure?"> | ||
Delete | ||
</.link> | ||
</:action> | ||
</.table> | ||
|
||
|
||
<%!-- <%= for blog <- @blog_collection do %> --%> | ||
<%!-- <h1 class="text-brown-400 text-3xl"> --%> | ||
<%!-- <%= link to: Routes.blog_path(@conn, :show, blog) do %> --%> | ||
<%!-- <%= blog.title %> --%> | ||
<%!-- <% end %> --%> | ||
|
||
<%!-- </h1> --%> | ||
<%!-- <br /> --%> | ||
|
||
<%!-- <p><%= blog.content %></p> --%> | ||
<%!-- <% 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,8 @@ | ||
<.header> | ||
New Blog | ||
<:subtitle>Use this form to manage blog records in your database.</:subtitle> | ||
</.header> | ||
|
||
<.blog_form changeset={@changeset} action={~p"/blog"} /> | ||
|
||
<.back navigate={~p"/blog"}>Back to blog</.back> |
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,16 @@ | ||
<.header> | ||
Blog <%= @blog.id %> | ||
<:subtitle>This is a blog record from your database.</:subtitle> | ||
<:actions> | ||
<.link href={~p"/blog/#{@blog}/edit"}> | ||
<.button>Edit blog</.button> | ||
</.link> | ||
</:actions> | ||
</.header> | ||
|
||
<.list> | ||
<:item title="Title"><%= @blog.title %></:item> | ||
<:item title="Content"><%= @blog.content %></:item> | ||
</.list> | ||
|
||
<.back navigate={~p"/blog"}>Back to blog</.back> |
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
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,12 @@ | ||
defmodule Pento.Repo.Migrations.CreateBlog do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:blog) do | ||
add :title, :string | ||
add :content, :text | ||
|
||
timestamps(type: :utc_datetime) | ||
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,61 @@ | ||
defmodule Pento.BlogsTest do | ||
use Pento.DataCase | ||
|
||
alias Pento.Blogs | ||
|
||
describe "blog" do | ||
alias Pento.Blogs.Blog | ||
|
||
import Pento.BlogsFixtures | ||
|
||
@invalid_attrs %{title: nil, content: nil} | ||
|
||
test "list_blog/0 returns all blog" do | ||
blog = blog_fixture() | ||
assert Blogs.list_blog() == [blog] | ||
end | ||
|
||
test "get_blog!/1 returns the blog with given id" do | ||
blog = blog_fixture() | ||
assert Blogs.get_blog!(blog.id) == blog | ||
end | ||
|
||
test "create_blog/1 with valid data creates a blog" do | ||
valid_attrs = %{title: "some title", content: "some content"} | ||
|
||
assert {:ok, %Blog{} = blog} = Blogs.create_blog(valid_attrs) | ||
assert blog.title == "some title" | ||
assert blog.content == "some content" | ||
end | ||
|
||
test "create_blog/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{}} = Blogs.create_blog(@invalid_attrs) | ||
end | ||
|
||
test "update_blog/2 with valid data updates the blog" do | ||
blog = blog_fixture() | ||
update_attrs = %{title: "some updated title", content: "some updated content"} | ||
|
||
assert {:ok, %Blog{} = blog} = Blogs.update_blog(blog, update_attrs) | ||
assert blog.title == "some updated title" | ||
assert blog.content == "some updated content" | ||
end | ||
|
||
test "update_blog/2 with invalid data returns error changeset" do | ||
blog = blog_fixture() | ||
assert {:error, %Ecto.Changeset{}} = Blogs.update_blog(blog, @invalid_attrs) | ||
assert blog == Blogs.get_blog!(blog.id) | ||
end | ||
|
||
test "delete_blog/1 deletes the blog" do | ||
blog = blog_fixture() | ||
assert {:ok, %Blog{}} = Blogs.delete_blog(blog) | ||
assert_raise Ecto.NoResultsError, fn -> Blogs.get_blog!(blog.id) end | ||
end | ||
|
||
test "change_blog/1 returns a blog changeset" do | ||
blog = blog_fixture() | ||
assert %Ecto.Changeset{} = Blogs.change_blog(blog) | ||
end | ||
end | ||
end |
Oops, something went wrong.