chore: generate resources for pastes
Signed-off-by: SphericalKat <amolele@gmail.com>
This commit is contained in:
13 files changed
+431
No files matched your search
@@ -0,0 +1,104 @@
|
||||
defmodule Ketbin.Pastes do
|
||||
@moduledoc """
|
||||
The Pastes context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Ketbin.Repo
|
||||
|
||||
alias Ketbin.Pastes.Paste
|
||||
|
||||
@doc """
|
||||
Returns the list of pastes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_pastes()
|
||||
[%Paste{}, ...]
|
||||
|
||||
"""
|
||||
def list_pastes do
|
||||
Repo.all(Paste)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single paste.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Paste does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_paste!(123)
|
||||
%Paste{}
|
||||
|
||||
iex> get_paste!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_paste!(id), do: Repo.get!(Paste, id)
|
||||
|
||||
@doc """
|
||||
Creates a paste.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_paste(%{field: value})
|
||||
{:ok, %Paste{}}
|
||||
|
||||
iex> create_paste(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_paste(attrs \\ %{}) do
|
||||
%Paste{}
|
||||
|> Paste.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a paste.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_paste(paste, %{field: new_value})
|
||||
{:ok, %Paste{}}
|
||||
|
||||
iex> update_paste(paste, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_paste(%Paste{} = paste, attrs) do
|
||||
paste
|
||||
|> Paste.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a paste.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_paste(paste)
|
||||
{:ok, %Paste{}}
|
||||
|
||||
iex> delete_paste(paste)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_paste(%Paste{} = paste) do
|
||||
Repo.delete(paste)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking paste changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_paste(paste)
|
||||
%Ecto.Changeset{data: %Paste{}}
|
||||
|
||||
"""
|
||||
def change_paste(%Paste{} = paste, attrs \\ %{}) do
|
||||
Paste.changeset(paste, attrs)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
defmodule Ketbin.Pastes.Paste do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
schema "pastes" do
|
||||
field :content, :string
|
||||
field :is_url, :boolean, default: false
|
||||
field :belongs_to, :id
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(paste, attrs) do
|
||||
paste
|
||||
|> cast(attrs, [:is_url, :content])
|
||||
|> validate_required([:is_url, :content])
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,62 @@
|
||||
defmodule KetbinWeb.PasteController do
|
||||
use KetbinWeb, :controller
|
||||
|
||||
alias Ketbin.Pastes
|
||||
alias Ketbin.Pastes.Paste
|
||||
|
||||
def index(conn, _params) do
|
||||
pastes = Pastes.list_pastes()
|
||||
render(conn, "index.html", pastes: pastes)
|
||||
end
|
||||
|
||||
def new(conn, _params) do
|
||||
changeset = Pastes.change_paste(%Paste{})
|
||||
render(conn, "new.html", changeset: changeset)
|
||||
end
|
||||
|
||||
def create(conn, %{"paste" => paste_params}) do
|
||||
case Pastes.create_paste(paste_params) do
|
||||
{:ok, paste} ->
|
||||
conn
|
||||
|> put_flash(:info, "Paste created successfully.")
|
||||
|> redirect(to: Routes.paste_path(conn, :show, paste))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "new.html", changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def show(conn, %{"id" => id}) do
|
||||
paste = Pastes.get_paste!(id)
|
||||
render(conn, "show.html", paste: paste)
|
||||
end
|
||||
|
||||
def edit(conn, %{"id" => id}) do
|
||||
paste = Pastes.get_paste!(id)
|
||||
changeset = Pastes.change_paste(paste)
|
||||
render(conn, "edit.html", paste: paste, changeset: changeset)
|
||||
end
|
||||
|
||||
def update(conn, %{"id" => id, "paste" => paste_params}) do
|
||||
paste = Pastes.get_paste!(id)
|
||||
|
||||
case Pastes.update_paste(paste, paste_params) do
|
||||
{:ok, paste} ->
|
||||
conn
|
||||
|> put_flash(:info, "Paste updated successfully.")
|
||||
|> redirect(to: Routes.paste_path(conn, :show, paste))
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
render(conn, "edit.html", paste: paste, changeset: changeset)
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, %{"id" => id}) do
|
||||
paste = Pastes.get_paste!(id)
|
||||
{:ok, _paste} = Pastes.delete_paste(paste)
|
||||
|
||||
conn
|
||||
|> put_flash(:info, "Paste deleted successfully.")
|
||||
|> redirect(to: Routes.paste_path(conn, :index))
|
||||
end
|
||||
end
|
||||
@@ -73,5 +73,7 @@ defmodule KetbinWeb.Router do
|
||||
get "/users/confirm", UserConfirmationController, :new
|
||||
post "/users/confirm", UserConfirmationController, :create
|
||||
get "/users/confirm/:token", UserConfirmationController, :confirm
|
||||
|
||||
resources "/pastes", PasteController
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
<h1>Edit Paste</h1>
|
||||
|
||||
<%= render "form.html", Map.put(assigns, :action, Routes.paste_path(@conn, :update, @paste)) %>
|
||||
|
||||
<span><%= link "Back", to: Routes.paste_path(@conn, :index) %></span>
|
||||
@@ -0,0 +1,19 @@
|
||||
<%= form_for @changeset, @action, fn f -> %>
|
||||
<%= if @changeset.action do %>
|
||||
<div class="alert alert-danger">
|
||||
<p>Oops, something went wrong! Please check the errors below.</p>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<%= label f, :is_url %>
|
||||
<%= checkbox f, :is_url %>
|
||||
<%= error_tag f, :is_url %>
|
||||
|
||||
<%= label f, :content %>
|
||||
<%= textarea f, :content %>
|
||||
<%= error_tag f, :content %>
|
||||
|
||||
<div>
|
||||
<%= submit "Save" %>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -0,0 +1,28 @@
|
||||
<h1>Listing Pastes</h1>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Is url</th>
|
||||
<th>Content</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<%= for paste <- @pastes do %>
|
||||
<tr>
|
||||
<td><%= paste.is_url %></td>
|
||||
<td><%= paste.content %></td>
|
||||
|
||||
<td>
|
||||
<span><%= link "Show", to: Routes.paste_path(@conn, :show, paste) %></span>
|
||||
<span><%= link "Edit", to: Routes.paste_path(@conn, :edit, paste) %></span>
|
||||
<span><%= link "Delete", to: Routes.paste_path(@conn, :delete, paste), method: :delete, data: [confirm: "Are you sure?"] %></span>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<span><%= link "New Paste", to: Routes.paste_path(@conn, :new) %></span>
|
||||
@@ -0,0 +1,5 @@
|
||||
<h1>New Paste</h1>
|
||||
|
||||
<%= render "form.html", Map.put(assigns, :action, Routes.paste_path(@conn, :create)) %>
|
||||
|
||||
<span><%= link "Back", to: Routes.paste_path(@conn, :index) %></span>
|
||||
@@ -0,0 +1,18 @@
|
||||
<h1>Show Paste</h1>
|
||||
|
||||
<ul>
|
||||
|
||||
<li>
|
||||
<strong>Is url:</strong>
|
||||
<%= @paste.is_url %>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<strong>Content:</strong>
|
||||
<%= @paste.content %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= link "Edit", to: Routes.paste_path(@conn, :edit, @paste) %></span>
|
||||
<span><%= link "Back", to: Routes.paste_path(@conn, :index) %></span>
|
||||
@@ -0,0 +1,3 @@
|
||||
defmodule KetbinWeb.PasteView do
|
||||
use KetbinWeb, :view
|
||||
end
|
||||
Reference in new issue
Block a user