From dda62357f395542a3fa7468048524a34919eb2b1 Mon Sep 17 00:00:00 2001 From: Hunter Jarrell Date: Thu, 18 Jul 2019 20:39:20 -0400 Subject: [PATCH] Add join course mutation to backend Add a mutation which adds a course to the user by the join code. Closes #118 --- lib/ask_a_gator_web/resolvers/user_resolver.ex | 16 ++++++++++++++++ lib/ask_a_gator_web/schema.ex | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/lib/ask_a_gator_web/resolvers/user_resolver.ex b/lib/ask_a_gator_web/resolvers/user_resolver.ex index bf99bc2..38a527b 100644 --- a/lib/ask_a_gator_web/resolvers/user_resolver.ex +++ b/lib/ask_a_gator_web/resolvers/user_resolver.ex @@ -1,5 +1,6 @@ defmodule AskAGatorWeb.UserResolver do alias AskAGator.Accounts.User + alias AskAGator.Courses alias AskAGator.Repo alias AskAGator.Services.Authenticator @@ -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 diff --git a/lib/ask_a_gator_web/schema.ex b/lib/ask_a_gator_web/schema.ex index a14b8da..568c4be 100644 --- a/lib/ask_a_gator_web/schema.ex +++ b/lib/ask_a_gator_web/schema.ex @@ -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