issues_controller.rb 2.71 KB
Newer Older
1 2
module Projects
  module Boards
3
    class IssuesController < Boards::ApplicationController
4
      before_action :authorize_read_issue!, only: [:index]
5
      before_action :authorize_create_issue!, only: [:create]
6 7 8 9
      before_action :authorize_update_issue!, only: [:update]

      def index
        issues = ::Boards::Issues::ListService.new(project, current_user, filter_params).execute
10
        issues = issues.page(params[:page]).per(params[:per] || 20)
11
        make_sure_position_is_set(issues)
12

13
        render json: {
14
          issues: serialize_as_json(issues),
15 16
          size: issues.total_count
        }
17 18
      end

19
      def create
20
        service = ::Boards::Issues::CreateService.new(project, current_user, issue_params)
21
        issue = service.execute
22

23 24 25 26 27
        if issue.valid?
          render json: serialize_as_json(issue)
        else
          render json: issue.errors, status: :unprocessable_entity
        end
28 29
      end

30 31 32
      def update
        service = ::Boards::Issues::MoveService.new(project, current_user, move_params)

33
        if service.execute(issue)
34 35 36 37 38 39 40 41
          head :ok
        else
          head :unprocessable_entity
        end
      end

      private

42 43
      def make_sure_position_is_set(issues)
        issues.each do |issue|
44
          issue.move_to_end && issue.save unless issue.relative_position
45 46 47
        end
      end

48
      def issue
49
        @issue ||=
50
          IssuesFinder.new(current_user, project_id: project.id)
51 52 53
                      .execute
                      .where(iid: params[:id])
                      .first!
54 55
      end

56 57 58 59
      def authorize_read_issue!
        return render_403 unless can?(current_user, :read_issue, project)
      end

60 61 62 63
      def authorize_create_issue!
        return render_403 unless can?(current_user, :admin_issue, project)
      end

64
      def authorize_update_issue!
65
        return render_403 unless can?(current_user, :update_issue, issue)
66 67 68
      end

      def filter_params
69 70
        params.merge(board_id: params[:board_id], id: params[:list_id])
          .reject { |_, value| value.nil? }
71 72 73
      end

      def move_params
74
        params.permit(:board_id, :id, :from_list_id, :to_list_id, :move_before_iid, :move_after_iid)
75
      end
76 77

      def issue_params
78
        params.require(:issue).permit(:title).merge(board_id: params[:board_id], list_id: params[:list_id], request: request)
79 80 81 82
      end

      def serialize_as_json(resource)
        resource.as_json(
83
          labels: true,
84
          only: [:id, :iid, :title, :confidential, :due_date, :relative_position],
85
          include: {
86
            assignees: { only: [:id, :name, :username], methods: [:avatar_url] },
87 88 89 90
            milestone: { only: [:id, :title] }
          },
          user: current_user
        )
91
      end
92 93 94
    end
  end
end