updates to posts
This commit is contained in:
parent
b051e06454
commit
64aed0e5db
12 changed files with 65 additions and 15 deletions
|
@ -1,5 +1,7 @@
|
||||||
import Config
|
import Config
|
||||||
|
|
||||||
|
config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase
|
||||||
|
|
||||||
config :esbuild,
|
config :esbuild,
|
||||||
version: "0.17.11",
|
version: "0.17.11",
|
||||||
sloanely_but_surely: [
|
sloanely_but_surely: [
|
||||||
|
|
|
@ -2,4 +2,5 @@ import Config
|
||||||
|
|
||||||
config :logger, level: :info
|
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"
|
||||||
|
|
|
@ -21,7 +21,8 @@ defmodule Core.Accounts do
|
||||||
nil
|
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)
|
user = Repo.get_by(Schema.User, username: username)
|
||||||
if User.valid_password?(user, password), do: user
|
if User.valid_password?(user, password), do: user
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,12 +7,18 @@ defmodule Core.Posts do
|
||||||
Core.Repo.get!(Schema.Post, id)
|
Core.Repo.get!(Schema.Post, id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_by_publish_date_and_slug!(%Date{} = publish_date, slug) when is_binary(slug) do
|
def get_published_blog!(%Date{} = publish_date, slug) when is_binary(slug) do
|
||||||
publish_date
|
Post.Query.published()
|
||||||
|> Post.Query.where_publish_date_and_slug(slug)
|
|> Post.Query.where_publish_date_and_slug(publish_date, slug)
|
||||||
|> Core.Repo.one!()
|
|> Core.Repo.one!()
|
||||||
end
|
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
|
def change_post(%Schema.Post{} = post \\ %Schema.Post{}, attrs) do
|
||||||
Post.content_changeset(post, attrs)
|
Post.content_changeset(post, attrs)
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,6 +77,10 @@ defmodule Core.Posts.Post do
|
||||||
order_by(query, [posts: p], desc: :inserted_at, desc: :updated_at)
|
order_by(query, [posts: p], desc: :inserted_at, desc: :updated_at)
|
||||||
end
|
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
|
def where_publish_date_and_slug(query \\ current(), publish_date, slug) do
|
||||||
where(
|
where(
|
||||||
query,
|
query,
|
||||||
|
|
|
@ -24,7 +24,11 @@ defmodule Web.CoreComponents do
|
||||||
end
|
end
|
||||||
|
|
||||||
assigns
|
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(:name, fn -> field.name end)
|
||||||
|> assign_new(:value, fn -> field.value end)
|
|> assign_new(:value, fn -> field.value end)
|
||||||
|> input()
|
|> input()
|
||||||
|
|
|
@ -6,7 +6,7 @@ defmodule Web.PostController do
|
||||||
{month, ""} <- Integer.parse(month),
|
{month, ""} <- Integer.parse(month),
|
||||||
{day, ""} <- Integer.parse(day),
|
{day, ""} <- Integer.parse(day),
|
||||||
{:ok, publish_date} <- Date.new(year, month, day) do
|
{: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
|
conn
|
||||||
|> render_post(post)
|
|> render_post(post)
|
||||||
|
@ -15,7 +15,18 @@ defmodule Web.PostController do
|
||||||
end
|
end
|
||||||
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
|
defp render_post(conn, %Schema.Post{kind: :blog} = blog) do
|
||||||
render(conn, :show_blog, blog: blog)
|
render(conn, :show_blog, blog: blog)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp render_post(conn, %Schema.Post{kind: :status} = status) do
|
||||||
|
render(conn, :show_status, status: status)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
5
lib/web/controllers/post_html/show_status.html.heex
Normal file
5
lib/web/controllers/post_html/show_status.html.heex
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<article>
|
||||||
|
<p>{@status.body}</p>
|
||||||
|
|
||||||
|
<%!-- <footer class="mt-4 border-t border-gray-200"></footer> --%>
|
||||||
|
</article>
|
|
@ -103,7 +103,8 @@ defmodule Web.UserSettingsLive do
|
||||||
|> Map.put(:action, :validate)
|
|> Map.put(:action, :validate)
|
||||||
|> to_form()
|
|> 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
|
end
|
||||||
|
|
||||||
def handle_event("update_username", params, socket) do
|
def handle_event("update_username", params, socket) do
|
||||||
|
@ -117,7 +118,10 @@ defmodule Web.UserSettingsLive do
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> put_flash(:info, "Email updated")
|
|> 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} ->
|
{:error, changeset} ->
|
||||||
{:noreply, assign(socket, :username_form, to_form(Map.put(changeset, :action, :insert)))}
|
{:noreply, assign(socket, :username_form, to_form(Map.put(changeset, :action, :insert)))}
|
||||||
|
|
|
@ -41,6 +41,7 @@ defmodule Web.Router do
|
||||||
delete "/admin/users/log_out", UserSessionController, :delete
|
delete "/admin/users/log_out", UserSessionController, :delete
|
||||||
|
|
||||||
get "/:year/:month/:day/:slug", PostController, :show
|
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
|
# live_session :current_user, on_mount: [{Web.UserAuth, :mount_current_user}] do
|
||||||
# end
|
# end
|
||||||
|
|
8
mix.exs
8
mix.exs
|
@ -47,12 +47,18 @@ defmodule SlaonelyButSurely.MixProject do
|
||||||
{:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
|
{:esbuild, "~> 0.8", runtime: Mix.env() == :dev},
|
||||||
{:tailwind, "~> 0.2", runtime: Mix.env() == :dev},
|
{:tailwind, "~> 0.2", runtime: Mix.env() == :dev},
|
||||||
{:heroicons,
|
{: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"},
|
{:jason, "~> 1.2"},
|
||||||
{:bandit, "~> 1.5"},
|
{:bandit, "~> 1.5"},
|
||||||
|
|
||||||
# Added dependencies
|
# Added dependencies
|
||||||
{:boundary, "~> 0.10.4"},
|
{:boundary, "~> 0.10.4"},
|
||||||
|
{:tzdata, "~> 1.1"},
|
||||||
|
|
||||||
# Added dev and/or test dependencies
|
# Added dev and/or test dependencies
|
||||||
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
|
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},
|
||||||
|
|
|
@ -16,12 +16,17 @@ defmodule Test.Core.PostsTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "get_by_publish_date_and_slug!/2" do
|
describe "get_published_blog!/2" do
|
||||||
@tag blog: :blog
|
@tag blog: :blog
|
||||||
test "returns the post with the given publish date and slug", %{blogs: %{blog: blog}} do
|
test "returns the post with the given publish date and slug", %{blogs: %{blog: blog}} do
|
||||||
assert {:ok, blog} = Core.Posts.publish_post(blog)
|
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 %Schema.Post{} = result
|
||||||
assert result.id == blog.id
|
assert result.id == blog.id
|
||||||
end
|
end
|
||||||
|
@ -29,13 +34,13 @@ defmodule Test.Core.PostsTest do
|
||||||
@tag blog: :blog
|
@tag blog: :blog
|
||||||
test "raises an Ecto.NoResultsError if there is no matching post", %{blogs: %{blog: blog}} do
|
test "raises an Ecto.NoResultsError if there is no matching post", %{blogs: %{blog: blog}} do
|
||||||
assert_raise Ecto.NoResultsError, fn ->
|
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
|
end
|
||||||
|
|
||||||
assert {:ok, blog} = Core.Posts.publish_post(blog)
|
assert {:ok, blog} = Core.Posts.publish_post(blog)
|
||||||
|
|
||||||
assert_raise Ecto.NoResultsError, fn ->
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue