-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add is_permit_authorized plug * Add topics create endpoint * add enrollment check plug
- Loading branch information
Showing
12 changed files
with
135 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apps/core/priv/repo/migrations/20231214203342_create_course_index_topics.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters