Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitdc2f95b

Browse files
authored
refactor(post-meta): use build-in embed schema (#313)
1 parentf303cb5 commitdc2f95b

File tree

8 files changed

+68
-59
lines changed

8 files changed

+68
-59
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmoduleGroupherServer.CMS.ArticleMetado
2+
@moduledoc"""
3+
general article meta info for article-like content, like post, job, works ...
4+
"""
5+
useEcto.Schema
6+
7+
@default_meta%{
8+
is_edited:false,
9+
forbid_comment:false,
10+
is_reported:false
11+
# linkedPostsCount: 0,
12+
# linkedJobsCount: 0,
13+
# linkedWorksCount: 0,
14+
# reaction: %{
15+
# rocketCount: 0,
16+
# heartCount: 0,
17+
# }
18+
}
19+
20+
@doc"for test usage"
21+
defdefault_meta(),do:@default_meta
22+
23+
embedded_schemado
24+
field(:is_edited,:boolean,default:false)
25+
field(:forbid_comment,:boolean,default:false)
26+
field(:is_reported,:boolean,default:false)
27+
end
28+
end

‎lib/groupher_server/cms/delegates/article_operation.ex‎

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
1010
aliasHelper.ORM
1111

1212
aliasGroupherServer.CMS.{
13+
ArticleMeta,
1314
Community,
1415
Post,
1516
PostCommunityFlag,
@@ -29,22 +30,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2930
aliasGroupherServer.CMS.Repo,as:CMSRepo
3031
aliasGroupherServer.Repo
3132

32-
@default_article_meta%{
33-
isEdited:false,
34-
forbidComment:false,
35-
isReported:false
36-
# linkedPostsCount: 0,
37-
# linkedJobsCount: 0,
38-
# linkedWorksCount: 0,
39-
# reaction: %{
40-
# rocketCount: 0,
41-
# heartCount: 0,
42-
# }
43-
}
44-
45-
@doc"for test usage"
46-
defdefault_article_meta(),do:@default_article_meta
47-
4833
defpin_content(%Post{id:post_id},%Community{id:community_id},topic)do
4934
with{:ok,%{id:topic_id}}<-ORM.find_by(Topic,%{raw:topic}),
5035
{:ok,pined}<-
@@ -294,19 +279,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
294279
defset_topic(_topic,_thread,_content_id),do:{:ok,:pass}
295280

296281
@doc"set meta info"
282+
# embeds_one do not have default option, so we init it with empty map mannully
283+
# see: https://github.com/elixir-ecto/ecto/issues/2634
297284
defset_meta(:post,content_id)do
298-
ORM.update_by(Post,[id:content_id],%{meta:@default_article_meta})
285+
ORM.update_by(Post,[id:content_id],%{meta:%{}})
299286
end
300287

301288
defset_meta(_,_),do:{:ok,:pass}
302289

303290
@doc"update isEdited meta label if needed"
304-
defupdate_meta(%Post{meta:%{"isEdited"=>false}=meta}=content,:is_edited)do
305-
ORM.update(content,%{meta:Map.merge(meta,%{"isEdited"=>true})})
291+
defupdate_meta(%Post{meta:%ArticleMeta{is_edited:false}=meta}=content,:is_edited)do
292+
new_meta=meta|>Map.from_struct()|>Map.delete(:id)|>Map.merge(%{is_edited:true})
293+
294+
content
295+
|>Ecto.Changeset.change()
296+
|>Ecto.Changeset.put_embed(:meta,new_meta)
297+
|>Repo.update()
306298
end
307299

300+
# for test or exsiting articles
308301
defupdate_meta(%Post{meta:nil}=content,:is_edited)do
309-
ORM.update(content,%{meta:Map.merge(@default_article_meta,%{"isEdited"=>true})})
302+
new_meta=ArticleMeta.default_meta()|>Map.merge(%{is_edited:true})
303+
304+
content
305+
|>Ecto.Changeset.change()
306+
|>Ecto.Changeset.put_embed(:meta,new_meta)
307+
|>Repo.update()
310308
end
311309

312310
defupdate_meta(content,_),do:{:ok,content}

‎lib/groupher_server/cms/post.ex‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Post do
88
aliasGroupherServer.CMS
99

1010
aliasCMS.{
11+
ArticleMeta,
1112
Author,
1213
Community,
1314
PostComment,
@@ -23,7 +24,8 @@ defmodule GroupherServer.CMS.Post do
2324

2425
@timestamps_opts[type::utc_datetime_usec]
2526
@required_fields~w(title body digest length)a
26-
@optional_fields~w(origial_community_id link_addr copy_right link_addr link_icon meta)a
27+
@optional_fields~w(origial_community_id link_addr copy_right link_addr link_icon)a
28+
@embed_fileds~w(meta)a
2729

2830
@typet::%Post{}
2931
schema"cms_posts"do
@@ -36,7 +38,9 @@ defmodule GroupherServer.CMS.Post do
3638
field(:length,:integer)
3739
field(:views,:integer,default:0)
3840

39-
field(:meta,:map)
41+
embeds_one(:meta,ArticleMeta,on_replace::update)
42+
43+
# field(:meta, :map)
4044

4145
has_many(:community_flags,{"posts_communities_flags",PostCommunityFlag})
4246

‎lib/groupher_server_web/resolvers/cms_resolver.ex‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ defmodule GroupherServerWeb.Resolvers.CMS do
1313
aliasHelper.ORM
1414
aliasHelper.Utils
1515

16-
@default_article_metaCMS.Delegate.ArticleOperation.default_article_meta()
17-
1816
# #######################
1917
# community ..
2018
# #######################
@@ -444,21 +442,4 @@ defmodule GroupherServerWeb.Resolvers.CMS do
444442
deftags_count(root,_,_)do
445443
CMS.count(%Community{id:root.id},:tags)
446444
end
447-
448-
@doc"""
449-
covert normal map to absinthe fmt
450-
e.g:
451-
%{"exampleKey" => false } -> %{example_key: false }
452-
"""
453-
defget_article_meta(root,_,_)do
454-
# if meta is nil , means exsit article or test env (like: db_insert)
455-
meta=ifis_nil(root.meta),do:@default_article_meta,else:root.meta
456-
457-
fmt_meta=
458-
meta
459-
|>Utils.snake_map_key()
460-
|>Utils.keys_to_atoms()
461-
462-
{:ok,fmt_meta}
463-
end
464445
end

‎lib/groupher_server_web/schema/cms/cms_types.ex‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
3939
field(:communities,list_of(:community),resolve:dataloader(CMS,:communities))
4040
# field(:topic)
4141

42-
# article meta info
43-
field(:meta,:article_meta)do
44-
# NOTE: absinthe has issue with :map resolving, do it mannulay
45-
resolve(&R.CMS.get_article_meta/3)
46-
end
42+
field(:meta,:article_meta)
4743

4844
# field :meta, :article_meta do
4945
# resolve(&R.CMS.get_meta/3)

‎test/groupher_server/cms/post_meta_test.exs‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
defmoduleGroupherServer.Test.CMS.PostMetado
2+
@moduledocfalse
23
useGroupherServer.TestTools
34

45
aliasHelper.ORM
56
aliasGroupherServer.CMS
6-
aliasHelper.Utils
77

8-
@default_article_metaCMS.Delegate.ArticleOperation.default_article_meta()
8+
aliasCMS.{ArticleMeta,Author,Post}
9+
10+
@default_article_metaArticleMeta.default_meta()
911

1012
setupdo
1113
{:ok,user}=db_insert(:user)
@@ -18,29 +20,28 @@ defmodule GroupherServer.Test.CMS.PostMeta do
1820
end
1921

2022
describe"[cms post meta info]"do
21-
aliasCMS.{Author,Post}
22-
23-
@tag:wip
23+
@tag:wip2
2424
test"can get default meta info",~m(user community post_attrs)ado
2525
assert{:error,_}=ORM.find_by(Author,user_id:user.id)
2626

2727
{:ok,post}=CMS.create_content(community,:post,post_attrs,user)
2828
{:ok,post}=ORM.find_by(Post,id:post.id)
29+
meta=post.meta|>Map.from_struct()|>Map.delete(:id)
2930

30-
assert@default_article_meta==Utils.keys_to_atoms(post.meta)
31+
assert@default_article_meta==meta
3132
end
3233

33-
@tag:wip
34-
test"isEdited flag should set to true after post updated",~m(user community post_attrs)ado
34+
@tag:wip2
35+
test"is_edited flag should set to true after post updated",~m(user community post_attrs)ado
3536
{:ok,post}=CMS.create_content(community,:post,post_attrs,user)
3637
{:ok,post}=ORM.find_by(Post,id:post.id)
3738

38-
assertpost.meta["isEdited"]==false
39+
assertpost.meta.is_edited==false
3940

4041
{:ok,_}=CMS.update_content(post,%{"title"=>"new title"})
4142
{:ok,post}=ORM.find_by(Post,id:post.id)
4243

43-
assertpost.meta["isEdited"]==true
44+
assertpost.meta.is_edited==true
4445
end
4546

4647
# test "post with image should have imageCount in meta" do

‎test/groupher_server_web/mutation/cms/post_test.exs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ defmodule GroupherServer.Test.Mutation.Post do
268268
assertupdated_post["copyRight"]==variables.copyRight
269269
end
270270

271-
test"update post with valid attrs should have isEdited meta info update",
271+
@tag:wip2
272+
test"update post with valid attrs should have is_edited meta info update",
272273
~m(owner_conn post)ado
273274
unique_num=System.unique_integer([:positive,:monotonic])
274275

‎test/groupher_server_web/query/cms/post_test.exs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule GroupherServer.Test.Query.Post do
2828
}
2929
}
3030
"""
31-
#@tag :wip2
31+
@tag:wip2
3232
test"basic graphql query on post with logined user",
3333
~m(user_conn community user post_attrs)ado
3434
{:ok,post}=CMS.create_content(community,:post,post_attrs,user)
@@ -43,7 +43,7 @@ defmodule GroupherServer.Test.Query.Post do
4343
assertlength(Map.keys(results))==4
4444
end
4545

46-
@tag:wip2
46+
#@tag :wip2
4747
test"basic graphql query on post with stranger(unloged user)",~m(guest_conn post)ado
4848
variables=%{id:post.id}
4949
results=guest_conn|>query_result(@query,variables,"post")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp