update things
This commit is contained in:
parent
984a193c5d
commit
b2f2f57029
6 changed files with 27 additions and 9 deletions
|
|
@ -35,7 +35,7 @@ defmodule Zoeyscomputer.Gists do
|
|||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_gist!(id), do: Repo.get!(Gist, id)
|
||||
def get_gist!(id), do: Repo.get!(Gist, id) |> Repo.preload([:author])
|
||||
|
||||
@doc """
|
||||
Creates a gist.
|
||||
|
|
@ -49,7 +49,11 @@ defmodule Zoeyscomputer.Gists do
|
|||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_gist(attrs \\ %{}) do
|
||||
def create_gist(user, attrs \\ %{}) do
|
||||
IO.puts("hereo")
|
||||
attrs = Map.put(attrs, "author_id", user.id)
|
||||
IO.inspect(attrs)
|
||||
|
||||
%Gist{}
|
||||
|> Gist.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
|
|
|
|||
|
|
@ -18,12 +18,14 @@ defmodule Zoeyscomputer.Gists.Gist do
|
|||
@doc false
|
||||
def changeset(gist, attrs) do
|
||||
gist
|
||||
|> cast(attrs, [:code, :lang, :title, :desc])
|
||||
|> validate_required([:code, :lang, :title])
|
||||
|> cast(attrs, [:code, :lang, :title, :desc, :author_id])
|
||||
|> validate_required([:code, :lang, :title, :author_id])
|
||||
|> put_new_id()
|
||||
end
|
||||
|
||||
defp put_new_id(changeset) do
|
||||
IO.puts("assigning new id")
|
||||
|
||||
case get_field(changeset, :id) do
|
||||
nil -> put_change(changeset, :id, IdGenerator.generate(7))
|
||||
_id -> changeset
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ defmodule ZoeyscomputerWeb.GistLive.FormComponent do
|
|||
end
|
||||
|
||||
defp save_gist(socket, :new, gist_params) do
|
||||
case Gists.create_gist(gist_params) do
|
||||
case Gists.create_gist(socket.assigns.current_user, gist_params) do
|
||||
{:ok, gist} ->
|
||||
notify_parent({:saved, gist})
|
||||
|
||||
|
|
@ -98,6 +98,7 @@ defmodule ZoeyscomputerWeb.GistLive.FormComponent do
|
|||
|> push_patch(to: socket.assigns.patch)}
|
||||
|
||||
{:error, %Ecto.Changeset{} = changeset} ->
|
||||
IO.inspect(changeset)
|
||||
{:noreply, assign(socket, form: to_form(changeset))}
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -6,7 +6,13 @@ defmodule ZoeyscomputerWeb.GistLive.Index do
|
|||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
{:ok, stream(socket, :gists, Gists.list_gists())}
|
||||
socket = socket |> assign(:current_user, socket.assigns.current_user)
|
||||
|
||||
if connected?(socket) && socket.assigns.current_user do
|
||||
{:ok, stream(socket, :gists, Gists.list_gists())}
|
||||
else
|
||||
{:ok, stream(socket, :gists, [])}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
|
@ -18,12 +24,14 @@ defmodule ZoeyscomputerWeb.GistLive.Index do
|
|||
defp apply_action(socket, :edit, %{"id" => id}) do
|
||||
socket
|
||||
|> assign(:page_title, "Edit Gist")
|
||||
|> assign(:current_user, socket.assigns.current_user)
|
||||
|> assign(:gist, Gists.get_gist!(id))
|
||||
end
|
||||
|
||||
defp apply_action(socket, :new, _params) do
|
||||
socket
|
||||
|> assign(:page_title, "New Gist")
|
||||
|> assign(:current_user, socket.assigns.current_user)
|
||||
|> assign(:gist, %Gist{
|
||||
code: "",
|
||||
lang: nil
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@
|
|||
id={@gist.id || :new}
|
||||
title={@page_title}
|
||||
action={@live_action}
|
||||
current_user={@current_user}
|
||||
gist={@gist}
|
||||
patch={~p"/gists"}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@
|
|||
<%= @gist.title || "Gist: #{@gist.id}" %>
|
||||
<:subtitle>This is a gist record from your database.</:subtitle>
|
||||
<:actions>
|
||||
<.link patch={~p"/gists/#{@gist}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit gist</.button>
|
||||
</.link>
|
||||
<%= if(@current_user && @gist.author.email == @current_user.email) do %>
|
||||
<.link patch={~p"/gists/#{@gist}/show/edit"} phx-click={JS.push_focus()}>
|
||||
<.button>Edit gist</.button>
|
||||
</.link>
|
||||
<% end %>
|
||||
</:actions>
|
||||
</.header>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue