From 9e0520603f1c9028036116f6640a1a5c92c84cf0 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Thu, 8 Aug 2019 17:36:22 +0800 Subject: [PATCH 1/2] chore: wip --- .../cms/delegates/article_curd.ex | 1 + .../cms/delegates/community_sync.ex | 12 ++++++++++++ lib/helper/common_types.ex | 17 +++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 lib/helper/common_types.ex diff --git a/lib/groupher_server/cms/delegates/article_curd.ex b/lib/groupher_server/cms/delegates/article_curd.ex index 654a88dcd..67852601b 100644 --- a/lib/groupher_server/cms/delegates/article_curd.ex +++ b/lib/groupher_server/cms/delegates/article_curd.ex @@ -130,6 +130,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do end end + @spec ensure_author_exists(User.t()) :: {:ok, User.t()} def ensure_author_exists(%User{} = user) do # unique_constraint: avoid race conditions, make sure user_id unique # foreign_key_constraint: check foreign key: user_id exsit or not diff --git a/lib/groupher_server/cms/delegates/community_sync.ex b/lib/groupher_server/cms/delegates/community_sync.ex index 0156d2f7d..409036425 100644 --- a/lib/groupher_server/cms/delegates/community_sync.ex +++ b/lib/groupher_server/cms/delegates/community_sync.ex @@ -4,6 +4,7 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do """ import Ecto.Query, warn: false import Helper.ErrorCode + import Helper.CommonTypes # import ShortMaps alias Helper.ORM @@ -17,6 +18,7 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ get wiki """ + @spec get_wiki(Community.t()) :: {:ok, CommunityWiki.t()} def get_wiki(%Community{raw: raw}) do with {:ok, community} <- ORM.find_by(Community, raw: raw), {:ok, wiki} <- ORM.find_by(CommunityWiki, community_id: community.id) do @@ -30,6 +32,7 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ get cheatsheet """ + @spec get_cheatsheet(Community.t()) :: {:ok, CommunityCheatsheet.t()} def get_cheatsheet(%Community{raw: raw}) do with {:ok, community} <- ORM.find_by(Community, raw: raw), {:ok, wiki} <- ORM.find_by(CommunityCheatsheet, community_id: community.id) do @@ -43,6 +46,7 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ sync wiki """ + @spec sync_github_content(Community.t(), atom) :: {:ok, CommunityWiki.t()} def sync_github_content(%Community{id: id}, :wiki, attrs) do with {:ok, community} <- ORM.find(Community, id) do attrs = Map.merge(attrs, %{community_id: community.id}) @@ -54,6 +58,7 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ sync cheatsheet """ + @spec sync_github_content(Community.t(), atom()) :: {:ok, CommunityCheatsheet.t()} def sync_github_content(%Community{id: id}, :cheatsheet, attrs) do with {:ok, community} <- ORM.find(Community, id) do attrs = Map.merge(attrs, %{community_id: community.id}) @@ -65,10 +70,17 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ add contributor to exsit wiki contributors list """ + @spec add_contributor(Community.t(), github_contributor()) :: + {:ok, CommunityWiki} | custom_error() def add_contributor(%CommunityWiki{id: id}, contributor_attrs) do do_add_contributor(CommunityWiki, id, contributor_attrs) end + @doc """ + add contributor to exsit cheatsheet contributors list + """ + @spec add_contributor(Community.t(), github_contributor()) :: + {:ok, CommunityCheatsheet} | custom_error() def add_contributor(%CommunityCheatsheet{id: id}, contributor_attrs) do do_add_contributor(CommunityCheatsheet, id, contributor_attrs) end diff --git a/lib/helper/common_types.ex b/lib/helper/common_types.ex new file mode 100644 index 000000000..9b02fec23 --- /dev/null +++ b/lib/helper/common_types.ex @@ -0,0 +1,17 @@ +defmodule Helper.CommonTypes do + @moduledoc """ + common types for lint + """ + + @type github_contributor :: %{ + github_id: String.t(), + avatar: String.t(), + html_url: String.t(), + nickname: String.t(), + bio: nil | String.t(), + location: nil | String.t(), + company: nil | String.t() + } + + @type custom_error :: {:error, [message: String.t(), code: Number.t()]} +end From 6f0ee76932396b8d0def294522dbb7f83810b059 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sun, 11 Aug 2019 15:57:03 +0800 Subject: [PATCH 2/2] refactor: add more spec --- .../cms/delegates/community_sync.ex | 18 ++++++++++-------- lib/helper/common_types.ex | 17 ----------------- lib/helper/spec_type.ex | 13 +++++++++++++ 3 files changed, 23 insertions(+), 25 deletions(-) delete mode 100644 lib/helper/common_types.ex diff --git a/lib/groupher_server/cms/delegates/community_sync.ex b/lib/groupher_server/cms/delegates/community_sync.ex index 409036425..9804fa4d5 100644 --- a/lib/groupher_server/cms/delegates/community_sync.ex +++ b/lib/groupher_server/cms/delegates/community_sync.ex @@ -4,12 +4,14 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do """ import Ecto.Query, warn: false import Helper.ErrorCode - import Helper.CommonTypes # import ShortMaps + alias GroupherServer.CMS + alias Helper.ORM + alias Helper.SpecType, as: T - alias GroupherServer.CMS.{ + alias CMS.{ Community, CommunityWiki, CommunityCheatsheet @@ -46,7 +48,7 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ sync wiki """ - @spec sync_github_content(Community.t(), atom) :: {:ok, CommunityWiki.t()} + @spec sync_github_content(Community.t(), atom(), any()) :: {:ok, CommunityWiki.t()} def sync_github_content(%Community{id: id}, :wiki, attrs) do with {:ok, community} <- ORM.find(Community, id) do attrs = Map.merge(attrs, %{community_id: community.id}) @@ -58,7 +60,7 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ sync cheatsheet """ - @spec sync_github_content(Community.t(), atom()) :: {:ok, CommunityCheatsheet.t()} + @spec sync_github_content(Community.t(), atom(), any()) :: {:ok, CommunityCheatsheet.t()} def sync_github_content(%Community{id: id}, :cheatsheet, attrs) do with {:ok, community} <- ORM.find(Community, id) do attrs = Map.merge(attrs, %{community_id: community.id}) @@ -70,8 +72,8 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ add contributor to exsit wiki contributors list """ - @spec add_contributor(Community.t(), github_contributor()) :: - {:ok, CommunityWiki} | custom_error() + @spec add_contributor(Community.t(), T.github_contributor()) :: + {:ok, CommunityWiki} | T.gq_error() def add_contributor(%CommunityWiki{id: id}, contributor_attrs) do do_add_contributor(CommunityWiki, id, contributor_attrs) end @@ -79,8 +81,8 @@ defmodule GroupherServer.CMS.Delegate.CommunitySync do @doc """ add contributor to exsit cheatsheet contributors list """ - @spec add_contributor(Community.t(), github_contributor()) :: - {:ok, CommunityCheatsheet} | custom_error() + @spec add_contributor(Community.t(), T.github_contributor()) :: + {:ok, CommunityCheatsheet} | T.gq_error() def add_contributor(%CommunityCheatsheet{id: id}, contributor_attrs) do do_add_contributor(CommunityCheatsheet, id, contributor_attrs) end diff --git a/lib/helper/common_types.ex b/lib/helper/common_types.ex deleted file mode 100644 index 9b02fec23..000000000 --- a/lib/helper/common_types.ex +++ /dev/null @@ -1,17 +0,0 @@ -defmodule Helper.CommonTypes do - @moduledoc """ - common types for lint - """ - - @type github_contributor :: %{ - github_id: String.t(), - avatar: String.t(), - html_url: String.t(), - nickname: String.t(), - bio: nil | String.t(), - location: nil | String.t(), - company: nil | String.t() - } - - @type custom_error :: {:error, [message: String.t(), code: Number.t()]} -end diff --git a/lib/helper/spec_type.ex b/lib/helper/spec_type.ex index 0b4a043de..40dd76b42 100644 --- a/lib/helper/spec_type.ex +++ b/lib/helper/spec_type.ex @@ -14,4 +14,17 @@ defmodule Helper.SpecType do @type done :: {:ok, map} | {:error, map} @type id :: non_neg_integer() | String.t() + + @typedoc """ + general contribute type for wiki and cheatshet + """ + @type github_contributor2 :: %{ + github_id: String.t(), + avatar: String.t(), + html_url: String.t(), + nickname: String.t(), + bio: nil | String.t(), + location: nil | String.t(), + company: nil | String.t() + } end