feat: public note list

This commit is contained in:
Ivan R. 2024-07-29 18:07:44 +05:00
parent 5a00fdf843
commit 107af78925
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
16 changed files with 147 additions and 114 deletions

View file

@ -17,8 +17,8 @@ defmodule ComfycampWeb.NavBar do
~H"""
<nav class="limiter navbar">
<.link href={~p"/"}>Главная</.link>
<.link href={~p"/blog/"}>Блог</.link>
<.link href={~p"/cinema/"}>Кинотеатр</.link>
<.link href={~p"/notes"}>Заметки</.link>
<.link href={~p"/cinema"}>Кинотеатр</.link>
<div class="space" />

View file

@ -1,7 +0,0 @@
defmodule ComfycampWeb.BlogController do
use ComfycampWeb, :controller
def index(conn, _params) do
render(conn, :index, page_title: "Блог")
end
end

View file

@ -1,8 +0,0 @@
defmodule ComfycampWeb.BlogHTML do
@moduledoc """
This module contains pages rendered by BlogController.
"""
use ComfycampWeb, :html
embed_templates "blog_html/*"
end

View file

@ -1 +0,0 @@
<p>Блог</p>

View file

@ -2,88 +2,16 @@ defmodule ComfycampWeb.NotesController do
use ComfycampWeb, :controller
alias Comfycamp.Notes
alias Comfycamp.Notes.Note
def index(conn, _params) do
notes = Notes.list_notes()
conn
|> put_layout(html: :admin)
|> render(:index, page_title: "Заметки", notes: notes)
end
def new(conn, _params) do
changeset = Notes.change_note(%Note{})
conn
|> put_layout(html: :admin)
|> render(:new, page_title: "Новая заметка", changeset: changeset)
end
def edit(conn, %{"id" => id}) do
note = Notes.get_note!(id)
changeset = Notes.change_note(note)
conn
|> put_layout(html: :admin)
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
end
def create(conn, %{"note" => note_params}) do
case Notes.create_note(note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Заметка сохранена.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при обновлении заметки.")
|> put_layout(html: :admin)
|> render(:new, page_title: "Создать заметку", changeset: changeset)
end
end
def update(conn, %{"id" => id, "note" => note_params}) do
note = Notes.get_note!(id)
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Заметка обновлена.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при обновлении заметки.")
|> put_layout(html: :admin)
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
end
render(conn, :index, page_title: "Заметки", notes: notes)
end
def show(conn, %{"id" => id}) do
note = Notes.get_note!(id)
conn
|> put_layout(html: :admin)
|> render(:show, page_title: "Заметка", note: note)
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
case Notes.delete_note(note) do
{:ok, _note} ->
conn
|> put_flash(:info, "Заметка удалена.")
|> redirect(to: ~p"/admin/notes")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при удалении заметки.")
|> put_layout(html: :admin)
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
end
render(conn, :show, page_title: note.title, note: note)
end
end

View file

@ -0,0 +1,89 @@
defmodule ComfycampWeb.NotesEditorController do
use ComfycampWeb, :controller
alias Comfycamp.Notes
alias Comfycamp.Notes.Note
def index(conn, _params) do
notes = Notes.list_notes()
conn
|> put_layout(html: :admin)
|> render(:index, page_title: "Заметки", notes: notes)
end
def new(conn, _params) do
changeset = Notes.change_note(%Note{})
conn
|> put_layout(html: :admin)
|> render(:new, page_title: "Новая заметка", changeset: changeset)
end
def edit(conn, %{"id" => id}) do
note = Notes.get_note!(id)
changeset = Notes.change_note(note)
conn
|> put_layout(html: :admin)
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
end
def create(conn, %{"note" => note_params}) do
case Notes.create_note(note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Заметка сохранена.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при обновлении заметки.")
|> put_layout(html: :admin)
|> render(:new, page_title: "Создать заметку", changeset: changeset)
end
end
def update(conn, %{"id" => id, "note" => note_params}) do
note = Notes.get_note!(id)
case Notes.update_note(note, note_params) do
{:ok, note} ->
conn
|> put_flash(:info, "Заметка обновлена.")
|> redirect(to: ~p"/admin/notes/#{note}")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при обновлении заметки.")
|> put_layout(html: :admin)
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
note = Notes.get_note!(id)
conn
|> put_layout(html: :admin)
|> render(:show, page_title: "Заметка", note: note)
end
def delete(conn, %{"id" => id}) do
note = Notes.get_note!(id)
case Notes.delete_note(note) do
{:ok, _note} ->
conn
|> put_flash(:info, "Заметка удалена.")
|> redirect(to: ~p"/admin/notes")
{:error, changeset} ->
conn
|> put_flash(:error, "Ошибка при удалении заметки.")
|> put_layout(html: :admin)
|> render(:edit, page_title: "Редактировать заметку", changeset: changeset)
end
end
end

View file

@ -0,0 +1,5 @@
defmodule ComfycampWeb.NotesEditorHTML do
use ComfycampWeb, :html
embed_templates "notes_editor_html/*"
end

View file

@ -0,0 +1,17 @@
<div>
<.link href={~p"/admin/notes/new"}>
Создать заметку
</.link>
<ul>
<%= for note <- @notes do %>
<li>
<.link href={~p"/admin/notes/#{note}"}>
<%= note.title %>
</.link>
<%= note.updated_at %>
</li>
<% end %>
</ul>
</div>

View file

@ -0,0 +1,13 @@
<div>
<.back navigate={~p"/admin/notes"}>Назад</.back>
<h3><%= @note.title %></h3>
<.link href={~p"/admin/notes/#{@note}/edit"}>
Редактировать
</.link>
<p>Создана: <%= @note.inserted_at %></p>
<p>Обновлена: <%= @note.updated_at %></p>
<pre><%= @note.markdown %></pre>
</div>

View file

@ -1,4 +1,7 @@
defmodule ComfycampWeb.NotesHTML do
@moduledoc """
This module contains pages rendered by BlogController.
"""
use ComfycampWeb, :html
embed_templates "notes_html/*"

View file

@ -1,17 +1,12 @@
<div>
<.link href={~p"/admin/notes/new"}>
Создать заметку
</.link>
<h1>Заметки</h1>
<ul>
<%= for note <- @notes do %>
<li>
<.link href={~p"/admin/notes/#{note}"}>
<%= note.title %>
</.link>
<%= note.updated_at %>
</li>
<% end %>
</ul>
<%= for note <- @notes do %>
<div>
<.link href={~p"/notes/#{note}"}>
<h2><%= note.title %></h2>
</.link>
<%= note.inserted_at %>
</div>
<% end %>
</div>

View file

@ -1,13 +1,12 @@
<div>
<.back navigate={~p"/admin/notes"}>Назад</.back>
<h3><%= @note.title %></h3>
<h1><%= @note.title %></h1>
<.link href={~p"/admin/notes/#{@note}/edit"}>
Редактировать
</.link>
<.back navigate={~p"/notes"}>К списку заметок</.back>
<p>Создана: <%= @note.inserted_at %></p>
<p>Обновлена: <%= @note.updated_at %></p>
<pre><%= @note.markdown %></pre>
<pre>
<%= @note.markdown %>
</pre>
</div>

View file

@ -21,7 +21,7 @@ defmodule ComfycampWeb.Router do
pipe_through :browser
get "/", MainController, :index
get "/blog", BlogController, :index
resources "/notes", NotesController, only: [:index, :show]
get "/cinema", CinemaController, :index
end
@ -91,6 +91,6 @@ defmodule ComfycampWeb.Router do
get "/", AdminPageController, :home
get "/users", AdminPageController, :users
get "/services", AdminPageController, :services
resources "/notes", NotesController
resources "/notes", NotesEditorController
end
end