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

      def index
        issues = ::Boards::Issues::ListService.new(project, current_user, filter_params).execute
        issues = issues.page(params[:page])

11 12 13 14 15 16 17 18 19
        render json: {
          issues: issues.as_json(
            only: [:iid, :title, :confidential],
            include: {
              assignee: { only: [:id, :name, :username], methods: [:avatar_url] },
              labels:   { only: [:id, :title, :description, :color, :priority], methods: [:text_color] }
            }),
          size: issues.total_count
        }
20 21
      end

P
Phil Hughes 已提交
22 23 24 25
      def create
        list = project.board.lists.find(params[:list_id])

        issue = Issues::CreateService.new(project, current_user, issue_params.merge(request: request)).execute
26
        issue.labels << list.label if list.label
P
Phil Hughes 已提交
27 28 29 30

        render json: issue.to_json
      end

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

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

      private

43
      def issue
44
        @issue ||=
45
          IssuesFinder.new(current_user, project_id: project.id)
46 47 48
                      .execute
                      .where(iid: params[:id])
                      .first!
49 50
      end

51 52 53 54 55
      def authorize_read_issue!
        return render_403 unless can?(current_user, :read_issue, project)
      end

      def authorize_update_issue!
56
        return render_403 unless can?(current_user, :update_issue, issue)
57 58 59 60 61 62 63 64 65
      end

      def filter_params
        params.merge(id: params[:list_id])
      end

      def move_params
        params.permit(:id, :from_list_id, :to_list_id)
      end
P
Phil Hughes 已提交
66 67 68 69

      def issue_params
        params.require(:issue).permit(:title)
      end
70 71 72
    end
  end
end