Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit e7b33cb

Browse files
authored
Merge pull request #63 from coderplanets/user-detail
feat: add edu & work background && refactor
2 parents dc65785 + 21be0f2 commit e7b33cb

File tree

13 files changed

+345
-20
lines changed

13 files changed

+345
-20
lines changed

lib/mastani_server/accounts/delegates/profile.ex

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,33 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
1414

1515
@default_subscribed_communities get_config(:general, :default_subscribed_communities)
1616

17-
def update_profile(%User{id: id}, attrs \\ %{}) do
18-
with {:ok, user} <- ORM.find(User, id) do
19-
case user.id === id do
20-
true -> user |> ORM.update(attrs)
21-
false -> {:error, "Error: not qualified"}
17+
@doc """
18+
update user's profile
19+
"""
20+
def update_profile(%User{} = user, attrs \\ %{}) do
21+
changeset =
22+
user
23+
|> Ecto.Changeset.change(attrs)
24+
25+
changeset =
26+
cond do
27+
Map.has_key?(attrs, :education_backgrounds) ->
28+
changeset
29+
|> Ecto.Changeset.put_embed(:education_backgrounds, attrs.education_backgrounds)
30+
31+
Map.has_key?(attrs, :work_backgrounds) ->
32+
changeset
33+
|> Ecto.Changeset.put_embed(:work_backgrounds, attrs.work_backgrounds)
34+
35+
Map.has_key?(attrs, :other_embeds) ->
36+
changeset
37+
|> Ecto.Changeset.put_embed(:other_embeds, attrs.other_embeds)
38+
39+
true ->
40+
changeset
2241
end
23-
end
42+
43+
changeset |> User.update_changeset(attrs) |> Repo.update()
2444
end
2545

2646
@doc """
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule MastaniServer.Accounts.EducationBackground do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
@required_fields ~w(school)a
9+
@optional_fields ~w(major)a
10+
11+
@type t :: %EducationBackground{}
12+
embedded_schema do
13+
field(:school, :string)
14+
field(:major, :string)
15+
end
16+
17+
@doc false
18+
def changeset(%EducationBackground{} = education_background, attrs) do
19+
education_background
20+
|> cast(attrs, @optional_fields ++ @required_fields)
21+
|> validate_required(@required_fields)
22+
end
23+
end

lib/mastani_server/accounts/user.ex

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ defmodule MastaniServer.Accounts.User do
88
alias MastaniServer.Accounts.{
99
Achievement,
1010
Customization,
11+
EducationBackground,
1112
FavoriteCategory,
1213
GithubUser,
1314
Purchase,
1415
UserBill,
1516
UserFollower,
16-
UserFollowing
17+
UserFollowing,
18+
WorkBackground
1719
}
1820

1921
alias MastaniServer.CMS
2022

23+
@required_fields ~w(nickname avatar)a
24+
@optional_fields ~w(nickname bio sex location email company education qq weichat weibo)a
25+
2126
@type t :: %User{}
2227
schema "users" do
2328
field(:nickname, :string)
@@ -26,8 +31,16 @@ defmodule MastaniServer.Accounts.User do
2631
field(:bio, :string)
2732
field(:email, :string)
2833
field(:location, :string)
34+
2935
field(:education, :string)
3036
field(:company, :string)
37+
38+
# TODO
39+
# field(:twitter, :string)
40+
# field(:facebook, :string)
41+
embeds_many(:education_backgrounds, EducationBackground)
42+
embeds_many(:work_backgrounds, WorkBackground)
43+
3144
field(:qq, :string)
3245
field(:weibo, :string)
3346
field(:weichat, :string)
@@ -56,17 +69,21 @@ defmodule MastaniServer.Accounts.User do
5669
timestamps(type: :utc_datetime)
5770
end
5871

59-
@required_fields ~w(nickname avatar)a
60-
@optional_fields ~w(nickname bio avatar sex location email company education qq weichat weibo)a
61-
6272
@doc false
6373
def changeset(%User{} = user, attrs) do
64-
# |> cast(attrs, [:username, :nickname, :bio, :company])
65-
# |> validate_required([:username])
66-
# |> cast(attrs, @required_fields, @optional_fields)
6774
user
6875
|> cast(attrs, @optional_fields ++ @required_fields)
76+
|> update_changeset(attrs)
6977
|> validate_required(@required_fields)
78+
79+
# |> unique_constraint(:username)
80+
end
81+
82+
def update_changeset(user, attrs) do
83+
user
84+
|> cast(attrs, @optional_fields ++ @required_fields)
85+
|> cast_embed(:education_backgrounds, with: &EducationBackground.changeset/2)
86+
|> cast_embed(:work_backgrounds, with: &WorkBackground.changeset/2)
7087
|> validate_length(:nickname, min: 3, max: 30)
7188
|> validate_length(:bio, min: 3, max: 100)
7289
|> validate_inclusion(:sex, ["dude", "girl"])
@@ -76,7 +93,5 @@ defmodule MastaniServer.Accounts.User do
7693
|> validate_length(:qq, min: 8, max: 15)
7794
|> validate_length(:weichat, min: 3, max: 30)
7895
|> validate_length(:weibo, min: 3, max: 30)
79-
80-
# |> unique_constraint(:username)
8196
end
8297
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
defmodule MastaniServer.Accounts.WorkBackground do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
@required_fields ~w(company)a
9+
@optional_fields ~w(title)a
10+
11+
@type t :: %WorkBackground{}
12+
embedded_schema do
13+
field(:company, :string)
14+
field(:title, :string)
15+
end
16+
17+
@doc false
18+
def changeset(%WorkBackground{} = work_background, attrs) do
19+
work_background
20+
|> cast(attrs, @optional_fields ++ @required_fields)
21+
|> validate_required(@required_fields)
22+
end
23+
end

lib/mastani_server_web/middleware/changeset_errors.ex

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,36 @@
33
# see https://hexdocs.pm/absinthe/Absinthe.Middleware.html#content
44
# ---
55
defmodule MastaniServerWeb.Middleware.ChangesetErrors do
6+
@moduledoc """
7+
translate changeset into Graphql-spec with i18n support
8+
"""
9+
610
@behaviour Absinthe.Middleware
711
import Helper.Utils, only: [handle_absinthe_error: 3]
812
import Helper.ErrorCode
913

1014
alias MastaniServerWeb.Gettext, as: Translator
1115

1216
def call(%{errors: [%Ecto.Changeset{} = changeset]} = resolution, _) do
13-
# IO.inspect changeset, label: "Changeset error"
14-
# IO.inspect transform_errors(changeset), label: "transform_errors"
1517
resolution
1618
|> handle_absinthe_error(transform_errors(changeset), ecode(:changeset))
1719
end
1820

1921
def call(resolution, _), do: resolution
2022

23+
# handle special embed schema errors
24+
defp transform_errors(%Ecto.Changeset{errors: [], valid?: false} = changeset) do
25+
first_errored_embed_changeset =
26+
changeset.changes
27+
|> Map.values()
28+
|> List.flatten()
29+
|> Enum.filter(&is_map/1)
30+
|> Enum.filter(fn x -> x.valid? == false end)
31+
|> List.first()
32+
33+
transform_errors(first_errored_embed_changeset)
34+
end
35+
2136
defp transform_errors(changeset) do
2237
changeset
2338
|> Ecto.Changeset.traverse_errors(&format_error/1)

lib/mastani_server_web/schema/account/account_misc.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ defmodule MastaniServerWeb.Schema.Account.Misc do
3131
field(:public_gists, :integer)
3232
end
3333

34+
input_object :education_background do
35+
field(:school, :string)
36+
field(:major, :string)
37+
end
38+
39+
input_object :work_background do
40+
field(:company, :string)
41+
field(:title, :string)
42+
end
43+
3444
input_object :user_profile_input do
3545
field(:nickname, :string)
3646
field(:bio, :string)
@@ -42,6 +52,8 @@ defmodule MastaniServerWeb.Schema.Account.Misc do
4252
field(:qq, :string)
4353
field(:weibo, :string)
4454
field(:weichat, :string)
55+
field(:education_backgrounds, list_of(:education_background))
56+
field(:work_backgrounds, list_of(:work_background))
4557
end
4658

4759
# see: https://github.com/absinthe-graphql/absinthe/issues/206

lib/mastani_server_web/schema/account/account_types.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ defmodule MastaniServerWeb.Schema.Account.Types do
1414
field(:is_valid, :boolean)
1515
end
1616

17+
object :education_background do
18+
field(:school, :string)
19+
field(:major, :string)
20+
end
21+
22+
object :work_background do
23+
field(:company, :string)
24+
field(:title, :string)
25+
end
26+
1727
object :user do
1828
field(:id, :id)
1929
field(:nickname, :string)
@@ -33,6 +43,9 @@ defmodule MastaniServerWeb.Schema.Account.Types do
3343
field(:github_profile, :github_profile, resolve: dataloader(Accounts, :github_profile))
3444
field(:achievement, :achievement, resolve: dataloader(Accounts, :achievement))
3545

46+
field(:education_backgrounds, list_of(:education_background))
47+
field(:work_backgrounds, list_of(:work_background))
48+
3649
# field(:favorites_categories, :paged_favorites_category) do
3750
# arg(:filter, non_null(:common_paged_filter))
3851

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule MastaniServer.Repo.Migrations.CreateEducationBackgrounds do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:education_backgrounds) do
6+
add(:user_id, references(:users, on_delete: :delete_all), null: false)
7+
add(:school, :string)
8+
add(:major, :string)
9+
end
10+
11+
create(unique_index(:education_backgrounds, [:user_id]))
12+
end
13+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule MastaniServer.Repo.Migrations.AddEducationBackgroundsToUsers do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:users) do
6+
add(:education_backgrounds, :map)
7+
end
8+
end
9+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule MastaniServer.Repo.Migrations.AddWorkBackgroundsToUsers do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:users) do
6+
add(:work_backgrounds, :map)
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)