This repository was archived by the owner on Nov 8, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork20
feat: article document concept#409
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
53d4233
chore: setup basic table
mydearxym908ef19
chore: basic create update remove workflow
mydearxyme43bff2
chore: re-org read & paged without body field
mydearxym064ba91
chore: fix Repo create edege case
mydearxymdd52f97
chore: fix body arg choas
mydearxymefbdcee
fix: document issue in hooks
mydearxym0396970
fix: document issue in create
mydearxym5c62809
fix: document issue in create
mydearxym226888d
fix: missing paged blogs tests
mydearxym69de83c
fix: document return issue
mydearxym3474f83
fix: clean up
mydearxymf05776a
fix: clean up
mydearxym8f43d79
fix: thread document test adjust
mydearxymFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 2 additions & 0 deletionsconfig/config.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionslib/groupher_server/cms/cms.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
86 changes: 63 additions & 23 deletionslib/groupher_server/cms/delegates/article_curd.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletionslib/groupher_server/cms/delegates/document.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
defmodule GroupherServer.CMS.Delegate.Document do | ||
@moduledoc """ | ||
CURD operation on post/job ... | ||
""" | ||
import Ecto.Query, warn: false | ||
import Helper.Utils, only: [done: 1, thread_of_article: 2, get_config: 2] | ||
import Helper.ErrorCode | ||
import ShortMaps | ||
alias Helper.{ORM, Converter} | ||
alias GroupherServer.{CMS, Repo} | ||
alias CMS.Model.ArticleDocument | ||
alias Ecto.Multi | ||
# alias Helper.Converter.MdToEditor | ||
alias GroupherServer.Support.Factory | ||
# TODO: spec repo logic | ||
def create(article, %{readme: readme} = attrs) do | ||
# .parse(markdown) | ||
# body = MdToEditor.mock_rich_text(readme) | ||
body = Factory.mock_rich_text(readme) | ||
attrs = attrs |> Map.drop([:readme]) |> Map.put(:body, body) | ||
create(article, attrs) | ||
end | ||
# for create artilce step in Multi.new | ||
def create(article, %{body: body} = attrs) do | ||
with {:ok, article_thread} <- thread_of_article(article, :upcase), | ||
{:ok, parsed} <- Converter.Article.parse_body(body) do | ||
attrs = Map.take(parsed, [:body, :body_html]) | ||
Multi.new() | ||
|> Multi.run(:create_article_document, fn _, _ -> | ||
document_attrs = | ||
Map.merge(attrs, %{ | ||
thread: article_thread, | ||
article_id: article.id, | ||
title: article.title | ||
}) | ||
ArticleDocument |> ORM.create(document_attrs) | ||
end) | ||
|> Multi.run(:create_thread_document, fn _, _ -> | ||
attrs = attrs |> Map.put(foreign_key(article_thread), article.id) | ||
CMS.Model | ||
|> Module.concat("#{Recase.to_title(article_thread)}Document") | ||
|> ORM.create(attrs) | ||
end) | ||
|> Repo.transaction() | ||
|> result() | ||
end | ||
end | ||
@doc """ | ||
update both article and thread document | ||
""" | ||
def update(article, %{body: body} = attrs) when not is_nil(body) do | ||
with {:ok, article_thread} <- thread_of_article(article, :upcase), | ||
{:ok, article_doc} <- find_article_document(article_thread, article), | ||
{:ok, thread_doc} <- find_thread_document(article_thread, article), | ||
{:ok, parsed} <- Converter.Article.parse_body(body) do | ||
attrs = Map.take(parsed, [:body, :body_html]) | ||
Multi.new() | ||
|> Multi.run(:update_article_document, fn _, _ -> | ||
case Map.has_key?(attrs, :title) do | ||
true -> article_doc |> ORM.update(Map.merge(attrs, %{title: attrs.title})) | ||
false -> article_doc |> ORM.update(attrs) | ||
end | ||
end) | ||
|> Multi.run(:update_thread_document, fn _, _ -> | ||
thread_doc |> ORM.update(attrs) | ||
end) | ||
|> Repo.transaction() | ||
|> result() | ||
end | ||
end | ||
# 只更新 title 的情况 | ||
def update(article, %{title: title} = attrs) do | ||
with {:ok, article_thread} <- thread_of_article(article, :upcase), | ||
{:ok, article_doc} <- find_article_document(article_thread, article) do | ||
article_doc |> ORM.update(%{title: attrs.title}) | ||
end | ||
end | ||
def update(article, _), do: {:ok, article} | ||
defp find_article_document(article_thread, article) do | ||
ORM.find_by(ArticleDocument, %{article_id: article.id, thread: article_thread}) | ||
end | ||
defp find_thread_document(article_thread, article) do | ||
CMS.Model | ||
|> Module.concat("#{Recase.to_title(article_thread)}Document") | ||
|> ORM.find_by(Map.put(%{}, foreign_key(article_thread), article.id)) | ||
end | ||
@doc """ | ||
remove article document foever | ||
""" | ||
def remove(thread, id) do | ||
thread = thread |> to_string |> String.upcase() | ||
ArticleDocument |> ORM.findby_delete!(%{thread: thread, article_id: id}) | ||
end | ||
defp foreign_key(article_thread) do | ||
thread_atom = article_thread |> String.downcase() |> String.to_atom() | ||
:"#{thread_atom}_id" | ||
end | ||
defp result({:ok, %{create_thread_document: result}}), do: {:ok, result} | ||
defp result({:ok, %{update_article_document: result}}), do: {:ok, result} | ||
defp result({:error, _, _result, _steps}) do | ||
{:error, [message: "create document", code: ecode(:create_fails)]} | ||
end | ||
end |
6 changes: 6 additions & 0 deletionslib/groupher_server/cms/delegates/hooks/cite.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletionslib/groupher_server/cms/delegates/hooks/mention.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.