Skip to content

Commit

Permalink
Feat/topic process (#22)
Browse files Browse the repository at this point in the history
* add topic update endpoint

* add topic delete endpoint
  • Loading branch information
nmenag authored Dec 22, 2023
1 parent 1c566bc commit d11dd0a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
19 changes: 17 additions & 2 deletions apps/core/lib/core/schema/topic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ defmodule GoEscuelaLms.Core.Schema.Topic do
|> Repo.insert()
end

def changeset(course, attrs) do
course
def find(uuid) do
Repo.get(Topic, uuid)
|> Repo.preload(:activities)
end

def update(%Topic{} = topic, attrs) do
topic
|> changeset(attrs)
|> Repo.update()
end

def delete(%Topic{} = topic) do
topic |> Repo.delete()
end

def changeset(topic, attrs) do
topic
|> cast(attrs, [:name, :course_id])
|> validate_required([:name, :course_id])
end
Expand Down
26 changes: 26 additions & 0 deletions apps/web/lib/web/controllers/topics/topics_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule Web.Topics.TopicsController do
plug :is_permit_authorized when action in [:create]
plug :load_course when action in [:create]
plug :check_enrollment when action in [:create]
plug :load_topic when action in [:update, :delete]

@create_params %{
name: [type: :string, required: true]
Expand All @@ -25,10 +26,35 @@ defmodule Web.Topics.TopicsController do
end
end

def update(conn, params) do
topic = conn.assigns.topic

with {:ok, valid_params} <- Tarams.cast(params, @create_params),
{:ok, topic_updated} <- update_topic(topic, valid_params) do
render(conn, :update, %{topic: topic_updated})
end
end

def delete(conn, _params) do
topic = conn.assigns.topic

case topic |> Topic.delete() do
{:ok, topic} ->
render(conn, :delete, %{topic: topic})
end
end

defp create_topic(course, params) do
Topic.create(%{
name: params |> get_in([:name]),
course_id: course.uuid
})
end

defp update_topic(topic, params) do
topic
|> Topic.update(%{
name: params |> get_in([:name])
})
end
end
16 changes: 15 additions & 1 deletion apps/web/lib/web/controllers/topics/topics_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ defmodule Web.Topics.TopicsJSON do
Renders topic
"""

alias GoEscuelaLms.Core.Schema.Topic

def create(%{topic: topic}) do
%{data: %{name: topic.name}}
%{data: data(topic)}
end

def update(%{topic: topic}) do
%{data: data(topic)}
end

def delete(%{topic: topic}) do
%{message: "topic deleted", data: data(topic)}
end

defp data(%Topic{} = topic) do
%{name: topic.name}
end
end
15 changes: 14 additions & 1 deletion apps/web/lib/web/plug/check_request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Web.Plug.CheckRequest do
This module plug check request and load resource
"""
import Plug.Conn
alias GoEscuelaLms.Core.Schema.{Course, User}
alias GoEscuelaLms.Core.Schema.{Course, Topic, User}

def load_course(conn, _) do
course_id = conn.params["id"] || conn.params["courses_id"]
Expand All @@ -18,6 +18,19 @@ defmodule Web.Plug.CheckRequest do
end
end

def load_topic(conn, _) do
id = conn.params["id"]

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

def load_user(conn, _) do
id = conn.params["id"] || conn.params["users_id"]

Expand Down
2 changes: 1 addition & 1 deletion apps/web/lib/web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Web.Router do
end

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

get "/profile", Users.ProfileController, :show
Expand Down

0 comments on commit d11dd0a

Please sign in to comment.