Skip to content

Commit

Permalink
Merge pull request #2 from thekeenant/explore
Browse files Browse the repository at this point in the history
Adds course, instructor, and subject exploration api
  • Loading branch information
thekeenant authored Apr 4, 2018
2 parents 809f062 + 847dcfc commit 75f4e44
Show file tree
Hide file tree
Showing 20 changed files with 1,683 additions and 1,441 deletions.
4 changes: 1 addition & 3 deletions app/controllers/v1/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def index
sorted = true

# if the user uses these sorting methods, we have a special query
if %w(trending_recent trending_all trending_gpa_recent trending_gpa_all).include?(sort)
elsif sort == 'name'
if sort == 'name'
@courses = Course.order("name #{order}")
elsif sort == 'number'
@courses = Course.order("number #{order}")
Expand Down Expand Up @@ -55,7 +54,6 @@ def index

searchkick = false

# TODO: Untested, needed searchkick backend to test
if query.present?
if sorted
search_results = Course.search_without_page(query).map(&:uuid)
Expand Down
57 changes: 57 additions & 0 deletions app/controllers/v1/explore_controller.rb
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
12 changes: 12 additions & 0 deletions app/models/course_grade.rb
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
12 changes: 12 additions & 0 deletions app/models/instructor_grade.rb
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
12 changes: 12 additions & 0 deletions app/models/subject_grade.rb
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
3 changes: 3 additions & 0 deletions app/views/v1/explore/_default.json.jbuilder
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
10 changes: 10 additions & 0 deletions app/views/v1/explore/courses.json.jbuilder
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
11 changes: 11 additions & 0 deletions app/views/v1/explore/instructors.json.jbuilder
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
10 changes: 10 additions & 0 deletions app/views/v1/explore/subjects.json.jbuilder
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
12 changes: 6 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ default: &default
adapter: mysql2
encoding: utf8
pool: 10
username: remote
password: 067dcea2cf57
host: server.madgrades.com
port: 3306
# socket: /var/run/mysqld/mysqld.sock
username: root
password: password
# host: server.madgrades.com
# port: 3306
socket: /var/run/mysqld/mysqld.sock

development:
<<: *default
database: madgrades
database: madgrades_dev

test:
<<: *default
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@
resources :sections, only: [:show]

resources :subjects, only: [:index, :show]

get 'explore/courses', to: 'explore#courses', as: :explore_courses
get 'explore/subjects', to: 'explore#subjects', as: :explore_subjects
get 'explore/instructors', to: 'explore#instructors', as: :explore_instructors
end
end
10 changes: 10 additions & 0 deletions db/migrate/20180404213526_create_subject_grades.rb
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
10 changes: 10 additions & 0 deletions db/migrate/20180404213543_create_instructor_grades.rb
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
10 changes: 10 additions & 0 deletions db/migrate/20180404213631_create_course_grades.rb
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
23 changes: 22 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180404203823) do
ActiveRecord::Schema.define(version: 20180404213631) do

create_table "course_grades", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" 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

create_table "course_offering_grade_dists", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "course_uuid"
Expand Down Expand Up @@ -112,6 +119,13 @@
t.index ["term_code"], name: "index_instructor_grade_dists_on_term_code"
end

create_table "instructor_grades", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" 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

create_table "instructors", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.integer "id"
t.string "name"
Expand Down Expand Up @@ -151,6 +165,13 @@
t.index ["uuid"], name: "index_sections_on_uuid", unique: true
end

create_table "subject_grades", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" 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

create_table "subject_memberships", id: false, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "subject_code"
t.string "course_offering_uuid"
Expand Down
Loading

0 comments on commit 75f4e44

Please sign in to comment.