|
| 1 | +defmodule GroupherServer.CMS.Delegate.WorksCURD do |
| 2 | + @moduledoc """ |
| 3 | + CURD operation on post/job ... |
| 4 | + """ |
| 5 | + import Ecto.Query, warn: false |
| 6 | + import Helper.Utils, only: [done: 1, atom_values_to_upcase: 1] |
| 7 | + import Helper.ErrorCode |
| 8 | + |
| 9 | + import GroupherServer.CMS.Delegate.ArticleCURD, only: [create_article: 4, update_article: 2] |
| 10 | + |
| 11 | + alias GroupherServer.{Accounts, CMS, Repo} |
| 12 | + alias CMS.Model.{Community, Techstack, City, Works} |
| 13 | + alias Accounts.Model.User |
| 14 | + |
| 15 | + alias Helper.ORM |
| 16 | + alias Ecto.Multi |
| 17 | + |
| 18 | + # works can only be published on home community |
| 19 | + def create_works(attrs, %User{} = user) do |
| 20 | + attrs = attrs |> atom_values_to_upcase |
| 21 | + |
| 22 | + with {:ok, home_community} <- ORM.find_by(Community, %{raw: "home"}) do |
| 23 | + Multi.new() |
| 24 | + |> Multi.run(:create_works, fn _, _ -> |
| 25 | + create_article(home_community, :works, attrs, user) |
| 26 | + end) |
| 27 | + |> Multi.run(:update_works_fields, fn _, %{create_works: works} -> |
| 28 | + update_works_fields(works, attrs) |
| 29 | + end) |
| 30 | + |> Repo.transaction() |
| 31 | + |> result() |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + def update_works(%Works{} = works, attrs) do |
| 36 | + attrs = attrs |> atom_values_to_upcase |
| 37 | + |
| 38 | + Multi.new() |
| 39 | + |> Multi.run(:update_works_fields, fn _, _ -> |
| 40 | + update_works_fields(works, attrs) |
| 41 | + end) |
| 42 | + |> Multi.run(:update_works, fn _, %{update_works_fields: works} -> |
| 43 | + update_article(works, attrs) |
| 44 | + end) |
| 45 | + |> Repo.transaction() |
| 46 | + |> result() |
| 47 | + end |
| 48 | + |
| 49 | + # update works spec fields |
| 50 | + defp update_works_fields(%Works{} = works, attrs) do |
| 51 | + techstacks = Map.get(attrs, :techstacks, []) |
| 52 | + cities = Map.get(attrs, :cities, []) |
| 53 | + social_info = Map.get(attrs, :social_info, []) |
| 54 | + app_store = Map.get(attrs, :app_store, []) |
| 55 | + |
| 56 | + with {:ok, techstacks} <- get_or_create_techstacks(techstacks), |
| 57 | + {:ok, cities} <- get_or_create_cities(cities) do |
| 58 | + works = Repo.preload(works, [:techstacks, :cities]) |
| 59 | + |
| 60 | + works |
| 61 | + |> Ecto.Changeset.change() |
| 62 | + |> Ecto.Changeset.put_assoc(:techstacks, works.techstacks ++ techstacks) |
| 63 | + |> Ecto.Changeset.put_assoc(:cities, works.cities ++ cities) |
| 64 | + |> Ecto.Changeset.put_embed(:social_info, social_info) |
| 65 | + |> Ecto.Changeset.put_embed(:app_store, app_store) |
| 66 | + |> Repo.update() |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + defp get_or_create_cities([]), do: {:ok, []} |
| 71 | + |
| 72 | + defp get_or_create_cities(cities) do |
| 73 | + cities |
| 74 | + |> Enum.map(&String.downcase(&1)) |
| 75 | + |> Enum.reduce([], fn title, acc -> |
| 76 | + with {:ok, city} <- get_city(title) do |
| 77 | + acc ++ [city] |
| 78 | + end |
| 79 | + end) |
| 80 | + |> done |
| 81 | + end |
| 82 | + |
| 83 | + defp get_city(title) do |
| 84 | + case ORM.find_by(City, %{title: title}) do |
| 85 | + {:error, _} -> create_city(title) |
| 86 | + {:ok, city} -> {:ok, city} |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + defp create_city(title) do |
| 91 | + attrs = |
| 92 | + case ORM.find_by(Community, %{raw: title}) do |
| 93 | + {:ok, community} -> |
| 94 | + %{ |
| 95 | + title: community.title, |
| 96 | + logo: community.logo, |
| 97 | + desc: community.desc, |
| 98 | + link: "/#{community.raw}" |
| 99 | + } |
| 100 | + |
| 101 | + {:error, _} -> |
| 102 | + %{title: title} |
| 103 | + end |
| 104 | + |
| 105 | + ORM.create(City, attrs) |
| 106 | + end |
| 107 | + |
| 108 | + defp get_or_create_techstacks([]), do: {:ok, []} |
| 109 | + |
| 110 | + defp get_or_create_techstacks(techstacks) do |
| 111 | + techstacks |
| 112 | + |> Enum.map(&String.downcase(&1)) |
| 113 | + |> Enum.reduce([], fn title, acc -> |
| 114 | + with {:ok, techstack} <- get_techstack(title) do |
| 115 | + acc ++ [techstack] |
| 116 | + end |
| 117 | + end) |
| 118 | + |> done |
| 119 | + end |
| 120 | + |
| 121 | + defp get_techstack(title) do |
| 122 | + case ORM.find_by(Techstack, %{title: title}) do |
| 123 | + {:error, _} -> create_techstack(title) |
| 124 | + {:ok, techstack} -> {:ok, techstack} |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + defp create_techstack(title) do |
| 129 | + attrs = |
| 130 | + case ORM.find_by(Community, %{raw: title}) do |
| 131 | + {:ok, community} -> |
| 132 | + %{ |
| 133 | + title: community.title, |
| 134 | + logo: community.logo, |
| 135 | + community_link: "/#{community.raw}", |
| 136 | + desc: community.desc |
| 137 | + } |
| 138 | + |
| 139 | + {:error, _} -> |
| 140 | + %{title: title} |
| 141 | + end |
| 142 | + |
| 143 | + ORM.create(Techstack, attrs) |
| 144 | + end |
| 145 | + |
| 146 | + defp result({:ok, %{create_works: result}}), do: {:ok, result} |
| 147 | + defp result({:ok, %{update_works: result}}), do: {:ok, result} |
| 148 | + |
| 149 | + defp result({:error, :create_works, _result, _steps}) do |
| 150 | + {:error, [message: "create works", code: ecode(:create_fails)]} |
| 151 | + end |
| 152 | + |
| 153 | + defp result({:error, :update_works_fields, _result, _steps}) do |
| 154 | + {:error, [message: "update works fields", code: ecode(:create_fails)]} |
| 155 | + end |
| 156 | + |
| 157 | + defp result({:error, :update_works, _result, _steps}) do |
| 158 | + {:error, [message: "update works", code: ecode(:update_fails)]} |
| 159 | + end |
| 160 | +end |
0 commit comments