Skip to content

Commit

Permalink
Add join course mutation to backend
Browse files Browse the repository at this point in the history
Add a mutation which adds a course to the user by the join code.

Closes  ufosc#118
  • Loading branch information
hjarrell committed Jul 19, 2019
1 parent 763785b commit dda6235
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/ask_a_gator_web/resolvers/user_resolver.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule AskAGatorWeb.UserResolver do
alias AskAGator.Accounts.User
alias AskAGator.Courses
alias AskAGator.Repo
alias AskAGator.Services.Authenticator

Expand Down Expand Up @@ -52,4 +53,19 @@ defmodule AskAGatorWeb.UserResolver do
def signed_in?(_root, _info) do
{:ok, false}
end

def join_course(%{join_code: join_code}, %{context: %{current_user: c}}) do
case Courses.get_course_by_join(join_code) do
nil ->
{:error, "Invalid join code"}
course ->
User.add_course(c, course)
{:ok, course.code}
end
end

def join_course(%{join_code: join_code}, _info) do
{:error, "Not Signed In"}
end

end
5 changes: 5 additions & 0 deletions lib/ask_a_gator_web/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ defmodule AskAGatorWeb.Schema do
end
end
end

field :join_course, type: :string do
arg(:join_code, non_null(:string))
resolve(&UserResolver.join_course/2)
end
end
end

0 comments on commit dda6235

Please sign in to comment.