Skip to content

Commit

Permalink
⚡ Remove some unnecessary database queries
Browse files Browse the repository at this point in the history
  • Loading branch information
RustySnek committed Oct 8, 2023
1 parent 309414c commit 965c27e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 44 deletions.
70 changes: 28 additions & 42 deletions lib/drag_board/board_tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ defmodule DragBoard.BoardTasks do
|> Repo.one()
end

defp move_tasks(tasks_to_move) do
fn repo, _ ->
try do
Enum.each(tasks_to_move, fn changeset ->
repo.update(changeset)
end)

{:ok, "Tasks moved successfully"}
catch
:error, reason ->
{:error, reason}
end
end
end

defp get_last_task_position(board) do
# Returns a -1 if the there are no other tasks, so when new one is created its position will be 0.
case length(board.board_tasks) do
Expand All @@ -37,14 +22,23 @@ defmodule DragBoard.BoardTasks do
end
end

def remove_task(task_id) do
task = get_task_by_id(task_id)

Ecto.Multi.new()
|> Ecto.Multi.delete(:remove_task, task)
|> Repo.transaction()
end

def add_task(name, board_id) do
board = Boards.get_board_with_desc_tasks_by_id(board_id)

last_position = get_last_task_position(board)
new_task = TaskQueries.new_board_task(board, name, last_position)

board
|> TaskQueries.new_board_task(name, last_position)
|> Repo.insert!()
Ecto.Multi.new()
|> Ecto.Multi.insert(:insert_new_task, new_task)
|> Repo.transaction()
end

def move_leftover_tasks_from_above_down(old_position, new_position, task_id, board_id) do
Expand All @@ -71,22 +65,10 @@ defmodule DragBoard.BoardTasks do
end
end

def move_task_board(previous_board_id, board_id, task_id, new_position, old_position) do
_move_task =
def move_task_board(board_id, task_id, new_position) do
move_task =
get_task_by_id(task_id)
|> BoardTask.changeset(%{"position" => new_position, "board_id" => board_id})
|> Repo.update!()

tasks_to_move =
TaskQueries.all_by_board_id(previous_board_id)
|> TaskQueries.get_leftover_tasks_from_below_without_new_position(old_position, task_id)
|> Repo.all()

moved_tasks_in_previous_board =
for task <- tasks_to_move do
new_position = task.position - 1
BoardTask.changeset(task, %{"position" => new_position})
end

tasks_to_move =
TaskQueries.all_by_board_id(board_id)
Expand All @@ -99,20 +81,20 @@ defmodule DragBoard.BoardTasks do
BoardTask.changeset(task, %{"position" => new_position})
end

move_tasks_in_current_board = move_tasks(moved_tasks_in_current_board)
move_tasks_in_previous_board = move_tasks(moved_tasks_in_previous_board)
multi = Ecto.Multi.new()

Ecto.Multi.new()
|> Ecto.Multi.run(:move_tasks_in_previous_board, move_tasks_in_previous_board)
|> Ecto.Multi.run(:move_tasks_in_current_board, move_tasks_in_current_board)
Enum.reduce(moved_tasks_in_current_board, multi, fn change, multi ->
id = Ecto.Changeset.get_field(change, :id)
Ecto.Multi.update(multi, {:move_task, id}, change)
end)
|> Ecto.Multi.update(:move_task, move_task)
|> Repo.transaction()
end

def move_task_position(old_position, new_position, task_id, board_id) do
_move_task =
move_task =
get_task_by_id(task_id)
|> BoardTask.changeset(%{"position" => new_position})
|> Repo.update!()

moved_tasks =
cond do
Expand All @@ -126,10 +108,14 @@ defmodule DragBoard.BoardTasks do
[]
end

move_tasks = move_tasks(moved_tasks)
multi = Ecto.Multi.new()

Ecto.Multi.new()
|> Ecto.Multi.run(:move_tasks, move_tasks)
moved_tasks
|> Enum.reduce(multi, fn change, multi ->
id = Ecto.Changeset.get_field(change, :id)
Ecto.Multi.update(multi, {:move_task, id}, change)
end)
|> Ecto.Multi.update(:move_task, move_task)
|> Repo.transaction()
end
end
8 changes: 6 additions & 2 deletions lib/drag_board/boards.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule DragBoard.Boards do

def get_board_by_id(board_id) do
BoardQueries.with_id(board_id)
|> Repo.all()
|> Repo.one()
end

def get_board_with_desc_tasks_by_id(board_id) do
Expand All @@ -20,14 +20,18 @@ defmodule DragBoard.Boards do
|> Repo.all()
end

def remove_board(board_id) do
BoardQueries.with_id(board_id)
|> Repo.delete_all()
end

def add_board(name, group) do
changeset =
%Board{}
|> Board.changeset(%{name: name, group: group})

Ecto.Multi.new()
|> Ecto.Multi.insert(:insert_board, changeset)
|> Ecto.Multi.all(:list_boards, BoardQueries.list_boards_with_tasks())
|> Repo.transaction()
end
end

0 comments on commit 965c27e

Please sign in to comment.