Skip to content

Commit

Permalink
Add functionality to fetch page by title
Browse files Browse the repository at this point in the history
  • Loading branch information
skbolton committed Dec 5, 2022
1 parent 981f4af commit a605824
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/infra/couchdb/content_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ defmodule Infra.CouchDB.ContentRepo do
alias Infra.CouchDB
alias Mantra.Contents.Page

def get_page_by(:id, page_id) do
case CouchDB.Documents.get_document("blocks", page_id) do
{:ok, %{status: 404}} ->
nil

{:ok, %{status: 200, body: doc}} ->
doc = movekeys(doc, [{"_rev", "rev"}, {"_id", "id"}])
Ecto.embedded_load(Page, doc, :atoms)
end
end

def create_page(page_changeset) do
with {:ok, page} <- Changeset.apply_action(page_changeset, :insert) do
page_id = Slug.slugify(page.title, lowercase: false, separator: "__")
Expand Down
2 changes: 2 additions & 0 deletions lib/mantra/contents/content_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ defmodule Mantra.Contents.ContentRepo do

@callback create_page(Changeset.t(Page.t())) ::
{:ok, Page.t()} | {:error, Changeset.t(Page.t())}

@callback get_page_by(:id, page_id :: String.t()) :: Page.t() | nil
end

0 comments on commit a605824

Please sign in to comment.