1- defmodule GroupherServer.CMS.Helper.Matcher2 do
1+ defmodule GroupherServer.CMS.Helper.Matcher2.Macros do
22 @ moduledoc """
3- this module defined the matches and handy guard ...
3+ generate match functions
44 """
55
6- import Ecto.Query , warn: false
6+ alias GroupherServer.CMS
7+ alias CMS . { ArticleComment , Community , Embeds }
78
8- alias GroupherServer . { Accounts , CMS }
9+ @ article_threads Community . article_threads ( )
910
10- alias Accounts.User
11- alias CMS . { ArticleComment , Post , Job , Repo }
11+ @ doc """
12+ match basic threads
1213
13- def match ( :article_comment ) do
14- { :ok ,
15- % {
16- model: ArticleComment ,
17- foreign_key: :article_comment_id ,
18- preload: :article_comment ,
19- default_meta: CMS.Embeds.ArticleCommentMeta . default_meta ( )
20- } }
14+ {:ok, info} <- match(:post)
15+ info:
16+ %{
17+ model: Post,
18+ thread: :post,
19+ foreign_key: post_id,
20+ preload: :post
21+ default_meta: ...
22+ }
23+ """
24+ defmacro thread_matches ( ) do
25+ @ article_threads
26+ |> Enum . map ( fn thread ->
27+ quote do
28+ def match ( unquote ( thread ) ) do
29+ thread_module = unquote ( thread ) |> to_string |> Recase . to_pascal ( )
30+
31+ { :ok ,
32+ % {
33+ model: Module . concat ( CMS , thread_module ) ,
34+ thread: unquote ( thread ) ,
35+ foreign_key: unquote ( :"#{ thread } _id" ) ,
36+ preload: unquote ( thread ) ,
37+ default_meta: Embeds.ArticleMeta . default_meta ( )
38+ } }
39+ end
40+ end
41+ end )
2142 end
2243
23- def match ( :comment_article , % ArticleComment { post_id: post_id } ) when not is_nil ( post_id ) do
24- { :ok , % { model: Post , id: post_id , foreign_key: :post_id } }
25- end
44+ @ doc """
45+ match basic thread query
2646
27- def match ( :comment_article , % ArticleComment { job_id: job_id } ) when not is_nil ( job_id ) do
28- { :ok , % { model: Job , id: job_id , foreign_key: :job_id } }
47+ {:ok, info} <- match(:post, :query, id)
48+ info:
49+ %{dynamic([c], field(c, :post_id) == ^id)}
50+ """
51+ defmacro thread_query_matches ( ) do
52+ @ article_threads
53+ |> Enum . map ( fn thread ->
54+ quote do
55+ def match ( unquote ( thread ) , :query , id ) do
56+ { :ok , dynamic ( [ c ] , field ( c , unquote ( :"#{ thread } _id" ) ) == ^ id ) }
57+ end
58+ end
59+ end )
2960 end
3061
31- def match ( :comment_article , % ArticleComment { repo_id: repo_id } ) when not is_nil ( repo_id ) do
32- { :ok , % { model: Repo , id: repo_id , foreign_key: :repo_id } }
33- end
62+ @ doc """
63+ mapping basic article_comment -> thread
3464
35- def match ( :comment_article , % ArticleComment { } ) do
36- { :error , "match error, not supported" }
65+ {:ok, info} <- match(:comment_article, %ArticleComment{post_id: id} = comment)
66+ info:
67+ %{
68+ id: id,
69+ model: CMS.Post,
70+ foreign_key: :post_id,
71+ }
72+ """
73+ defmacro comment_article_matches ( ) do
74+ @ article_threads
75+ |> Enum . map ( fn thread ->
76+ # def match(:comment_article, %ArticleComment{post_id: id})
77+ quote do
78+ # see https://elixirforum.com/t/generate-map-pattern-matching-functions/21928/2
79+ def match ( :comment_article , % ArticleComment { unquote ( :"#{ thread } _id" ) => id } )
80+ when not is_nil ( id ) do
81+ thread_module = unquote ( thread ) |> to_string |> Recase . to_pascal ( )
82+
83+ { :ok ,
84+ % {
85+ id: id ,
86+ model: Module . concat ( CMS , thread_module ) ,
87+ foreign_key: unquote ( :"#{ thread } _id" )
88+ } }
89+ end
90+ end
91+ end )
3792 end
93+ end
94+
95+ defmodule GroupherServer.CMS.Helper.Matcher2 do
96+ @ moduledoc """
97+ this module defined the matches and handy guard ...
98+ """
99+
100+ import Ecto.Query , warn: false
101+ import GroupherServer.CMS.Helper.Matcher2.Macros
102+
103+ alias GroupherServer . { Accounts , CMS }
104+
105+ alias Accounts.User
106+ alias CMS . { ArticleComment }
38107
39108 def match ( :account ) do
40109 { :ok ,
@@ -46,49 +115,17 @@ defmodule GroupherServer.CMS.Helper.Matcher2 do
46115 } }
47116 end
48117
49- # used for paged pin articles
50- def match ( Post ) do
51- { :ok , % { thread: :post } }
52- end
53-
54- def match ( :post ) do
55- { :ok ,
56- % {
57- model: Post ,
58- foreign_key: :post_id ,
59- preload: :post ,
60- default_meta: CMS.Embeds.ArticleMeta . default_meta ( )
61- } }
62- end
63-
64- def match ( Job ) do
65- { :ok , % { thread: :job } }
66- end
67-
68- def match ( :job ) do
69- { :ok ,
70- % {
71- model: Job ,
72- foreign_key: :job_id ,
73- preload: :job ,
74- default_meta: CMS.Embeds.ArticleMeta . default_meta ( )
75- } }
76- end
77-
78- def match ( Repo ) do
79- { :ok , % { thread: :repo } }
80- end
81-
82- def match ( :repo ) do
118+ def match ( :article_comment ) do
83119 { :ok ,
84120 % {
85- model: Repo ,
86- foreign_key: :repo_id ,
87- preload: :repo ,
88- default_meta: CMS.Embeds.ArticleMeta . default_meta ( )
121+ model: ArticleComment ,
122+ foreign_key: :article_comment_id ,
123+ preload: :article_comment ,
124+ default_meta: CMS.Embeds.ArticleCommentMeta . default_meta ( )
89125 } }
90126 end
91127
92- def match ( :post , :query , id ) , do: { :ok , dynamic ( [ c ] , c . post_id == ^ id ) }
93- def match ( :job , :query , id ) , do: { :ok , dynamic ( [ c ] , c . job_id == ^ id ) }
128+ thread_matches ( )
129+ thread_query_matches ( )
130+ comment_article_matches ( )
94131end
0 commit comments