diff --git a/lib/jol/blog.ex b/lib/jol/blog.ex index 45f7e9a..e718e92 100644 --- a/lib/jol/blog.ex +++ b/lib/jol/blog.ex @@ -1,48 +1,26 @@ defmodule JOL.Blog do alias JOL.Blog.Post - alias JOL.Blog.Parser defmodule NotFoundError do defexception [:message, plug_status: 404] end - use NimblePublisher, - build: Post, - from: Application.app_dir(:jol, "priv/posts/**/*.md"), - parser: Parser, - as: :posts + def all_posts do + [] + end - @posts Enum.sort_by(@posts, & &1.date, {:desc, Date}) - @tags @posts |> Enum.flat_map(& &1.tags) |> Enum.uniq() |> Enum.sort() - - @spec all_posts() :: [ - %Post{ - author: String.t(), - body: String.t(), - date: DateTime.t(), - draft: false | nil, - slug: String.t(), - tags: [String.t()], - title: String.t() - } - ] - def all_posts, do: @posts - def unique_tag_list, do: @tags + def unique_tag_list do + [] + end def recent_posts(num \\ 10) do - Enum.take(all_posts(), num) + [] end def get_post_by_slug!(slug) do - Enum.find(all_posts(), &(&1.slug == slug)) || - raise NotFoundError, "post ``slug=#{slug}` not found" end def get_posts_by_tag!(tag) do - case Enum.filter(all_posts(), &(tag in &1.tags)) do - [] -> raise NotFoundError, "posts tagged `#{tag}` not found" - posts -> posts - end end def format_date(date) do diff --git a/lib/jol/blog/post.ex b/lib/jol/blog/post.ex index f3c5383..0e03b1f 100644 --- a/lib/jol/blog/post.ex +++ b/lib/jol/blog/post.ex @@ -1,25 +1,20 @@ defmodule JOL.Blog.Post do - @enforce_keys [:author, :title, :body, :tags, :date, :slug, :lede] - defstruct [:author, :draft, :title, :body, :tags, :date, :slug, :lede] + use Ecto.Schema + import Ecto.Changeset - def build(_filename, attrs, body) do - struct!( - __MODULE__, - [author: "Jessica Phoenix Canady", body: body, lede: extract_lede(body)] ++ - Map.to_list(attrs) - ) + schema "posts" do + field :title, :string + field :body, :string + field :published_at, :naive_datetime + field :slug, :string + + timestamps(type: :utc_datetime) end - defp extract_lede(text) do - case Regex.named_captures(~r/(?.*)\n/s, text) do - %{"lede" => lede} -> - lede - - _ -> - text - |> String.split() - |> Enum.take(50) - |> Enum.join(" ") - end + @doc false + def changeset(post, attrs) do + post + |> cast(attrs, [:title, :body, :published_at, :slug]) + |> validate_required([:title, :body, :published_at, :slug]) end end diff --git a/mix.exs b/mix.exs index d2b361d..76c07a5 100644 --- a/mix.exs +++ b/mix.exs @@ -58,8 +58,6 @@ defmodule JOL.MixProject do {:jason, "~> 1.2"}, {:dns_cluster, "~> 0.1.1"}, {:bandit, "~> 1.2"}, - {:nimble_publisher, "~> 1.1.0"}, - {:toml, "~> 0.7"}, {:atomex, "~> 0.3.0"}, {:tz, "~> 0.27"} ] diff --git a/priv/repo/migrations/20240924145455_create_posts.exs b/priv/repo/migrations/20240924145455_create_posts.exs new file mode 100644 index 0000000..ba5c9a3 --- /dev/null +++ b/priv/repo/migrations/20240924145455_create_posts.exs @@ -0,0 +1,14 @@ +defmodule JOL.Repo.Migrations.CreatePosts do + use Ecto.Migration + + def change do + create table(:posts) do + add :title, :string + add :body, :text + add :published_at, :naive_datetime + add :slug, :string + + timestamps(type: :utc_datetime) + end + end +end