mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-10 09:23:58 +08:00
Refactor tasks tree API endpoint [SCI-2609]
This commit is contained in:
parent
0e372af729
commit
26b0bd9e11
3 changed files with 0 additions and 132 deletions
|
@ -1,53 +0,0 @@
|
|||
module Api
|
||||
module V20170715
|
||||
class CoreApiController < ApiController
|
||||
def tasks_tree
|
||||
teams_json = []
|
||||
current_user.teams.find_each do |tm|
|
||||
team = tm.as_json(only: %i(name description))
|
||||
team['team_id'] = tm.id.to_s
|
||||
projects = []
|
||||
tm.projects.find_each do |pr|
|
||||
project = pr.as_json(only: %i(name visibility archived))
|
||||
project['project_id'] = pr.id.to_s
|
||||
experiments = []
|
||||
pr.experiments.find_each do |exp|
|
||||
experiment = exp.as_json(only: %i(name description archived))
|
||||
experiment['experiment_id'] = exp.id.to_s
|
||||
tasks = []
|
||||
exp.my_modules.find_each do |tk|
|
||||
task = tk.as_json(only: %i(name description archived))
|
||||
task['task_id'] = tk.id.to_s
|
||||
task['editable'] = can_manage_module?(tk)
|
||||
tasks << task
|
||||
end
|
||||
experiment['tasks'] = tasks
|
||||
experiments << experiment
|
||||
end
|
||||
project['experiments'] = experiments
|
||||
projects << project
|
||||
end
|
||||
team['projects'] = projects
|
||||
teams_json << team
|
||||
end
|
||||
render json: teams_json, status: :ok
|
||||
end
|
||||
|
||||
def task_samples
|
||||
task = MyModule.find_by_id(params[:task_id])
|
||||
return render json: {}, status: :not_found unless task
|
||||
return render json: {}, status: :forbidden unless
|
||||
can_read_experiment?(task.experiment)
|
||||
samples = task.samples
|
||||
samples_json = []
|
||||
samples.find_each do |s|
|
||||
sample = {}
|
||||
sample['sample_id'] = s.id.to_s
|
||||
sample['name'] = s.name
|
||||
samples_json << sample
|
||||
end
|
||||
render json: samples_json, status: :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -540,10 +540,6 @@ Rails.application.routes.draw do
|
|||
get 'health', to: 'api#health'
|
||||
get 'status', to: 'api#status'
|
||||
post 'auth/token', to: 'api#authenticate'
|
||||
scope '20170715', module: 'v20170715' do
|
||||
get 'tasks/tree', to: 'core_api#tasks_tree'
|
||||
# get 'tasks/:task_id/samples', to: 'core_api#task_samples'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe Api::V20170715::CoreApiController, type: :controller do
|
||||
let!(:user) { create :user }
|
||||
let!(:team) { create :team, created_by: user }
|
||||
let(:task) { create :my_module }
|
||||
let(:sample1) { create :sample }
|
||||
let(:sample2) { create :sample }
|
||||
let(:sample3) { create :sample }
|
||||
before do
|
||||
task.samples << [sample1, sample2, sample3]
|
||||
UserProject.create!(user: user, project: task.experiment.project, role: 0)
|
||||
end
|
||||
|
||||
describe 'GET #tasks_tree' do
|
||||
let!(:user_team) { create :user_team, team: team, user: user }
|
||||
context 'When valid request' do
|
||||
before do
|
||||
request.headers['HTTP_ACCEPT'] = 'application/json'
|
||||
request.headers['Authorization'] = 'Bearer ' + generate_token(user.id)
|
||||
get :tasks_tree
|
||||
end
|
||||
|
||||
it 'Returns HTTP success' do
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
|
||||
it 'Returns JSON body containing expected Task tree' do
|
||||
team = user.teams.first
|
||||
experiment = task.experiment
|
||||
project = experiment.project
|
||||
hash_body = nil
|
||||
expect { hash_body = json }.not_to raise_exception
|
||||
expect(hash_body).to match(
|
||||
['name' => team.name,
|
||||
'description' => team.description,
|
||||
'team_id' => team.id.to_s,
|
||||
'projects' => [{
|
||||
'name' => project.name,
|
||||
'visibility' => project.visibility,
|
||||
'archived' => project.archived,
|
||||
'project_id' => project.id.to_s,
|
||||
'experiments' => [{
|
||||
'name' => experiment.name,
|
||||
'description' => experiment.description,
|
||||
'archived' => experiment.archived,
|
||||
'experiment_id' => experiment.id.to_s,
|
||||
'tasks' => [{
|
||||
'name' => task.name,
|
||||
'description' => task.description,
|
||||
'archived' => task.archived,
|
||||
'task_id' => task.id.to_s,
|
||||
'editable' => true
|
||||
}]
|
||||
}]
|
||||
}]]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'When invalid request' do
|
||||
context 'When invalid token' do
|
||||
before do
|
||||
request.headers['HTTP_ACCEPT'] = 'application/json'
|
||||
request.headers['Authorization'] = 'Bearer WroNgToken'
|
||||
get :tasks_tree
|
||||
end
|
||||
|
||||
it 'Returns HTTP unauthorized' do
|
||||
expect(response).to have_http_status(401)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue