diff --git a/Gemfile b/Gemfile index ab7a41a16..34a1a7956 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ unless RUBY_PLATFORM =~ /w32/ end gem 'grape', '~> 0.9.0' +gem 'grape-entity' gem 'seems_rateable', path: 'lib/seems_rateable' gem "rails", "3.2.13" gem "jquery-rails", "~> 2.0.2" diff --git a/Gemfile.lock b/Gemfile.lock index d2ae03453..377ded53e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -107,6 +107,9 @@ GEM rack-accept rack-mount virtus (>= 1.0.0) + grape-entity (0.4.4) + activesupport + multi_json (>= 1.3.2) guard (2.6.1) formatador (>= 0.2.4) listen (~> 2.7) @@ -303,6 +306,7 @@ DEPENDENCIES factory_girl (~> 4.4.0) fastercsv (~> 1.5.0) grape (~> 0.9.0) + grape-entity guard-rails (~> 0.5.3) guard-spork (~> 1.5.1) guard-test (~> 1.0.0) diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index bd146e235..9409fddd7 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -1,27 +1,57 @@ module Mobile + + module Entities + + class Course < Grape::Entity + def self.course_expose(field) + expose field do |f,opt| + f[:course][field] || f[:course].__send__(field) + end + end + expose :img_url + course_expose :attachmenttype + course_expose :class_period + course_expose :code + course_expose :created_at + course_expose :description + course_expose :endup_time + course_expose :extra + course_expose :id + course_expose :inherit_members + course_expose :is_public + course_expose :lft + course_expose :location + course_expose :name + course_expose :open_student + # course_expose :password + course_expose :rgt + course_expose :school_id + course_expose :setup_time + course_expose :state + course_expose :status + course_expose :string + course_expose :tea_id + course_expose :term + course_expose :time + course_expose :updated_at + course_expose :teacher + end + + end + class Courses < Grape::API resource :courses do desc "get all courses" params do + optional :school_id, type: Integer, desc: 'school number' requires :per_page_count, type: Integer requires :page, type: Integer, desc: 'current page no' end get do - per_page_option = params[:per_page_count] || 10 - page_no = params[:page] || 1 - @courses_all = Course.active.visible. - joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id") - @course_count = @courses_all.count - @course_pages = Redmine::Pagination::Paginator.new @course_count, per_page_option,page_no - @course_activity_count=Hash.new - @courses_all.each do |course| - @course_activity_count[course.id]=0 - end - @courses = @courses_all.order("created_at desc") - @s_type = 0 - @courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page) - {status: 0, data: @courses} - # @course_activity_count=get_course_activity @courses, @course_activity_count + cs = CoursesService.new + courses = cs.course_list(params) + present :data, courses, with: Entities::Course + present :status, 0 end desc "Return a course"