created base projects endpoint

This commit is contained in:
Zanz2 2018-09-10 16:20:02 +02:00
parent 765ee25853
commit dace81f724
3 changed files with 54 additions and 1 deletions

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
module Api
module V1
class ProjectsController < BaseController
before_action :load_team
before_action :load_project, only: :show
def index
projects = @team.projects
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: projects, each_serializer: ProjectSerializer
end
def show
render jsonapi: @project, serializer: ProjectSerializer
end
private
def load_team
@team = Team.find(params.require(:team_id))
render jsonapi: {}, status: :forbidden unless can_read_team?(@team)
end
def load_project
@project = @team.projects.find(params.require(:id))
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
end
end
end

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
module Api
module V1
class ProjectSerializer < ActiveModel::Serializer
type :projects
attributes :id, :name, :visibility, :due_date, :team_id, :created_at,
:updated_at, :archived, :archived_on, :created_by_id,
:last_modified_by_id, :archived_by_id, :restored_by_id,
:restored_on, :experiments_order
belongs_to :team, serializer: TeamSerializer
end
end
end

View file

@ -554,7 +554,8 @@ Rails.application.routes.draw do
path: 'items',
as: :items
end
resources :projects, only: %i(index show) do
resources :projects, only: %i(index show), path: 'projects',
as: :projects do
resources :experiments, only: %i(index show) do
resources :my_modules,
only: %i(index show),