Skip to content

Commit

Permalink
Feat/topics controller (#18)
Browse files Browse the repository at this point in the history
* Add is_permit_authorized plug

* Add topics create endpoint

* add enrollment check plug
  • Loading branch information
nmenag authored Dec 14, 2023
1 parent 2be1451 commit c5f714c
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 20 deletions.
2 changes: 2 additions & 0 deletions apps/core/lib/core/schema/course.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ defmodule GoEscuelaLms.Core.Schema.Course do

def find(uuid) do
Repo.get(Course, uuid)
|> Repo.preload(:topics)
|> Repo.preload(:enrollments)
end

def create(attrs \\ %{}) do
Expand Down
8 changes: 8 additions & 0 deletions apps/core/lib/core/schema/topic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule GoEscuelaLms.Core.Schema.Topic do
use Ecto.Schema
import Ecto.Changeset

alias __MODULE__
alias GoEscuelaLms.Core.Repo, as: Repo
alias GoEscuelaLms.Core.Schema.{Course, Activity}

@primary_key {:uuid, Ecto.UUID, autogenerate: true}
Expand All @@ -19,6 +21,12 @@ defmodule GoEscuelaLms.Core.Schema.Topic do
timestamps()
end

def create(attrs \\ %{}) do
%Topic{}
|> Topic.changeset(attrs)
|> Repo.insert()
end

def changeset(course, attrs) do
course
|> cast(attrs, [:name, :course_id])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule GoEscuelaLms.Core.Repo.Migrations.CreateCourseIndexTopics do
use Ecto.Migration

def change do
drop index(:topics, [:course_id])
create index(:topics, [:course_id])
end
end
20 changes: 11 additions & 9 deletions apps/web/lib/web/auth/authorized_plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ defmodule Web.Auth.AuthorizedPlug do
import Plug.Conn
alias GoEscuelaLms.Core.Schema.User

# def is_authorized(%{params: %{"id" => id}} = conn, _opts) do
# if conn.assigns.account.uuid == id do
# conn
# else
# Web.FallbackController.call(conn, {:error, :forbidden}) |> halt()
# end
# end

def is_admin_authorized(conn, _) do
def is_organizer_authorized(conn, _) do
case conn.assigns.account |> User.organizer?() do
true ->
conn
Expand All @@ -32,4 +24,14 @@ defmodule Web.Auth.AuthorizedPlug do
Web.FallbackController.call(conn, {:error, :forbidden}) |> halt()
end
end

def is_permit_authorized(conn, _) do
case conn.assigns.account |> User.instructor?() || conn.assigns.account |> User.organizer?() do
true ->
conn

_ ->
Web.FallbackController.call(conn, {:error, :forbidden}) |> halt()
end
end
end
2 changes: 1 addition & 1 deletion apps/web/lib/web/controllers/courses/courses_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Web.Courses.CoursesController do

alias GoEscuelaLms.Core.Schema.Course

plug :is_admin_authorized when action in [:create]
plug :is_organizer_authorized when action in [:create]

@create_params %{
name: [type: :string, required: true],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ defmodule Web.Enrollments.EnrollmentsController do

alias GoEscuelaLms.Core.Schema.{Course, Enrollment, User}

plug :is_admin_authorized when action in [:create]
plug :is_organizer_authorized when action in [:create]

def create(conn, params) do
user_id = params["users_id"]
course_id = params["courses_id"]

with :ok <- valid_uuids(user_id, course_id),
with :ok <- valid_uuids(user_id),
:ok <- valid_uuids(course_id),
:ok <- valid_resources(user_id, course_id),
{:ok, enrollment} <- create_enrollment(user_id, course_id) do
render(conn, :create, %{enrollment: enrollment})
Expand All @@ -27,11 +28,11 @@ defmodule Web.Enrollments.EnrollmentsController do
})
end

defp valid_uuids(user_id, course_id) do
with {:ok, _} <- Ecto.UUID.dump(user_id),
{:ok, _} <- Ecto.UUID.dump(course_id) do
:ok
else
defp valid_uuids(id) do
case Ecto.UUID.dump(id) do
{:ok, _} ->
:ok

_ ->
{:error, "invalid params"}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Web.Onboarding.InstitutionInfoController do

alias GoEscuelaLms.Core.Schema.InstitutionInfo

plug :is_admin_authorized when action in [:show]
plug :is_organizer_authorized when action in [:show]

def show(conn, _params) do
institution_info = InstitutionInfo.get!()
Expand Down
35 changes: 35 additions & 0 deletions apps/web/lib/web/controllers/topics/topics_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule Web.Topics.TopicsController do
use Web, :controller

action_fallback Web.FallbackController

import Web.Auth.AuthorizedPlug
import Web.Plug.CheckRequest

alias GoEscuelaLms.Core.Schema.Topic

plug :is_permit_authorized when action in [:create]
plug :load_course when action in [:create]
plug :load_course when action in [:create]
plug :check_enrollment when action in [:create]

@create_params %{
name: [type: :string, required: true]
}

def create(conn, params) do
course = conn.assigns.course

with {:ok, valid_params} <- Tarams.cast(params, @create_params),
{:ok, topic} <- create_topic(course, valid_params) do
render(conn, :create, %{topic: topic})
end
end

defp create_topic(course, params) do
Topic.create(%{
name: params |> get_in([:name]),
course_id: course.uuid
})
end
end
9 changes: 9 additions & 0 deletions apps/web/lib/web/controllers/topics/topics_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Web.Topics.TopicsJSON do
@doc """
Renders topic
"""

def create(%{topic: topic}) do
%{data: %{name: topic.name}}
end
end
2 changes: 1 addition & 1 deletion apps/web/lib/web/controllers/users/users_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Web.Users.UsersController do

alias GoEscuelaLms.Core.Schema.User

plug :is_admin_authorized when action in [:create]
plug :is_organizer_authorized when action in [:create]

@create_params %{
full_name: [type: :string, required: true],
Expand Down
48 changes: 48 additions & 0 deletions apps/web/lib/web/plug/check_request.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule Web.Plug.CheckRequest do
@moduledoc """
This module plug check request and load resource
"""
import Plug.Conn
alias GoEscuelaLms.Core.Schema.{Course}

def load_course(conn, _) do
course_id = conn.params["courses_id"]

with :ok <- valid_uuids(course_id),
course <- Course.find(course_id),
false <- is_nil(course) do
assign(conn, :course, course)
else
_ ->
Web.FallbackController.call(conn, {:error, "invalid params"}) |> halt()
end
end

defp valid_uuids(id) do
case Ecto.UUID.dump(id) do
{:ok, _} ->
:ok

_ ->
{:error, "invalid params"}
end
end

def check_enrollment(%{assigns: %{account: %{role: :organizer}}} = conn, _), do: conn

def check_enrollment(conn, _) do
user_id = conn.assigns.account.uuid
course = conn.assigns.course

case is_nil(
course.enrollments
|> Enum.find(fn enrollment -> enrollment.user_id == user_id end)
) do
false ->
conn

_ ->
Web.FallbackController.call(conn, {:error, :forbidden}) |> halt()
end
end
end
4 changes: 3 additions & 1 deletion apps/web/lib/web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ defmodule Web.Router do
end
end

resources "/courses", Courses.CoursesController, only: [:create]
resources "/courses", Courses.CoursesController, only: [:create] do
resources "/topics", Topics.TopicsController, only: [:create]
end

get "/profile", Users.ProfileController, :show
get "/auth/sessions", Auth.SessionController, :refresh_session
Expand Down

0 comments on commit c5f714c

Please sign in to comment.