update things

This commit is contained in:
zack 2024-10-26 22:38:41 -04:00
parent 984a193c5d
commit b2f2f57029
No known key found for this signature in database
GPG key ID: 5F873416BCF59F35
6 changed files with 27 additions and 9 deletions

View file

@ -35,7 +35,7 @@ defmodule Zoeyscomputer.Gists do
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
def get_gist!(id), do: Repo.get!(Gist, id) def get_gist!(id), do: Repo.get!(Gist, id) |> Repo.preload([:author])
@doc """ @doc """
Creates a gist. Creates a gist.
@ -49,7 +49,11 @@ defmodule Zoeyscomputer.Gists do
{:error, %Ecto.Changeset{}} {: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{}
|> Gist.changeset(attrs) |> Gist.changeset(attrs)
|> Repo.insert() |> Repo.insert()

View file

@ -18,12 +18,14 @@ defmodule Zoeyscomputer.Gists.Gist do
@doc false @doc false
def changeset(gist, attrs) do def changeset(gist, attrs) do
gist gist
|> cast(attrs, [:code, :lang, :title, :desc]) |> cast(attrs, [:code, :lang, :title, :desc, :author_id])
|> validate_required([:code, :lang, :title]) |> validate_required([:code, :lang, :title, :author_id])
|> put_new_id() |> put_new_id()
end end
defp put_new_id(changeset) do defp put_new_id(changeset) do
IO.puts("assigning new id")
case get_field(changeset, :id) do case get_field(changeset, :id) do
nil -> put_change(changeset, :id, IdGenerator.generate(7)) nil -> put_change(changeset, :id, IdGenerator.generate(7))
_id -> changeset _id -> changeset

View file

@ -88,7 +88,7 @@ defmodule ZoeyscomputerWeb.GistLive.FormComponent do
end end
defp save_gist(socket, :new, gist_params) do 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} -> {:ok, gist} ->
notify_parent({:saved, gist}) notify_parent({:saved, gist})
@ -98,6 +98,7 @@ defmodule ZoeyscomputerWeb.GistLive.FormComponent do
|> push_patch(to: socket.assigns.patch)} |> push_patch(to: socket.assigns.patch)}
{:error, %Ecto.Changeset{} = changeset} -> {:error, %Ecto.Changeset{} = changeset} ->
IO.inspect(changeset)
{:noreply, assign(socket, form: to_form(changeset))} {:noreply, assign(socket, form: to_form(changeset))}
end end
end end

View file

@ -6,7 +6,13 @@ defmodule ZoeyscomputerWeb.GistLive.Index do
@impl true @impl true
def mount(_params, _session, socket) do 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 end
@impl true @impl true
@ -18,12 +24,14 @@ defmodule ZoeyscomputerWeb.GistLive.Index do
defp apply_action(socket, :edit, %{"id" => id}) do defp apply_action(socket, :edit, %{"id" => id}) do
socket socket
|> assign(:page_title, "Edit Gist") |> assign(:page_title, "Edit Gist")
|> assign(:current_user, socket.assigns.current_user)
|> assign(:gist, Gists.get_gist!(id)) |> assign(:gist, Gists.get_gist!(id))
end end
defp apply_action(socket, :new, _params) do defp apply_action(socket, :new, _params) do
socket socket
|> assign(:page_title, "New Gist") |> assign(:page_title, "New Gist")
|> assign(:current_user, socket.assigns.current_user)
|> assign(:gist, %Gist{ |> assign(:gist, %Gist{
code: "", code: "",
lang: nil lang: nil

View file

@ -38,6 +38,7 @@
id={@gist.id || :new} id={@gist.id || :new}
title={@page_title} title={@page_title}
action={@live_action} action={@live_action}
current_user={@current_user}
gist={@gist} gist={@gist}
patch={~p"/gists"} patch={~p"/gists"}
/> />

View file

@ -2,9 +2,11 @@
<%= @gist.title || "Gist: #{@gist.id}" %> <%= @gist.title || "Gist: #{@gist.id}" %>
<:subtitle>This is a gist record from your database.</:subtitle> <:subtitle>This is a gist record from your database.</:subtitle>
<:actions> <:actions>
<.link patch={~p"/gists/#{@gist}/show/edit"} phx-click={JS.push_focus()}> <%= if(@current_user && @gist.author.email == @current_user.email) do %>
<.button>Edit gist</.button> <.link patch={~p"/gists/#{@gist}/show/edit"} phx-click={JS.push_focus()}>
</.link> <.button>Edit gist</.button>
</.link>
<% end %>
</:actions> </:actions>
</.header> </.header>