Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #263 from YACS-RCOS/staging
Browse files Browse the repository at this point in the history
Admin API, Use Redis for Caching
  • Loading branch information
Bad-Science authored Oct 18, 2017
2 parents 28605ef + 07d4165 commit bec5b5e
Show file tree
Hide file tree
Showing 17 changed files with 382 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gem 'sprockets-rails', :require => 'sprockets/railtie'
gem 'actionpack-page_caching'
gem 'actionpack-action_caching'
gem 'rails-observers'
gem 'dalli'
gem 'redis-rails'

# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby
Expand Down
21 changes: 18 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ GEM
nokogiri (~> 1.5)
railties (>= 3, < 5.1)
cucumber-wire (0.0.1)
dalli (2.7.6)
database_cleaner (1.5.3)
debug_inspector (0.0.2)
debugger-linecache (1.2.0)
Expand Down Expand Up @@ -185,7 +184,23 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.3.0)
redis (3.3.1)
redis (3.3.3)
redis-actionpack (5.0.1)
actionpack (>= 4.0, < 6)
redis-rack (>= 1, < 3)
redis-store (>= 1.1.0, < 1.4.0)
redis-activesupport (5.0.3)
activesupport (>= 3, < 6)
redis-store (~> 1.3.0)
redis-rack (2.0.2)
rack (>= 1.5, < 3)
redis-store (>= 1.2, < 1.4)
redis-rails (5.0.2)
redis-actionpack (>= 5.0, < 6)
redis-activesupport (>= 5.0, < 6)
redis-store (>= 1.2, < 2)
redis-store (1.3.0)
redis (>= 2.2)
ref (2.0.0)
responders (2.3.0)
railties (>= 4.2.0, < 5.1)
Expand Down Expand Up @@ -268,7 +283,6 @@ DEPENDENCIES
coveralls
crono
cucumber-rails
dalli
database_cleaner
factory_girl_rails
handlebars_assets
Expand All @@ -286,6 +300,7 @@ DEPENDENCIES
rails (= 4.2.5.1)
rails-observers
redis
redis-rails
responders (~> 2.0)
rspec-rails
sass-rails (~> 5.0)
Expand Down
23 changes: 19 additions & 4 deletions app/controllers/api/v5/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ class Api::V5::ApiController < ActionController::Metal
include ActionController::Caching
include ActionController::Instrumentation
include ActionView::Layouts

include ActionController::StrongParameters
include ActionController::Head
include ActionController::Rescue
include ActionController::Redirecting

append_view_path "#{Rails.root}/app/views"

self.page_cache_directory = Rails.public_path
self.perform_caching = true
self.cache_store = :dalli_store
self.cache_store = :redis_store, 'redis://redis:6379/0/cache'

before_filter :nested_queries, only: [:index]
before_action :nested_queries, only: [:show, :index]
before_action :authenticate, except: [:show, :index]

rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

private
def nested_queries
Expand All @@ -27,6 +34,10 @@ def nested_queries
def query
@query
end

def record_not_found
head :not_found
end

def any param
params[param].split(',')
Expand All @@ -47,4 +58,8 @@ def filter_any *param
end
end
end
end

def authenticate
Rails.env.test? || Rails.env.development?
end
end
26 changes: 26 additions & 0 deletions app/controllers/api/v5/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,30 @@ def index
end
end

def show
@query = Course.where(id: params[:id])
end

def create
@query = [Course.create!(course_params)]
render action: :show, status: :created
end

def update
@query = [Course.find(params[:id])]
@query.first.update!(course_params)
render action: :show, status: :success
end

def destroy
Course.find(params[:id]).destroy
head :no_content
end

private

def course_params
params.require(:course).permit(:name, :number, :min_credits,
:max_credits, :description, :department_id)
end
end
26 changes: 26 additions & 0 deletions app/controllers/api/v5/departments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,30 @@ def index
query.includes! courses: [:sections] if @show_sections
end
end

def show
@query = Department.where(id: params[:id])
end

def create
@query = [Department.create!(department_params)]
render action: :show, status: :created
end

def update
@query = [Department.find(params[:id])]
@query.first.update!(department_params)
render action: :show, status: :success
end

def destroy
Department.find(params[:id]).destroy!
head :no_content
end

private

def department_params
params.require(:department).permit(:code, :name, :school_id)
end
end
26 changes: 26 additions & 0 deletions app/controllers/api/v5/schools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,30 @@ def index
end
query.order! :id
end

def show
@query = School.where(id: params[:id])
end

def create
@query = [School.create!(school_params)]
render action: :show, status: :created
end

def update
@query = [School.find(params[:id])]
@query.first.update!(school_params)
render action: :show, status: :success
end

def destroy
School.find(params[:id]).destroy!
head :no_content
end

private

def school_params
params.require(:school).permit(:name)
end
end
27 changes: 27 additions & 0 deletions app/controllers/api/v5/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,31 @@ def index
filter_model Section
filter_any :id, :course_id, :name, :crn
end

def show
@query = Section.where(id: params[:id])
end

def create
@query = [Section.create!(section_params)]
render action: :show, status: :created
end

def update
@query = [Section.find(params[:id])]
@query.first.update!(section_params)
render action: :show, status: :success
end

def destroy
Section.find(params[:id]).destroy!
head :no_content
end

private

def section_params
params.require(:section).permit(:name, :crn, :seats, :seats_taken,
:instructors, :num_periods, :course_id)
end
end
1 change: 1 addition & 0 deletions app/views/api/v5/courses/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! '/api/v5/courses/courses', courses: @query
1 change: 1 addition & 0 deletions app/views/api/v5/departments/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! '/api/v5/departments/departments', departments: @query
6 changes: 6 additions & 0 deletions app/views/api/v5/schools/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
json.schools @query do |school|
json.(school, :id, :name)
if @show_departments
json.partial! '/api/v5/departments/departments', departments: school.departments
end
end
1 change: 1 addition & 0 deletions app/views/api/v5/sections/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! '/api/v5/sections/sections', sections: @query
10 changes: 5 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace :api, defaults: { format: :json } do
namespace :v5 do
resources :schools, only: [:index]
resources :departments, only: [:index]
resources :courses, only: [:index]
resources :sections, only: [:index]
resources :schools, only: [:index, :update, :destroy, :create, :show]
resources :departments, only: [:index, :update, :destroy, :create, :show]
resources :courses, only: [:index, :update, :destroy, :create, :show]
resources :sections, only: [:index, :update, :destroy, :create, :show]
resources :schedules, only: [:index]
end
end
end

get '/' => 'static#index'
get '/about' => 'static#about'
Expand Down
1 change: 1 addition & 0 deletions spec/models/department_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
expect(build(:department, code: '')).to_not be_valid
expect(build(:department, name: '')).to_not be_valid
end

end
61 changes: 61 additions & 0 deletions spec/requests/api/v5/courses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,65 @@ def json_validate_courses(courses=@courses, sections=false, periods=false)
json_validate_courses(Course.all, true, true)
end
end

context 'There is a course to be created' do
it 'creates a course' do
department = FactoryGirl.create(:department)
course_params={
course: {
name: 'Principles of Software',
number: 2600,
min_credits: 1,
max_credits: 4,
description: 'XXXXXX',
department_id: department.id
}
}
post "/api/v5/courses/", course_params
expect(response).to be_success
created_course=Course.find_by(name: 'Principles of Software', number: 2600)
expect(created_course).to be_present
json_validate_courses([created_course])
end
end

context 'There is a course to be updated' do
it 'updates the maximum number of credits for the course'do
course = FactoryGirl.create(:course, max_credits: 4)
course_params={
course: {
max_credits: 7
}
}
put "/api/v5/courses/#{course.id}", course_params
course.reload
expect(course.max_credits).to eq 7
json_validate_courses([course])
end
it 'updates the minimum number of credits for the course' do
course_params={
course: {
min_credits:2
}
}

course = FactoryGirl.create(:course, min_credits: 2)
put "/api/v5/courses/#{course.id}", course_params
course.reload
expect(course.min_credits).to eq 2
json_validate_courses([course])
end
it 'deletes a course' do
course = FactoryGirl.create(:course)
delete "/api/v5/courses/#{course.id}"
expect(response.status).to eq 204
end

it 'course id is not found for deletion' do
course = FactoryGirl.create(:course)
delete "/api/v5/courses/#{5000000}"
expect(response.status).to eq 404
end

end
end
56 changes: 56 additions & 0 deletions spec/requests/api/v5/departments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,60 @@ def json_validate_departments(departments=@departments, courses=false)
json_validate_departments(Department.all, true)
end
end

context 'There is a department to be created' do
it 'creates a department' do
department_params={
department: {
name: 'Information Technology and Web Science',
code: 'ITWS'
}
}
post "/api/v5/departments/", department_params
expect(response).to be_success
created_department=Department.find_by(code: 'ITWS')
expect(created_department).to be_present
json_validate_departments([created_department])
end

end

context 'There is a department to be updated' do
it 'updates the code for department' do
department = FactoryGirl.create(:department, code: 'XYZ')
department_params = {
department: {
code: 'other'
}
}
put "/api/v5/departments/#{department.id}", department_params
department.reload
expect(department.code).to eq 'other'
json_validate_departments([department])
end
it 'updates the name for department' do
department = FactoryGirl.create(:department, name: 'ITWS')
department_params = {
department: {
name: 'other'
}
}
put "/api/v5/departments/#{department.id}", department_params
department.reload
expect(department.name).to eq 'other'
json_validate_departments([department])
end

it 'deletes a department' do
department = FactoryGirl.create(:department)
delete "/api/v5/departments/#{department.id}"
expect(response.status).to eq 204
end

it 'department id is not found for deletion' do
department = FactoryGirl.create(:department)
delete "/api/v5/departments/#{500000}"
expect(response.status).to eq 404
end
end
end
Loading

0 comments on commit bec5b5e

Please sign in to comment.