diff --git a/config/config.exs b/config/config.exs index 9d9cf50..f46990a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,5 +1,7 @@ import Config +config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase + config :esbuild, version: "0.17.11", sloanely_but_surely: [ diff --git a/config/prod.exs b/config/prod.exs index 6041dfa..e75049f 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -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" diff --git a/lib/core/accounts.ex b/lib/core/accounts.ex index fcdf022..5fba8c4 100644 --- a/lib/core/accounts.ex +++ b/lib/core/accounts.ex @@ -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 diff --git a/lib/core/posts.ex b/lib/core/posts.ex index 533d817..fc5721f 100644 --- a/lib/core/posts.ex +++ b/lib/core/posts.ex @@ -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 diff --git a/lib/core/posts/post.ex b/lib/core/posts/post.ex index b8adb17..d2dad96 100644 --- a/lib/core/posts/post.ex +++ b/lib/core/posts/post.ex @@ -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, diff --git a/lib/web/components/core_components.ex b/lib/web/components/core_components.ex index 92d5c24..5340ada 100644 --- a/lib/web/components/core_components.ex +++ b/lib/web/components/core_components.ex @@ -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() diff --git a/lib/web/controllers/post_controller.ex b/lib/web/controllers/post_controller.ex index e6ecd32..5d28516 100644 --- a/lib/web/controllers/post_controller.ex +++ b/lib/web/controllers/post_controller.ex @@ -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 diff --git a/lib/web/controllers/post_html/show_status.html.heex b/lib/web/controllers/post_html/show_status.html.heex new file mode 100644 index 0000000..1b9608c --- /dev/null +++ b/lib/web/controllers/post_html/show_status.html.heex @@ -0,0 +1,5 @@ +<article> + <p>{@status.body}</p> + + <%!-- <footer class="mt-4 border-t border-gray-200"></footer> --%> +</article> diff --git a/lib/web/live/user_settings_live.ex b/lib/web/live/user_settings_live.ex index 7802340..72f9789 100644 --- a/lib/web/live/user_settings_live.ex +++ b/lib/web/live/user_settings_live.ex @@ -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)))} diff --git a/lib/web/router.ex b/lib/web/router.ex index 1345e26..6f05566 100644 --- a/lib/web/router.ex +++ b/lib/web/router.ex @@ -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 diff --git a/mix.exs b/mix.exs index 9d0073c..5fdb2ab 100644 --- a/mix.exs +++ b/mix.exs @@ -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}, diff --git a/test/core/posts_test.exs b/test/core/posts_test.exs index a48b191..3adacd3 100644 --- a/test/core/posts_test.exs +++ b/test/core/posts_test.exs @@ -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