-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from thekeenant/explore
Adds course, instructor, and subject exploration api
- Loading branch information
Showing
20 changed files
with
1,683 additions
and
1,441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class V1::ExploreController < ApiController | ||
def courses | ||
@grades = CourseGrade | ||
@grades = apply_filters(@grades) | ||
@grades = apply_sort(@grades) | ||
@grades = apply_page(@grades) | ||
end | ||
|
||
def subjects | ||
@grades = SubjectGrade | ||
@grades = apply_filters(@grades) | ||
@grades = apply_sort(@grades) | ||
@grades = apply_page(@grades) | ||
end | ||
|
||
def instructors | ||
@grades = InstructorGrade | ||
@grades = apply_filters(@grades) | ||
@grades = apply_sort(@grades) | ||
@grades = apply_page(@grades) | ||
end | ||
|
||
private | ||
|
||
def apply_params(model) | ||
apply_page(apply_sort(apply_filters(model))) | ||
end | ||
|
||
def apply_page(model) | ||
model.page(params[:page]).per(params[:per_page]) | ||
end | ||
|
||
def apply_filters(model) | ||
min_gpa_total = (params[:min_gpa_total] || '0').to_i | ||
min_count_avg = (params[:min_count_avg] || '0').to_i | ||
|
||
model = model.where('gpa_total >= ?', min_gpa_total) | ||
model = model.where('count_avg >= ?', min_count_avg) | ||
|
||
model | ||
end | ||
|
||
def apply_sort(model) | ||
sort = (params[:sort] || '').downcase | ||
order = (params[:order] || '').downcase | ||
|
||
unless %w(gpa_total count_avg gpa).include?(sort) | ||
sort = 'gpa_total' | ||
end | ||
|
||
unless %w(asc desc).include?(order) | ||
order = 'desc' | ||
end | ||
|
||
model.order("#{sort} #{order}") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CourseGrade < ApplicationRecord | ||
def self.repopulate! | ||
CourseGrade.delete_all | ||
query = File.read("#{Rails.root}/lib/populate_course_grades.sql") | ||
ActiveRecord::Base.connection.execute("INSERT INTO course_grades #{query}") | ||
true | ||
end | ||
|
||
def course | ||
Course.find(course_uuid) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class InstructorGrade < ApplicationRecord | ||
def self.repopulate! | ||
InstructorGrade.delete_all | ||
query = File.read("#{Rails.root}/lib/populate_instructor_grades.sql") | ||
ActiveRecord::Base.connection.execute("INSERT INTO instructor_grades #{query}") | ||
true | ||
end | ||
|
||
def instructor | ||
Instructor.find(instructor_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class SubjectGrade < ApplicationRecord | ||
def self.repopulate! | ||
SubjectGrade.delete_all | ||
query = File.read("#{Rails.root}/lib/populate_subject_grades.sql") | ||
ActiveRecord::Base.connection.execute("INSERT INTO subject_grades #{query}") | ||
true | ||
end | ||
|
||
def subject | ||
Subject.find(subject_code) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
json.gpa_total model.gpa_total | ||
json.count_avg model.count_avg.to_f | ||
json.gpa model.gpa.to_f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
json.current_page @grades.current_page | ||
json.total_pages @grades.total_pages | ||
json.next_page_url url_to_next_page(@grades) | ||
json.results @grades.each do |grade| | ||
json.rank @grades.limit_value * (@grades.current_page - 1) + @grades.index(grade) + 1 | ||
json.course do | ||
json.partial! 'v1/courses/course', course: grade.course | ||
end | ||
json.partial! 'v1/explore/default', model: grade | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
json.current_page @grades.current_page | ||
json.total_pages @grades.total_pages | ||
json.next_page_url url_to_next_page(@grades) | ||
json.results @grades.each do |grade| | ||
json.rank @grades.limit_value * (@grades.current_page - 1) + @grades.index(grade) + 1 | ||
json.instructor do | ||
json.partial! 'v1/instructors/instructor', instructor: grade.instructor | ||
end | ||
json.partial! 'v1/explore/default', model: grade | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
json.current_page @grades.current_page | ||
json.total_pages @grades.total_pages | ||
json.next_page_url url_to_next_page(@grades) | ||
json.results @grades.each do |grade| | ||
json.rank @grades.limit_value * (@grades.current_page - 1) + @grades.index(grade) + 1 | ||
json.subject do | ||
json.partial! 'v1/subjects/subject', subject: grade.subject | ||
end | ||
json.partial! 'v1/explore/default', model: grade | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateSubjectGrades < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :subject_grades, id: false do |t| | ||
t.string :subject_code | ||
t.integer :gpa_total | ||
t.decimal :count_avg, precision: 6, scale: 3 | ||
t.decimal :gpa, precision: 10, scale: 6 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateInstructorGrades < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :instructor_grades, id: false do |t| | ||
t.integer :instructor_id | ||
t.integer :gpa_total | ||
t.decimal :count_avg, precision: 6, scale: 3 | ||
t.decimal :gpa, precision: 10, scale: 6 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateCourseGrades < ActiveRecord::Migration[5.1] | ||
def change | ||
create_table :course_grades, id: false do |t| | ||
t.string :course_uuid | ||
t.integer :gpa_total | ||
t.decimal :count_avg, precision: 6, scale: 3 | ||
t.decimal :gpa, precision: 6, scale: 3 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.