@@ -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
8297end
0 commit comments