Skip to content

Commit 1525c25

Browse files
committed
Some format change to adhere to backend style
1 parent 9e7f141 commit 1525c25

File tree

4 files changed

+127
-124
lines changed

4 files changed

+127
-124
lines changed

lib/cadet/accounts/game_states.ex

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Cadet.Accounts.GameStates do
2-
import Ecto.Repo
2+
use Cadet, :context
3+
alias Cadet.Accounts.User
34

5+
@update_gamestate_roles ~w(student)a
46
# currently in this module no error handling function
57
# has been implemented yet
68

@@ -16,24 +18,24 @@ defmodule Cadet.Accounts.GameStates do
1618
user.game_states["completed_quests"]
1719
end
1820

19-
def update(user, new_game_states) do
20-
if user.role == :student do
21-
changeset = Ecto.Changeset.cast(user, %{game_states: new_game_states}, [:game_states])
22-
Cadet.Repo.update!(changeset)
21+
def update(user = %User{role: role}, new_game_states) do
22+
if role in @update_gamestate_roles do
23+
changeset = cast(user, %{game_states: new_game_states}, [:game_states])
24+
Repo.update!(changeset)
2325
{:ok, nil}
2426
else
2527
{:error, {:forbidden, "Please try again later."}}
2628
end
2729
end
2830

29-
def clear(user) do
30-
if user.role == :student do
31+
def clear(user = %User{role: role}) do
32+
if role in @update_gamestate_roles do
3133
changeset =
32-
Ecto.Changeset.cast(user, %{game_states: %{collectibles: %{}, completed_quests: []}}, [
34+
cast(user, %{game_states: %{collectibles: %{}, completed_quests: []}}, [
3335
:game_states
3436
])
3537

36-
Cadet.Repo.update!(changeset)
38+
Repo.update!(changeset)
3739
{:ok, nil}
3840
else
3941
{:error, {:forbidden, "Please try again later."}}

lib/cadet_web/controllers/user_controller.ex

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ defmodule CadetWeb.UserController do
66
use CadetWeb, :controller
77
use PhoenixSwagger
88
import Cadet.Assessments
9-
import Cadet.GameStates
10-
import Ecto.Repo
9+
import Cadet.Accounts.GameStates
1110

1211
def index(conn, _) do
1312
user = conn.assigns.current_user
@@ -29,19 +28,24 @@ defmodule CadetWeb.UserController do
2928
)
3029
end
3130

32-
swagger_path :index do
33-
get("/user")
34-
summary("Get the name and role of a user")
35-
security([%{JWT: []}])
36-
produces("application/json")
37-
response(200, "OK", Schema.ref(:UserInfo))
38-
response(401, "Unauthorised")
31+
def update_game_states(conn, %{"gameStates" => new_game_states}) do
32+
user = conn.assigns[:current_user]
33+
34+
case update(user, new_game_states) do
35+
{:ok, nil} ->
36+
text(conn, "OK")
37+
38+
{:error, {status, message}} ->
39+
conn
40+
|> put_status(status)
41+
|> text(message)
42+
end
3943
end
4044

41-
def update_game_states(conn, %{"gameStates" => new_game_states}) do
45+
def clear_up_game_states(conn, _) do
4246
user = conn.assigns[:current_user]
4347

44-
case Cadet.GameStates.update(user, new_game_states) do
48+
case clear(user) do
4549
{:ok, nil} ->
4650
text(conn, "OK")
4751

@@ -52,6 +56,16 @@ defmodule CadetWeb.UserController do
5256
end
5357
end
5458

59+
swagger_path :index do
60+
get("/user")
61+
summary("Get the name and role of a user")
62+
security([%{JWT: []}])
63+
produces("application/json")
64+
response(200, "OK", Schema.ref(:UserInfo))
65+
response(401, "Unauthorised")
66+
end
67+
68+
5569
swagger_path :update_game_states do
5670
put("/user/game_states/save")
5771
summary("update user's game states")
@@ -70,20 +84,6 @@ defmodule CadetWeb.UserController do
7084
response(401, "Unauthorised")
7185
end
7286

73-
def clear_up_game_states(conn, _) do
74-
user = conn.assigns[:current_user]
75-
76-
case Cadet.GameStates.clear(user) do
77-
{:ok, nil} ->
78-
text(conn, "OK")
79-
80-
{:error, {status, message}} ->
81-
conn
82-
|> put_status(status)
83-
|> text(message)
84-
end
85-
end
86-
8787
swagger_path :clear_up_game_states do
8888
put("/user/game_states/clear")
8989
summary("clear up users' game data saved")

0 commit comments

Comments
 (0)