Create post schema, ditch NimblePublisher.
This commit is contained in:
parent
923e1562a8
commit
fa9760a94b
|
@ -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
|
||||
|
|
|
@ -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/(?<lede>.*)\n<!-- more -->/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
|
||||
|
|
2
mix.exs
2
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"}
|
||||
]
|
||||
|
|
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