removing 'properties' from boilerplate
This commit is contained in:
parent
82ab1d1ea5
commit
3a9f00b14e
16 changed files with 16 additions and 2058 deletions
|
@ -1,104 +0,0 @@
|
|||
defmodule Shift73k.Properties do
|
||||
@moduledoc """
|
||||
The Properties context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Shift73k.Repo
|
||||
|
||||
alias Shift73k.Properties.Property
|
||||
|
||||
@doc """
|
||||
Returns the list of properties.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_properties()
|
||||
[%Property{}, ...]
|
||||
|
||||
"""
|
||||
def list_properties do
|
||||
Repo.all(Property)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single property.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Property does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_property!(123)
|
||||
%Property{}
|
||||
|
||||
iex> get_property!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_property!(id), do: Repo.get!(Property, id)
|
||||
|
||||
@doc """
|
||||
Creates a property.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_property(%{field: value})
|
||||
{:ok, %Property{}}
|
||||
|
||||
iex> create_property(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_property(attrs \\ %{}) do
|
||||
%Property{}
|
||||
|> Property.create_changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a property.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_property(property, %{field: new_value})
|
||||
{:ok, %Property{}}
|
||||
|
||||
iex> update_property(property, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_property(%Property{} = property, attrs) do
|
||||
property
|
||||
|> Property.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a property.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_property(property)
|
||||
{:ok, %Property{}}
|
||||
|
||||
iex> delete_property(property)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_property(%Property{} = property) do
|
||||
Repo.delete(property)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking property changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_property(property)
|
||||
%Ecto.Changeset{data: %Property{}}
|
||||
|
||||
"""
|
||||
def change_property(%Property{} = property, attrs \\ %{}) do
|
||||
Property.changeset(property, attrs)
|
||||
end
|
||||
end
|
|
@ -1,28 +0,0 @@
|
|||
defmodule Shift73k.Properties.Property do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@primary_key {:id, :binary_id, autogenerate: true}
|
||||
@foreign_key_type :binary_id
|
||||
schema "properties" do
|
||||
field(:description, :string)
|
||||
field(:name, :string)
|
||||
field(:price, :decimal)
|
||||
field(:user_id, :binary_id)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def create_changeset(property, attrs) do
|
||||
property
|
||||
|> cast(attrs, [:name, :price, :description, :user_id])
|
||||
|> validate_required([:name, :price, :description, :user_id])
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(property, attrs) do
|
||||
property
|
||||
|> cast(attrs, [:name, :price, :description])
|
||||
|> validate_required([:name, :price, :description])
|
||||
end
|
||||
end
|
|
@ -1,66 +0,0 @@
|
|||
defmodule Shift73kWeb.PropertyLive.FormComponent do
|
||||
use Shift73kWeb, :live_component
|
||||
|
||||
alias Shift73k.Properties
|
||||
|
||||
@impl true
|
||||
def update(%{property: property} = assigns, socket) do
|
||||
socket
|
||||
|> assign(assigns)
|
||||
|> assign(:changeset, Properties.change_property(property))
|
||||
|> live_okreply()
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("validate", %{"property" => property_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.property
|
||||
|> Properties.change_property(property_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("save", %{"property" => property_params}, socket) do
|
||||
save_property(socket, socket.assigns.action, property_params)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("cancel", _, socket) do
|
||||
{:noreply, push_event(socket, "modal-please-hide", %{})}
|
||||
end
|
||||
|
||||
defp save_property(socket, :edit, property_params) do
|
||||
case Properties.update_property(socket.assigns.property, property_params) do
|
||||
{:ok, _property} ->
|
||||
flash = {:info, "Property updated successfully"}
|
||||
send(self(), {:put_flash_message, flash})
|
||||
|
||||
socket
|
||||
|> push_event("modal-please-hide", %{})
|
||||
|> live_noreply()
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, :changeset, changeset)}
|
||||
end
|
||||
end
|
||||
|
||||
defp save_property(socket, :new, property_params) do
|
||||
current_user = socket.assigns.current_user
|
||||
property_params = Map.put(property_params, "user_id", current_user.id)
|
||||
|
||||
case Properties.create_property(property_params) do
|
||||
{:ok, _property} ->
|
||||
flash = {:info, "Property created successfully"}
|
||||
send(self(), {:put_flash_message, flash})
|
||||
|
||||
socket
|
||||
|> push_event("modal-please-hide", %{})
|
||||
|> live_noreply()
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
{:noreply, assign(socket, changeset: changeset)}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,57 +0,0 @@
|
|||
<%= form_for @changeset, "#", [
|
||||
id: "property-form",
|
||||
phx_target: @myself,
|
||||
phx_change: "validate",
|
||||
phx_submit: "save"
|
||||
], fn f -> %>
|
||||
|
||||
<div class="modal-body">
|
||||
|
||||
<div class="mb-3" phx-feedback-for="<%= input_id(f, :name)%>">
|
||||
<%= label f, :name, class: "form-label" %>
|
||||
<div class="input-group has-validation">
|
||||
<%= text_input f, :name,
|
||||
class: input_class(f, :name, "form-control"),
|
||||
aria_describedby: error_id(f, :name)
|
||||
%>
|
||||
<%= error_tag f, :name %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3" phx-feedback-for="<%= input_id(f, :price)%>">
|
||||
<%= label f, :price, class: "form-label" %>
|
||||
<div class="input-group has-validation">
|
||||
<%= number_input f, :price,
|
||||
class: input_class(f, :price, "form-control"),
|
||||
step: "any",
|
||||
aria_describedby: error_id(f, :price)
|
||||
%>
|
||||
<%= error_tag f, :price %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3" phx-feedback-for="<%= input_id(f, :description)%>">
|
||||
<%= label f, :description, class: "form-label" %>
|
||||
<div class="input-group has-validation">
|
||||
<%= textarea f, :description,
|
||||
class: input_class(f, :description, "form-control"),
|
||||
aria_describedby: error_id(f, :description)
|
||||
%>
|
||||
<%= error_tag f, :description %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
|
||||
<%= link "Cancel", to: "#", class: "btn btn-secondary me-2", phx_click: "cancel", phx_target: @myself %>
|
||||
<%= submit "Save",
|
||||
class: "btn btn-primary ",
|
||||
disabled: !@changeset.valid?,
|
||||
aria_disabled: !@changeset.valid? && "true" || false,
|
||||
phx_disable_with: "Saving..."
|
||||
%>
|
||||
|
||||
</div>
|
||||
|
||||
<% end %>
|
|
@ -1,90 +0,0 @@
|
|||
defmodule Shift73kWeb.PropertyLive.Index do
|
||||
use Shift73kWeb, :live_view
|
||||
|
||||
alias Shift73k.Properties
|
||||
alias Shift73k.Properties.Property
|
||||
alias Shift73kWeb.Roles
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
socket = assign_defaults(socket, session)
|
||||
{:ok, assign(socket, :properties, [])}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(params, _url, socket) do
|
||||
current_user = socket.assigns.current_user
|
||||
live_action = socket.assigns.live_action
|
||||
property = property_from_params(params)
|
||||
|
||||
if Roles.can?(current_user, property, live_action) do
|
||||
socket
|
||||
|> assign(:properties, list_properties())
|
||||
|> assign(:modal_return_to, Routes.property_index_path(socket, :index))
|
||||
|> apply_action(live_action, params)
|
||||
|> live_noreply()
|
||||
else
|
||||
socket
|
||||
|> put_flash(:error, "Unauthorised")
|
||||
|> redirect(to: "/")
|
||||
|> live_noreply()
|
||||
end
|
||||
end
|
||||
|
||||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Property")
|
||||
|> assign(:property, Properties.get_property!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Property")
|
||||
|> assign(:property, %Property{})
|
||||
end
|
||||
|
||||
defp apply_action(socket, :index, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "Listing Properties")
|
||||
|> assign(:property, nil)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_event("delete", %{"id" => id}, socket) do
|
||||
current_user = socket.assigns.current_user
|
||||
property = Properties.get_property!(id)
|
||||
|
||||
if Shift73kWeb.Roles.can?(current_user, property, :delete) do
|
||||
property = Properties.get_property!(id)
|
||||
{:ok, _} = Properties.delete_property(property)
|
||||
|
||||
{:noreply, assign(socket, :properties, list_properties())}
|
||||
else
|
||||
{:noreply,
|
||||
socket
|
||||
|> put_flash(:error, "Unauthorised")
|
||||
|> redirect(to: "/")}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
|
||||
socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
|
||||
socket |> put_flash(flash_type, msg) |> live_noreply()
|
||||
end
|
||||
|
||||
defp property_from_params(params)
|
||||
|
||||
defp property_from_params(%{"id" => id}),
|
||||
do: Properties.get_property!(id)
|
||||
|
||||
defp property_from_params(_params), do: %Property{}
|
||||
|
||||
defp list_properties do
|
||||
Properties.list_properties()
|
||||
end
|
||||
end
|
|
@ -1,46 +0,0 @@
|
|||
<%= if @live_action in [:new, :edit] do %>
|
||||
<%= live_modal @socket, Shift73kWeb.PropertyLive.FormComponent,
|
||||
id: @property.id || :new,
|
||||
title: @page_title,
|
||||
action: @live_action,
|
||||
property: @property,
|
||||
current_user: @current_user %>
|
||||
<% end %>
|
||||
|
||||
<div class="d-flex justify-content-between d-flex align-items-end">
|
||||
<h2>Listing Properties</h2>
|
||||
<span><%= live_patch "New Property", to: Routes.property_index_path(@socket, :new), class: "btn btn-primary" %></span>
|
||||
</div>
|
||||
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Name</th>
|
||||
<th scope="col">Price</th>
|
||||
<th scope="col">Description</th>
|
||||
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="properties">
|
||||
<%= for property <- @properties do %>
|
||||
<tr id="property-<%= property.id %>">
|
||||
<td><%= property.name %></td>
|
||||
<td><%= property.price %></td>
|
||||
<td><%= property.description %></td>
|
||||
|
||||
<td>
|
||||
<%= if Roles.can?(@current_user, property, :show) do %>
|
||||
<%= live_redirect "Show", to: Routes.property_show_path(@socket, :show, property), class: "link-secondary mx-1" %>
|
||||
<% end %>
|
||||
<%= if Roles.can?(@current_user, property, :edit) do %>
|
||||
<%= live_patch "Edit", to: Routes.property_index_path(@socket, :edit, property), class: "mx-1" %>
|
||||
<% end %>
|
||||
<%= if Roles.can?(@current_user, property, :delete) do %>
|
||||
<%= link "Delete", to: "#", phx_click: "delete", phx_value_id: property.id, data: [confirm: "Are you sure?"], class: "link-danger mx-1" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
|
@ -1,45 +0,0 @@
|
|||
defmodule Shift73kWeb.PropertyLive.Show do
|
||||
use Shift73kWeb, :live_view
|
||||
|
||||
alias Shift73k.Properties
|
||||
alias Shift73kWeb.Roles
|
||||
|
||||
@impl true
|
||||
def mount(_params, session, socket) do
|
||||
socket = assign_defaults(socket, session)
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
current_user = socket.assigns.current_user
|
||||
live_action = socket.assigns.live_action
|
||||
property = Properties.get_property!(id)
|
||||
|
||||
if Roles.can?(current_user, property, live_action) do
|
||||
socket
|
||||
|> assign(:property, property)
|
||||
|> assign(:page_title, page_title(live_action))
|
||||
|> assign(:modal_return_to, Routes.property_show_path(socket, :show, property))
|
||||
|> live_noreply()
|
||||
else
|
||||
socket
|
||||
|> put_flash(:error, "Unauthorised")
|
||||
|> redirect(to: "/")
|
||||
|> live_noreply()
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:close_modal, _}, %{assigns: %{modal_return_to: to}} = socket) do
|
||||
socket |> copy_flash() |> push_patch(to: to) |> live_noreply()
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:put_flash_message, {flash_type, msg}}, socket) do
|
||||
socket |> put_flash(flash_type, msg) |> live_noreply()
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Property"
|
||||
defp page_title(:edit), do: "Edit Property"
|
||||
end
|
|
@ -1,33 +0,0 @@
|
|||
<h2>Show Property</h2>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<%= live_modal @socket, Shift73kWeb.PropertyLive.FormComponent,
|
||||
id: @property.id,
|
||||
title: @page_title,
|
||||
action: @live_action,
|
||||
property: @property %>
|
||||
<% end %>
|
||||
|
||||
<table class="table table-nonfluid">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th scope="row" class="text-end">Name</th>
|
||||
<td><%= @property.name %></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="text-end">Price</th>
|
||||
<td><%= @property.price %></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="text-end">Description</th>
|
||||
<td><%= @property.description %></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<%= if Roles.can?(@current_user, @property, :index) do %>
|
||||
<%= live_redirect "Back", to: Routes.property_index_path(@socket, :index), class: "btn btn-secondary" %>
|
||||
<% end %>
|
||||
<%= if Roles.can?(@current_user, @property, :edit) do %>
|
||||
<%= live_patch "Edit", to: Routes.property_show_path(@socket, :edit, @property), class: "btn btn-primary" %>
|
||||
<% end %>
|
|
@ -4,7 +4,7 @@ defmodule Shift73kWeb.Roles do
|
|||
"""
|
||||
|
||||
alias Shift73k.Accounts.User
|
||||
alias Shift73k.Properties.Property
|
||||
# alias Shift73k.Properties.Property
|
||||
|
||||
@type entity :: struct()
|
||||
@type action :: :new | :index | :edit | :show | :delete | :edit_role
|
||||
|
@ -12,13 +12,13 @@ defmodule Shift73kWeb.Roles do
|
|||
|
||||
def can?(user, entity, action)
|
||||
|
||||
# Properties / Property
|
||||
def can?(%User{role: :admin}, %Property{}, _any), do: true
|
||||
def can?(%User{}, %Property{}, :index), do: true
|
||||
def can?(%User{}, %Property{}, :new), do: true
|
||||
def can?(%User{}, %Property{}, :show), do: true
|
||||
def can?(%User{id: id}, %Property{user_id: id}, :edit), do: true
|
||||
def can?(%User{id: id}, %Property{user_id: id}, :delete), do: true
|
||||
# # Properties / Property
|
||||
# def can?(%User{role: :admin}, %Property{}, _any), do: true
|
||||
# def can?(%User{}, %Property{}, :index), do: true
|
||||
# def can?(%User{}, %Property{}, :new), do: true
|
||||
# def can?(%User{}, %Property{}, :show), do: true
|
||||
# def can?(%User{id: id}, %Property{user_id: id}, :edit), do: true
|
||||
# def can?(%User{id: id}, %Property{user_id: id}, :delete), do: true
|
||||
|
||||
# Accounts / User
|
||||
def can?(%User{role: :admin}, %User{}, _any), do: true
|
||||
|
|
|
@ -92,12 +92,6 @@ defmodule Shift73kWeb.Router do
|
|||
pipe_through([:browser, :require_authenticated_user, :user])
|
||||
|
||||
live("/user_dashboard", UserDashboardLive, :index)
|
||||
|
||||
live("/properties", PropertyLive.Index, :index)
|
||||
live("/properties/new", PropertyLive.Index, :new)
|
||||
live("/properties/:id/edit", PropertyLive.Index, :edit)
|
||||
live("/properties/:id", PropertyLive.Show, :show)
|
||||
live("/properties/:id/show/edit", PropertyLive.Show, :edit)
|
||||
end
|
||||
|
||||
scope "/", Shift73kWeb do
|
||||
|
|
|
@ -19,9 +19,10 @@
|
|||
<%# nav LEFT items %>
|
||||
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
|
||||
|
||||
<li class="nav-item">
|
||||
<%= link "Properties", nav_link_opts(@conn, to: Routes.property_index_path(@conn, :index), class: "nav-link") %>
|
||||
</li>
|
||||
<%# normal navbar link example %>
|
||||
<%# <li class="nav-item"> %>
|
||||
<%#= link "Properties", nav_link_opts(@conn, to: Routes.property_index_path(@conn, :index), class: "nav-link") %>
|
||||
<%# </li> %>
|
||||
|
||||
<%# ACTIVE page link example %>
|
||||
<%# <li class="nav-item">
|
||||
|
@ -33,7 +34,8 @@
|
|||
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
|
||||
</li> %>
|
||||
|
||||
<li class="nav-item dropdown">
|
||||
<%# normal dropdown menu example %>
|
||||
<%# <li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownExample" data-bs-toggle="dropdown" aria-expanded="false">Dropdown</a>
|
||||
<ul class="dropdown-menu" aria-labelledby="navbarDropdownExample">
|
||||
<li><a class="dropdown-item" href="#">Action</a></li>
|
||||
|
@ -41,7 +43,7 @@
|
|||
<li><hr class="dropdown-divider"></li>
|
||||
<li><a class="dropdown-item" href="#">Something else here</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
</li> %>
|
||||
|
||||
</ul>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue