From dace81f72426ab43bbbc61d6b167ec1c30b3f74b Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Mon, 10 Sep 2018 16:20:02 +0200 Subject: [PATCH] created base projects endpoint --- app/controllers/api/v1/projects_controller.rb | 37 +++++++++++++++++++ app/serializers/api/v1/project_serializer.rb | 15 ++++++++ config/routes.rb | 3 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/projects_controller.rb create mode 100644 app/serializers/api/v1/project_serializer.rb diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb new file mode 100644 index 000000000..ce0e73068 --- /dev/null +++ b/app/controllers/api/v1/projects_controller.rb @@ -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 diff --git a/app/serializers/api/v1/project_serializer.rb b/app/serializers/api/v1/project_serializer.rb new file mode 100644 index 000000000..b1d9dede8 --- /dev/null +++ b/app/serializers/api/v1/project_serializer.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 5712f8a9a..21567b83b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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),