-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathrepo.ex
More file actions
31 lines (25 loc) · 667 Bytes
/
Copy pathrepo.ex
File metadata and controls
31 lines (25 loc) · 667 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
defmodule StaticBlog.Repo do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, :ok, [name: __MODULE__])
end
def list() do
GenServer.call(__MODULE__, {:list})
end
def get_by_slug(slug) do
GenServer.call(__MODULE__, {:get_by_slug, slug})
end
def init(:ok) do
posts = StaticBlog.Crawler.crawl
{:ok, posts}
end
def handle_call({:list}, _from, posts) do
{:reply, {:ok, posts}, posts}
end
def handle_call({:get_by_slug, slug}, _from, posts) do
case Enum.find(posts, fn(x) -> x.slug == slug end) do
nil -> {:reply, :not_found, posts}
post -> {:reply, {:ok, post}, posts}
end
end
end