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

Commit 14ff5b5

Browse files
authored
Merge pull request #64 from coderplanets/user-detail
User detail
2 parents c71b849 + 43de507 commit 14ff5b5

File tree

8 files changed

+41
-27
lines changed

8 files changed

+41
-27
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ launch.prod:
4444

4545
migrate:
4646
mix ecto.migrate
47+
migrate.mock:
48+
MIX_ENV=mock mix ecto.migrate
4749
rollback:
4850
mix ecto.rollback
4951
migrate.mock:
@@ -63,6 +65,8 @@ gen:
6365
@echo "\n"
6466
gen.migration:
6567
mix ecto.gen.migration $(arg)
68+
gen.migration.mock:
69+
MIX_ENV=mock mix ecto.gen.migration $(arg)
6670
gen.context:
6771
mix phx.gen.context $(arg)
6872

@@ -117,6 +121,8 @@ test.watch:
117121
mix test.watch
118122
test.watch.wip:
119123
mix test.watch --only wip
124+
test.watch.wip2:
125+
mix test.watch --only wip2
120126
test.db_reset:
121127
env MIX_ENV=test mix ecto.drop
122128
env MIX_ENV=test mix ecto.create

lib/mastani_server/accounts/delegates/profile.ex

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,28 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
112112
end
113113
end
114114

115-
defp create_user(user, :github) do
116-
user = %User{
117-
nickname: user["login"],
118-
avatar: user["avatar_url"],
119-
bio: user["bio"],
120-
location: user["location"],
121-
email: user["email"],
122-
company: user["company"],
115+
defp create_user(profile, :github) do
116+
attrs = %{
117+
nickname: profile["login"],
118+
avatar: profile["avatar_url"],
119+
bio: profile["bio"],
120+
location: profile["location"],
121+
email: profile["email"],
123122
from_github: true
124123
}
125124

126-
Repo.insert(user)
125+
changeset =
126+
case profile |> Map.has_key?("company") do
127+
true ->
128+
%User{}
129+
|> Ecto.Changeset.change(attrs)
130+
|> Ecto.Changeset.put_embed(:work_backgrounds, [%{company: profile["company"]}])
131+
132+
false ->
133+
%User{} |> Ecto.Changeset.change(attrs)
134+
end
135+
136+
Repo.insert(changeset)
127137
end
128138

129139
defp create_profile(user, github_profile, :github) do

lib/mastani_server/accounts/user.ex

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule MastaniServer.Accounts.User do
2121
alias MastaniServer.CMS
2222

2323
@required_fields ~w(nickname avatar)a
24-
@optional_fields ~w(nickname bio sex location email company education qq weichat weibo)a
24+
@optional_fields ~w(nickname bio sex location email qq weichat weibo)a
2525

2626
@type t :: %User{}
2727
schema "users" do
@@ -32,9 +32,6 @@ defmodule MastaniServer.Accounts.User do
3232
field(:email, :string)
3333
field(:location, :string)
3434

35-
field(:education, :string)
36-
field(:company, :string)
37-
3835
# TODO
3936
# field(:twitter, :string)
4037
# field(:facebook, :string)
@@ -72,7 +69,6 @@ defmodule MastaniServer.Accounts.User do
7269
@doc false
7370
def changeset(%User{} = user, attrs) do
7471
user
75-
|> cast(attrs, @optional_fields ++ @required_fields)
7672
|> update_changeset(attrs)
7773
|> validate_required(@required_fields)
7874

@@ -89,7 +85,6 @@ defmodule MastaniServer.Accounts.User do
8985
|> validate_inclusion(:sex, ["dude", "girl"])
9086
|> validate_format(:email, ~r/@/)
9187
|> validate_length(:location, min: 2, max: 30)
92-
|> validate_length(:company, min: 3, max: 30)
9388
|> validate_length(:qq, min: 8, max: 15)
9489
|> validate_length(:weichat, min: 3, max: 30)
9590
|> validate_length(:weibo, min: 3, max: 30)

lib/mastani_server_web/schema/account/account_misc.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ defmodule MastaniServerWeb.Schema.Account.Misc do
4545
field(:nickname, :string)
4646
field(:bio, :string)
4747
field(:sex, :string)
48-
field(:education, :string)
4948
field(:location, :string)
50-
field(:company, :string)
5149
field(:email, :string)
5250
field(:qq, :string)
5351
field(:weibo, :string)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule MastaniServer.Repo.Migrations.RemoveEducationCompanyInUsers do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:users) do
6+
remove(:company)
7+
remove(:education)
8+
end
9+
end
10+
end

test/mastani_server/accounts/accounts_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ defmodule MastaniServer.Test.Accounts do
3535
assert updated.sex == attrs.sex
3636
end
3737

38-
@tag :wip
3938
test "update user education backgorunds with valid attrs" do
4039
{:ok, user} = db_insert(:user)
4140

@@ -57,7 +56,6 @@ defmodule MastaniServer.Test.Accounts do
5756
assert updated.education_backgrounds |> Enum.any?(&(&1.major == "bad ass"))
5857
end
5958

60-
@tag :wip
6159
test "update user work backgorunds with valid attrs" do
6260
{:ok, user} = db_insert(:user)
6361

@@ -79,7 +77,6 @@ defmodule MastaniServer.Test.Accounts do
7977
assert updated.work_backgrounds |> Enum.any?(&(&1.title == "bad ass"))
8078
end
8179

82-
@tag :wip
8380
test "update user with invalid attrs fails ?" do
8481
{:ok, user} = db_insert(:user)
8582

@@ -90,7 +87,7 @@ defmodule MastaniServer.Test.Accounts do
9087
end
9188

9289
describe "[github login]" do
93-
alias Accounts.{User, GithubUser}
90+
alias Accounts.{GithubUser, User}
9491

9592
test "register a valid github user with non-exist in db" do
9693
assert {:error, _} =
@@ -109,7 +106,10 @@ defmodule MastaniServer.Test.Accounts do
109106
assert created_user.bio == @valid_github_profile["bio"]
110107

111108
assert created_user.email == @valid_github_profile["email"]
112-
assert created_user.company == @valid_github_profile["company"]
109+
110+
company_title = created_user.work_backgrounds |> List.first() |> Map.get(:company)
111+
assert company_title == @valid_github_profile["company"]
112+
113113
assert created_user.location == @valid_github_profile["location"]
114114
assert created_user.from_github == true
115115

test/mastani_server_web/mutation/accounts/account_test.exs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do
4444
assert updated["nickname"] == "new nickname"
4545
end
4646

47-
@tag :wip
4847
test "user can update it's own education_backgrounds", ~m(user)a do
4948
ownd_conn = simu_conn(:user, user)
5049

@@ -75,7 +74,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do
7574
assert updated["education_backgrounds"] |> Enum.any?(&(&1["major"] == "bad ass"))
7675
end
7776

78-
@tag :wip
7977
test "user update education_backgrounds with invalid data fails", ~m(user)a do
8078
ownd_conn = simu_conn(:user, user)
8179

@@ -97,7 +95,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do
9795
assert ownd_conn |> mutation_get_error?(@update_query, variables)
9896
end
9997

100-
@tag :wip
10198
test "user can update it's own work backgrounds", ~m(user)a do
10299
ownd_conn = simu_conn(:user, user)
103100

@@ -128,7 +125,6 @@ defmodule MastaniServer.Test.Mutation.Account.Basic do
128125
assert updated["work_backgrounds"] |> Enum.any?(&(&1["title"] == "bad ass"))
129126
end
130127

131-
@tag :wip
132128
test "user update work backgrounds with invalid data fails", ~m(user)a do
133129
ownd_conn = simu_conn(:user, user)
134130

test/mastani_server_web/query/accounts/account_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ defmodule MastaniServer.Test.Query.Account.Basic do
6767
}
6868
}
6969
"""
70-
@tag :wip
7170
test "guest user can get specific user's info by user's id", ~m(guest_conn user)a do
7271
variables = %{id: user.id}
7372
results = guest_conn |> query_result(@query, variables, "user")

0 commit comments

Comments
 (0)