mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-01-31 12:09:17 +08:00
created base projects endpoint
This commit is contained in:
parent
765ee25853
commit
dace81f724
3 changed files with 54 additions and 1 deletions
37
app/controllers/api/v1/projects_controller.rb
Normal file
37
app/controllers/api/v1/projects_controller.rb
Normal 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
|
15
app/serializers/api/v1/project_serializer.rb
Normal file
15
app/serializers/api/v1/project_serializer.rb
Normal 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
|
|
@ -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),
|
||||
|
|
Loading…
Reference in a new issue