|
| 1 | +defmoduleGroupherServer.CMS.Delegate.WorksCURDdo |
| 2 | +@moduledoc""" |
| 3 | + CURD operation on post/job ... |
| 4 | + """ |
| 5 | +importEcto.Query,warn:false |
| 6 | +importHelper.Utils,only:[done:1,atom_values_to_upcase:1] |
| 7 | +importHelper.ErrorCode |
| 8 | + |
| 9 | +importGroupherServer.CMS.Delegate.ArticleCURD,only:[create_article:4,update_article:2] |
| 10 | + |
| 11 | +aliasGroupherServer.{Accounts,CMS,Repo} |
| 12 | +aliasCMS.Model.{Community,Techstack,City,Works} |
| 13 | +aliasAccounts.Model.User |
| 14 | + |
| 15 | +aliasHelper.ORM |
| 16 | +aliasEcto.Multi |
| 17 | + |
| 18 | +# works can only be published on home community |
| 19 | +defcreate_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 | +defupdate_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 | +defpupdate_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 | +defpget_or_create_cities([]),do:{:ok,[]} |
| 71 | + |
| 72 | +defpget_or_create_cities(cities)do |
| 73 | +cities |
| 74 | +|>Enum.map(&String.downcase(&1)) |
| 75 | +|>Enum.reduce([],fntitle,acc-> |
| 76 | +with{:ok,city}<-get_city(title)do |
| 77 | +acc++[city] |
| 78 | +end |
| 79 | +end) |
| 80 | +|>done |
| 81 | +end |
| 82 | + |
| 83 | +defpget_city(title)do |
| 84 | +caseORM.find_by(City,%{title:title})do |
| 85 | +{:error,_}->create_city(title) |
| 86 | +{:ok,city}->{:ok,city} |
| 87 | +end |
| 88 | +end |
| 89 | + |
| 90 | +defpcreate_city(title)do |
| 91 | +attrs= |
| 92 | +caseORM.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 | +defpget_or_create_techstacks([]),do:{:ok,[]} |
| 109 | + |
| 110 | +defpget_or_create_techstacks(techstacks)do |
| 111 | +techstacks |
| 112 | +|>Enum.map(&String.downcase(&1)) |
| 113 | +|>Enum.reduce([],fntitle,acc-> |
| 114 | +with{:ok,techstack}<-get_techstack(title)do |
| 115 | +acc++[techstack] |
| 116 | +end |
| 117 | +end) |
| 118 | +|>done |
| 119 | +end |
| 120 | + |
| 121 | +defpget_techstack(title)do |
| 122 | +caseORM.find_by(Techstack,%{title:title})do |
| 123 | +{:error,_}->create_techstack(title) |
| 124 | +{:ok,techstack}->{:ok,techstack} |
| 125 | +end |
| 126 | +end |
| 127 | + |
| 128 | +defpcreate_techstack(title)do |
| 129 | +attrs= |
| 130 | +caseORM.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 | +defpresult({:ok,%{create_works:result}}),do:{:ok,result} |
| 147 | +defpresult({:ok,%{update_works:result}}),do:{:ok,result} |
| 148 | + |
| 149 | +defpresult({:error,:create_works,_result,_steps})do |
| 150 | +{:error,[message:"create works",code:ecode(:create_fails)]} |
| 151 | +end |
| 152 | + |
| 153 | +defpresult({:error,:update_works_fields,_result,_steps})do |
| 154 | +{:error,[message:"update works fields",code:ecode(:create_fails)]} |
| 155 | +end |
| 156 | + |
| 157 | +defpresult({:error,:update_works,_result,_steps})do |
| 158 | +{:error,[message:"update works",code:ecode(:update_fails)]} |
| 159 | +end |
| 160 | +end |