BigW Consortium Gitlab

todos_controller.rb 1.1 KB
Newer Older
1
class Dashboard::TodosController < Dashboard::ApplicationController
2
  before_action :find_todos, only: [:index, :destroy_all]
3

4
  def index
5
    @todos = @todos.page(params[:page])
6 7 8
  end

  def destroy
9
    TodoService.new.mark_todos_as_done([todo], current_user)
10 11

    respond_to do |format|
12
      format.html { redirect_to dashboard_todos_path, notice: 'Todo was successfully marked as done.' }
13
      format.js { head :ok }
14
      format.json { render json: todos_counts }
15 16 17
    end
  end

18
  def destroy_all
19
    TodoService.new.mark_todos_as_done(@todos, current_user)
20 21 22

    respond_to do |format|
      format.html { redirect_to dashboard_todos_path, notice: 'All todos were marked as done.' }
23
      format.js { head :ok }
24
      format.json { render json: todos_counts }
25 26 27
    end
  end

28 29 30
  private

  def todo
31
    @todo ||= find_todos.find(params[:id])
32
  end
33 34

  def find_todos
35
    @todos ||= TodosFinder.new(current_user, params).execute
36
  end
37 38 39 40 41 42 43

  def todos_counts
    {
      count: TodosFinder.new(current_user, state: :pending).execute.count,
      done_count: TodosFinder.new(current_user, state: :done).execute.count
    }
  end
44
end