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
chore: works GQ endpoint#432
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
cc39fef
chore: wip
mydearxymd78a568
chore: wip
mydearxym03bea61
chore: basic create
mydearxym6a6fdf5
refactor(works): use Multi to re-org create/update
mydearxymf95aed3
refactor(works): gq endpoint wip
mydearxym6607375
refactor(works): gq endpoint wip
mydearxymc99cfd4
refactor(works): update GQ endpoint
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
4 changes: 4 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
7 changes: 6 additions & 1 deletionlib/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
25 changes: 1 addition & 24 deletionslib/groupher_server/cms/delegates/blog_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
160 changes: 160 additions & 0 deletionslib/groupher_server/cms/delegates/works_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
defmodule GroupherServer.CMS.Delegate.WorksCURD do | ||
@moduledoc """ | ||
CURD operation on post/job ... | ||
""" | ||
import Ecto.Query, warn: false | ||
import Helper.Utils, only: [done: 1, atom_values_to_upcase: 1] | ||
import Helper.ErrorCode | ||
import GroupherServer.CMS.Delegate.ArticleCURD, only: [create_article: 4, update_article: 2] | ||
alias GroupherServer.{Accounts, CMS, Repo} | ||
alias CMS.Model.{Community, Techstack, City, Works} | ||
alias Accounts.Model.User | ||
alias Helper.ORM | ||
alias Ecto.Multi | ||
# works can only be published on home community | ||
def create_works(attrs, %User{} = user) do | ||
attrs = attrs |> atom_values_to_upcase | ||
with {:ok, home_community} <- ORM.find_by(Community, %{raw: "home"}) do | ||
Multi.new() | ||
|> Multi.run(:create_works, fn _, _ -> | ||
create_article(home_community, :works, attrs, user) | ||
end) | ||
|> Multi.run(:update_works_fields, fn _, %{create_works: works} -> | ||
update_works_fields(works, attrs) | ||
end) | ||
|> Repo.transaction() | ||
|> result() | ||
end | ||
end | ||
def update_works(%Works{} = works, attrs) do | ||
attrs = attrs |> atom_values_to_upcase | ||
Multi.new() | ||
|> Multi.run(:update_works_fields, fn _, _ -> | ||
update_works_fields(works, attrs) | ||
end) | ||
|> Multi.run(:update_works, fn _, %{update_works_fields: works} -> | ||
update_article(works, attrs) | ||
end) | ||
|> Repo.transaction() | ||
|> result() | ||
end | ||
# update works spec fields | ||
defp update_works_fields(%Works{} = works, attrs) do | ||
techstacks = Map.get(attrs, :techstacks, []) | ||
cities = Map.get(attrs, :cities, []) | ||
social_info = Map.get(attrs, :social_info, []) | ||
app_store = Map.get(attrs, :app_store, []) | ||
with {:ok, techstacks} <- get_or_create_techstacks(techstacks), | ||
{:ok, cities} <- get_or_create_cities(cities) do | ||
works = Repo.preload(works, [:techstacks, :cities]) | ||
works | ||
|> Ecto.Changeset.change() | ||
|> Ecto.Changeset.put_assoc(:techstacks, works.techstacks ++ techstacks) | ||
|> Ecto.Changeset.put_assoc(:cities, works.cities ++ cities) | ||
|> Ecto.Changeset.put_embed(:social_info, social_info) | ||
|> Ecto.Changeset.put_embed(:app_store, app_store) | ||
|> Repo.update() | ||
end | ||
end | ||
defp get_or_create_cities([]), do: {:ok, []} | ||
defp get_or_create_cities(cities) do | ||
cities | ||
|> Enum.map(&String.downcase(&1)) | ||
|> Enum.reduce([], fn title, acc -> | ||
with {:ok, city} <- get_city(title) do | ||
acc ++ [city] | ||
end | ||
end) | ||
|> done | ||
end | ||
defp get_city(title) do | ||
case ORM.find_by(City, %{title: title}) do | ||
{:error, _} -> create_city(title) | ||
{:ok, city} -> {:ok, city} | ||
end | ||
end | ||
defp create_city(title) do | ||
attrs = | ||
case ORM.find_by(Community, %{raw: title}) do | ||
{:ok, community} -> | ||
%{ | ||
title: community.title, | ||
logo: community.logo, | ||
desc: community.desc, | ||
link: "/#{community.raw}" | ||
} | ||
{:error, _} -> | ||
%{title: title} | ||
end | ||
ORM.create(City, attrs) | ||
end | ||
defp get_or_create_techstacks([]), do: {:ok, []} | ||
defp get_or_create_techstacks(techstacks) do | ||
techstacks | ||
|> Enum.map(&String.downcase(&1)) | ||
|> Enum.reduce([], fn title, acc -> | ||
with {:ok, techstack} <- get_techstack(title) do | ||
acc ++ [techstack] | ||
end | ||
end) | ||
|> done | ||
end | ||
defp get_techstack(title) do | ||
case ORM.find_by(Techstack, %{title: title}) do | ||
{:error, _} -> create_techstack(title) | ||
{:ok, techstack} -> {:ok, techstack} | ||
end | ||
end | ||
defp create_techstack(title) do | ||
attrs = | ||
case ORM.find_by(Community, %{raw: title}) do | ||
{:ok, community} -> | ||
%{ | ||
title: community.title, | ||
logo: community.logo, | ||
community_link: "/#{community.raw}", | ||
desc: community.desc | ||
} | ||
{:error, _} -> | ||
%{title: title} | ||
end | ||
ORM.create(Techstack, attrs) | ||
end | ||
defp result({:ok, %{create_works: result}}), do: {:ok, result} | ||
defp result({:ok, %{update_works: result}}), do: {:ok, result} | ||
defp result({:error, :create_works, _result, _steps}) do | ||
{:error, [message: "create works", code: ecode(:create_fails)]} | ||
end | ||
defp result({:error, :update_works_fields, _result, _steps}) do | ||
{:error, [message: "update works fields", code: ecode(:create_fails)]} | ||
end | ||
defp result({:error, :update_works, _result, _steps}) do | ||
{:error, [message: "update works", code: ecode(:update_fails)]} | ||
end | ||
end |
47 changes: 47 additions & 0 deletionslib/groupher_server/cms/models/city.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,47 @@ | ||
defmodule GroupherServer.CMS.Model.City do | ||
@moduledoc false | ||
alias __MODULE__ | ||
use Ecto.Schema | ||
use Accessible | ||
import Ecto.Changeset | ||
# alias GroupherServer.CMS | ||
@timestamps_opts [type: :utc_datetime_usec] | ||
@required_fields ~w(title)a | ||
@optional_fields ~w(logo desc link)a | ||
@type t :: %City{} | ||
schema "cms_cities" do | ||
## mailstone | ||
field(:title, :string) | ||
field(:logo, :string) | ||
field(:desc, :string) | ||
field(:link, :string) | ||
timestamps() | ||
end | ||
@doc false | ||
def changeset(%City{} = city, attrs) do | ||
city | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> validate_required(@required_fields) | ||
|> generl_changeset | ||
end | ||
@doc false | ||
def update_changeset(%City{} = city, attrs) do | ||
city | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> generl_changeset | ||
end | ||
defp generl_changeset(changeset) do | ||
changeset | ||
|> validate_length(:title, min: 1, max: 100) | ||
end | ||
end |
23 changes: 23 additions & 0 deletionslib/groupher_server/cms/models/embeds/app_store.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,23 @@ | ||
defmodule GroupherServer.CMS.Model.Embeds.AppStore do | ||
@moduledoc """ | ||
general community meta | ||
""" | ||
use Ecto.Schema | ||
use Accessible | ||
import Ecto.Changeset | ||
@required_fields ~w(platform)a | ||
@optional_fields ~w(link)a | ||
embedded_schema do | ||
field(:platform, :string) | ||
field(:link, :string) | ||
end | ||
def changeset(struct, attrs) do | ||
struct | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> validate_required(@required_fields) | ||
end | ||
end |
23 changes: 23 additions & 0 deletionslib/groupher_server/cms/models/embeds/social_info.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,23 @@ | ||
defmodule GroupherServer.CMS.Model.Embeds.SocialInfo do | ||
@moduledoc """ | ||
general community meta | ||
""" | ||
use Ecto.Schema | ||
use Accessible | ||
import Ecto.Changeset | ||
@required_fields ~w(platform)a | ||
@optional_fields ~w(link)a | ||
embedded_schema do | ||
field(:platform, :string) | ||
field(:link, :string) | ||
end | ||
def changeset(struct, attrs) do | ||
struct | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> validate_required(@required_fields) | ||
end | ||
end |
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.