Skip to content

Commit

Permalink
update_step_segment/3
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Ceolin committed Aug 4, 2024
1 parent 3d0abc2 commit 63d4e4c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/content/content_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,39 @@ defmodule Zoonk.Content do
lesson_step |> change_lesson_step(%{kind: kind}) |> Repo.update()
end

@doc """
Updates a lesson step segment.
Take an index and a segment, and updates the segment at the given index.
When making a segment empty, it also adds a draft option associated to it.
When the segment is not empty, it also deletes any options associated to it.
## Examples
iex> update_step_segment(%LessonStep{}, 1, "This is a")
{:ok, %{}}
"""
@spec update_step_segment(LessonStep.t(), non_neg_integer(), String.t()) :: {:ok, map()} | {:error, any()} | Ecto.Multi.failure()
def update_step_segment(%LessonStep{} = lesson_step, index, "") do
updated_step = change_lesson_step(lesson_step, %{kind: :fill, segments: List.replace_at(lesson_step.segments, index, nil)})
option_attrs = %{kind: :fill, lesson_step_id: lesson_step.id, segment: index, title: dgettext("orgs", "draft option")}

Ecto.Multi.new()
|> Ecto.Multi.update(:lesson_step, updated_step)
|> Ecto.Multi.insert(:option, change_step_option(%StepOption{}, option_attrs))
|> Repo.transaction()
end

def update_step_segment(%LessonStep{} = lesson_step, index, segment) do
updated_step = change_lesson_step(lesson_step, %{kind: :fill, segments: List.replace_at(lesson_step.segments, index, segment)})
query = StepOption |> where(lesson_step_id: ^lesson_step.id) |> where(segment: ^index)

Ecto.Multi.new()
|> Ecto.Multi.update(:lesson_step, updated_step)
|> Ecto.Multi.delete_all(:options, query)
|> Repo.transaction()
end

@doc """
Deletes a lesson step.
Expand Down
19 changes: 19 additions & 0 deletions test/content/content_context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,25 @@ defmodule Zoonk.ContentTest do
end
end

describe "update_step_segment/3" do
test "updates a step segment" do
lesson_step = lesson_step_fixture(%{kind: :fill, segments: ["This is a", nil, "step."]})
assert {:ok, _updated} = Content.update_step_segment(lesson_step, 2, "")
assert Repo.get(LessonStep, lesson_step.id).segments == ["This is a", nil, nil]

option = StepOption |> where(lesson_step_id: ^lesson_step.id) |> where(segment: 2) |> Repo.one()
assert option.title == "draft option"
end

test "when a text is added, deletes any options associated with this segment" do
lesson_step = lesson_step_fixture(%{kind: :fill, segments: ["This is a", nil, "step."]})
step_option = step_option_fixture(%{kind: :fill, lesson_step_id: lesson_step.id, segment: 1})
assert {:ok, _updated} = Content.update_step_segment(lesson_step, 1, "new")
assert Repo.get(LessonStep, lesson_step.id).segments == ["This is a", "new", "step."]
assert Repo.get(StepOption, step_option.id) == nil
end
end

describe "delete_lesson_step/1" do
test "deletes a lesson step" do
lesson = lesson_fixture()
Expand Down

0 comments on commit 63d4e4c

Please sign in to comment.