updates to posts

This commit is contained in:
sloane 2025-03-25 22:11:52 -04:00
parent b051e06454
commit 64aed0e5db
Signed by: sloanelybutsurely
SSH key fingerprint: SHA256:8SBnwhl+RY3oEyQxy1a9wByPzxWM0x+/Ejc+sIlY5qQ
12 changed files with 65 additions and 15 deletions

View file

@ -1,5 +1,7 @@
import Config
config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase
config :esbuild,
version: "0.17.11",
sloanely_but_surely: [

View file

@ -2,4 +2,5 @@ import Config
config :logger, level: :info
config :sloanely_but_surely, Web.Endpoint, cache_static_manifest: "priv/static/cache_manifest.json"
config :sloanely_but_surely, Web.Endpoint,
cache_static_manifest: "priv/static/cache_manifest.json"

View file

@ -21,7 +21,8 @@ defmodule Core.Accounts do
nil
"""
def get_user_by_username_and_password(username, password) when is_binary(username) and is_binary(password) do
def get_user_by_username_and_password(username, password)
when is_binary(username) and is_binary(password) do
user = Repo.get_by(Schema.User, username: username)
if User.valid_password?(user, password), do: user
end

View file

@ -7,12 +7,18 @@ defmodule Core.Posts do
Core.Repo.get!(Schema.Post, id)
end
def get_by_publish_date_and_slug!(%Date{} = publish_date, slug) when is_binary(slug) do
publish_date
|> Post.Query.where_publish_date_and_slug(slug)
def get_published_blog!(%Date{} = publish_date, slug) when is_binary(slug) do
Post.Query.published()
|> Post.Query.where_publish_date_and_slug(publish_date, slug)
|> Core.Repo.one!()
end
def get_published_status!(id) do
Post.Query.published()
|> Post.Query.where_kind(:status)
|> Core.Repo.get!(id)
end
def change_post(%Schema.Post{} = post \\ %Schema.Post{}, attrs) do
Post.content_changeset(post, attrs)
end

View file

@ -77,6 +77,10 @@ defmodule Core.Posts.Post do
order_by(query, [posts: p], desc: :inserted_at, desc: :updated_at)
end
def where_kind(query \\ base(), kind) do
where(query, [posts: p], p.kind == ^kind)
end
def where_publish_date_and_slug(query \\ current(), publish_date, slug) do
where(
query,

View file

@ -24,7 +24,11 @@ defmodule Web.CoreComponents do
end
assigns
|> assign(field: nil, id: assigns.id || field.id, errors: Enum.map(errors, &translate_error/1))
|> assign(
field: nil,
id: assigns.id || field.id,
errors: Enum.map(errors, &translate_error/1)
)
|> assign_new(:name, fn -> field.name end)
|> assign_new(:value, fn -> field.value end)
|> input()

View file

@ -6,7 +6,7 @@ defmodule Web.PostController do
{month, ""} <- Integer.parse(month),
{day, ""} <- Integer.parse(day),
{:ok, publish_date} <- Date.new(year, month, day) do
post = Core.Posts.get_by_publish_date_and_slug!(publish_date, slug)
post = Core.Posts.get_published_blog!(publish_date, slug)
conn
|> render_post(post)
@ -15,7 +15,18 @@ defmodule Web.PostController do
end
end
def show(conn, %{"status_id" => status_id}) do
status = Core.Posts.get_published_status!(status_id)
conn
|> render_post(status)
end
defp render_post(conn, %Schema.Post{kind: :blog} = blog) do
render(conn, :show_blog, blog: blog)
end
defp render_post(conn, %Schema.Post{kind: :status} = status) do
render(conn, :show_status, status: status)
end
end

View file

@ -0,0 +1,5 @@
<article>
<p>{@status.body}</p>
<%!-- <footer class="mt-4 border-t border-gray-200"></footer> --%>
</article>

View file

@ -103,7 +103,8 @@ defmodule Web.UserSettingsLive do
|> Map.put(:action, :validate)
|> to_form()
{:noreply, assign(socket, username_form: username_form, username_form_current_password: password)}
{:noreply,
assign(socket, username_form: username_form, username_form_current_password: password)}
end
def handle_event("update_username", params, socket) do
@ -117,7 +118,10 @@ defmodule Web.UserSettingsLive do
{:noreply,
socket
|> put_flash(:info, "Email updated")
|> assign(username_form_current_password: nil, username_form: to_form(username_changeset))}
|> assign(
username_form_current_password: nil,
username_form: to_form(username_changeset)
)}
{:error, changeset} ->
{:noreply, assign(socket, :username_form, to_form(Map.put(changeset, :action, :insert)))}

View file

@ -41,6 +41,7 @@ defmodule Web.Router do
delete "/admin/users/log_out", UserSessionController, :delete
get "/:year/:month/:day/:slug", PostController, :show
get "/status/:status_id", PostController, :show
# live_session :current_user, on_mount: [{Web.UserAuth, :mount_current_user}] do
# end

View file

@ -47,12 +47,18 @@ defmodule SlaonelyButSurely.MixProject do
{:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
{:tailwind, "~> 0.2", runtime: Mix.env() == :dev},
{:heroicons,
github: "tailwindlabs/heroicons", tag: "v2.1.1", sparse: "optimized", app: false, compile: false, depth: 1},
github: "tailwindlabs/heroicons",
tag: "v2.1.1",
sparse: "optimized",
app: false,
compile: false,
depth: 1},
{:jason, "~> 1.2"},
{:bandit, "~> 1.5"},
# Added dependencies
{:boundary, "~> 0.10.4"},
{:tzdata, "~> 1.1"},
# Added dev and/or test dependencies
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},

View file

@ -16,12 +16,17 @@ defmodule Test.Core.PostsTest do
end
end
describe "get_by_publish_date_and_slug!/2" do
describe "get_published_blog!/2" do
@tag blog: :blog
test "returns the post with the given publish date and slug", %{blogs: %{blog: blog}} do
assert {:ok, blog} = Core.Posts.publish_post(blog)
publish_date = DateTime.to_date(blog.published_at)
assert result = Core.Posts.get_by_publish_date_and_slug!(publish_date, blog.slug)
publish_date =
blog.published_at
|> DateTime.shift_zone!("America/New_York")
|> DateTime.to_date()
assert result = Core.Posts.get_published_blog!(publish_date, blog.slug)
assert %Schema.Post{} = result
assert result.id == blog.id
end
@ -29,13 +34,13 @@ defmodule Test.Core.PostsTest do
@tag blog: :blog
test "raises an Ecto.NoResultsError if there is no matching post", %{blogs: %{blog: blog}} do
assert_raise Ecto.NoResultsError, fn ->
Core.Posts.get_by_publish_date_and_slug!(~D[1993-11-28], "first-day-on-earth")
Core.Posts.get_published_blog!(~D[1993-11-28], "first-day-on-earth")
end
assert {:ok, blog} = Core.Posts.publish_post(blog)
assert_raise Ecto.NoResultsError, fn ->
Core.Posts.get_by_publish_date_and_slug!(~D[1993-11-28], blog.slug)
Core.Posts.get_published_blog!(~D[1993-11-28], blog.slug)
end
end
end