Create post schema, ditch NimblePublisher.
This commit is contained in:
parent
923e1562a8
commit
fa9760a94b
|
@ -1,48 +1,26 @@
|
||||||
defmodule JOL.Blog do
|
defmodule JOL.Blog do
|
||||||
alias JOL.Blog.Post
|
alias JOL.Blog.Post
|
||||||
alias JOL.Blog.Parser
|
|
||||||
|
|
||||||
defmodule NotFoundError do
|
defmodule NotFoundError do
|
||||||
defexception [:message, plug_status: 404]
|
defexception [:message, plug_status: 404]
|
||||||
end
|
end
|
||||||
|
|
||||||
use NimblePublisher,
|
def all_posts do
|
||||||
build: Post,
|
[]
|
||||||
from: Application.app_dir(:jol, "priv/posts/**/*.md"),
|
end
|
||||||
parser: Parser,
|
|
||||||
as: :posts
|
|
||||||
|
|
||||||
@posts Enum.sort_by(@posts, & &1.date, {:desc, Date})
|
def unique_tag_list do
|
||||||
@tags @posts |> Enum.flat_map(& &1.tags) |> Enum.uniq() |> Enum.sort()
|
[]
|
||||||
|
end
|
||||||
@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 recent_posts(num \\ 10) do
|
def recent_posts(num \\ 10) do
|
||||||
Enum.take(all_posts(), num)
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_post_by_slug!(slug) do
|
def get_post_by_slug!(slug) do
|
||||||
Enum.find(all_posts(), &(&1.slug == slug)) ||
|
|
||||||
raise NotFoundError, "post ``slug=#{slug}` not found"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_posts_by_tag!(tag) do
|
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
|
end
|
||||||
|
|
||||||
def format_date(date) do
|
def format_date(date) do
|
||||||
|
|
|
@ -1,25 +1,20 @@
|
||||||
defmodule JOL.Blog.Post do
|
defmodule JOL.Blog.Post do
|
||||||
@enforce_keys [:author, :title, :body, :tags, :date, :slug, :lede]
|
use Ecto.Schema
|
||||||
defstruct [:author, :draft, :title, :body, :tags, :date, :slug, :lede]
|
import Ecto.Changeset
|
||||||
|
|
||||||
def build(_filename, attrs, body) do
|
schema "posts" do
|
||||||
struct!(
|
field :title, :string
|
||||||
__MODULE__,
|
field :body, :string
|
||||||
[author: "Jessica Phoenix Canady", body: body, lede: extract_lede(body)] ++
|
field :published_at, :naive_datetime
|
||||||
Map.to_list(attrs)
|
field :slug, :string
|
||||||
)
|
|
||||||
|
timestamps(type: :utc_datetime)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp extract_lede(text) do
|
@doc false
|
||||||
case Regex.named_captures(~r/(?<lede>.*)\n<!-- more -->/s, text) do
|
def changeset(post, attrs) do
|
||||||
%{"lede" => lede} ->
|
post
|
||||||
lede
|
|> cast(attrs, [:title, :body, :published_at, :slug])
|
||||||
|
|> validate_required([:title, :body, :published_at, :slug])
|
||||||
_ ->
|
|
||||||
text
|
|
||||||
|> String.split()
|
|
||||||
|> Enum.take(50)
|
|
||||||
|> Enum.join(" ")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
2
mix.exs
2
mix.exs
|
@ -58,8 +58,6 @@ defmodule JOL.MixProject do
|
||||||
{:jason, "~> 1.2"},
|
{:jason, "~> 1.2"},
|
||||||
{:dns_cluster, "~> 0.1.1"},
|
{:dns_cluster, "~> 0.1.1"},
|
||||||
{:bandit, "~> 1.2"},
|
{:bandit, "~> 1.2"},
|
||||||
{:nimble_publisher, "~> 1.1.0"},
|
|
||||||
{:toml, "~> 0.7"},
|
|
||||||
{:atomex, "~> 0.3.0"},
|
{:atomex, "~> 0.3.0"},
|
||||||
{:tz, "~> 0.27"}
|
{:tz, "~> 0.27"}
|
||||||
]
|
]
|
||||||
|
|
14
priv/repo/migrations/20240924145455_create_posts.exs
Normal file
14
priv/repo/migrations/20240924145455_create_posts.exs
Normal file
|
@ -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
|
Loading…
Reference in a new issue