Merge branch 'api' of http://repository.trustie.net/xianbo/trustie2 into api
Conflicts: Gemfile.lock
This commit is contained in:
commit
dbb46b07a6
7
Gemfile
7
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"
|
||||
|
@ -49,9 +50,9 @@ group :development, :test do
|
|||
gem 'guard-spork', '~> 1.5.1'
|
||||
gem 'guard-test', '~> 1.0.0'
|
||||
gem 'ruby-prof', '~> 0.15.1' unless RUBY_PLATFORM =~ /w32/
|
||||
gem 'pry'
|
||||
gem 'pry-nav'
|
||||
|
||||
gem 'pry-rails'
|
||||
gem 'pry-byebug'
|
||||
gem 'pry-remote'
|
||||
end
|
||||
|
||||
|
||||
|
|
24
Gemfile.lock
24
Gemfile.lock
|
@ -57,6 +57,9 @@ GEM
|
|||
ice_nine (~> 0.11.0)
|
||||
thread_safe (~> 0.3, >= 0.3.1)
|
||||
builder (3.0.0)
|
||||
byebug (2.7.0)
|
||||
columnize (~> 0.3)
|
||||
debugger-linecache (~> 1.2)
|
||||
capybara (2.4.1)
|
||||
mime-types (>= 1.16)
|
||||
nokogiri (>= 1.3.3)
|
||||
|
@ -81,6 +84,8 @@ GEM
|
|||
coffee-script-source
|
||||
execjs
|
||||
coffee-script-source (1.7.1)
|
||||
columnize (0.9.0)
|
||||
debugger-linecache (1.2.0)
|
||||
descendants_tracker (0.0.4)
|
||||
thread_safe (~> 0.3, >= 0.3.1)
|
||||
equalizer (0.0.9)
|
||||
|
@ -102,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)
|
||||
|
@ -169,8 +177,14 @@ GEM
|
|||
method_source (~> 0.8)
|
||||
slop (~> 3.4)
|
||||
win32console (~> 1.3)
|
||||
pry-nav (0.2.3)
|
||||
pry (~> 0.9.10)
|
||||
pry-byebug (1.3.2)
|
||||
byebug (~> 2.7)
|
||||
pry (~> 0.9.12)
|
||||
pry-rails (0.3.2)
|
||||
pry (>= 0.9.10)
|
||||
pry-remote (0.1.8)
|
||||
pry (~> 0.9)
|
||||
slop (~> 3.0)
|
||||
rack (1.4.5)
|
||||
rack-accept (0.4.5)
|
||||
rack (>= 0.4)
|
||||
|
@ -286,6 +300,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)
|
||||
|
@ -298,8 +313,9 @@ DEPENDENCIES
|
|||
net-ldap (~> 0.3.1)
|
||||
nokogiri (~> 1.6.3)
|
||||
paperclip (~> 3.5.4)
|
||||
pry
|
||||
pry-nav
|
||||
pry-byebug
|
||||
pry-rails
|
||||
pry-remote
|
||||
rack-mini-profiler!
|
||||
rack-openid
|
||||
rails (= 3.2.13)
|
||||
|
|
|
@ -1,11 +1,5 @@
|
|||
module Mobile
|
||||
|
||||
$LOAD_PATH << File.expand_path('..',__FILE__)
|
||||
|
||||
autoload :Auth, 'apis/auth'
|
||||
autoload :Users, 'apis/users'
|
||||
autoload :Courses, 'apis/courses'
|
||||
|
||||
class API < Grape::API
|
||||
version 'v1', using: :path
|
||||
format :json
|
||||
|
@ -30,9 +24,9 @@ module Mobile
|
|||
end
|
||||
end
|
||||
|
||||
mount Auth
|
||||
mount Users
|
||||
mount Courses
|
||||
mount Apis::Auth
|
||||
mount Apis::Users
|
||||
mount Apis::Courses
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,13 @@
|
|||
module Mobile
|
||||
|
||||
module Entities
|
||||
class Auth < Grape::Entity
|
||||
expose :token
|
||||
expose :user, using: User
|
||||
end
|
||||
end
|
||||
|
||||
module Apis
|
||||
class Auth < Grape::API
|
||||
resource :auth do
|
||||
desc "Creates and returns access_token if valid login"
|
||||
|
@ -11,14 +20,16 @@ module Mobile
|
|||
if user
|
||||
::ApiKey.delete_all(user_id: user.id)
|
||||
key = ::ApiKey.create!(user_id: user.id)
|
||||
data = {token: key.access_token}.merge(user.as_json).merge(user.extensions.as_json)
|
||||
{status: 0, data: data}
|
||||
api_user = UserService.new.show_user(user.id)
|
||||
present :data, {token: key, user: api_user}, using: Mobile:Entities::Auth
|
||||
present :status, 0
|
||||
else
|
||||
{status: 1, message: 'Unauthorized.'}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -1,27 +1,18 @@
|
|||
module Mobile
|
||||
module Apis
|
||||
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: Mobile::Entities::Course
|
||||
present :status, 0
|
||||
end
|
||||
|
||||
desc "Return a course"
|
||||
|
@ -37,5 +28,6 @@ module Mobile
|
|||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
module Mobile
|
||||
module Apis
|
||||
class Users < Grape::API
|
||||
resource :users do
|
||||
desc "get all users"
|
||||
|
@ -17,12 +18,15 @@ module Mobile
|
|||
begin
|
||||
user = us.register params.merge(:password_confirmation => params[:password],
|
||||
:should_confirmation_password => true)
|
||||
raise "register failed." if user.new_record?
|
||||
{status: 0, data: user}
|
||||
raise "register failed #{user.errors.full_messages}" if user.new_record?
|
||||
present :data, user, with: Mobile::Entities::User
|
||||
present :status, 0
|
||||
rescue => e
|
||||
{status: 1, message: e.message}
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
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
|
||||
end
|
|
@ -0,0 +1,20 @@
|
|||
module Mobile
|
||||
module Entities
|
||||
class User < Grape::Entity
|
||||
#头像
|
||||
#昵称
|
||||
expose :login
|
||||
#性别
|
||||
expose :gender do |user, opt|
|
||||
user.user_extentions.gender
|
||||
end
|
||||
#我的二维码
|
||||
#工作单位
|
||||
#邮箱地址
|
||||
expose :mail
|
||||
#地区
|
||||
#签名
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,26 @@
|
|||
if Rails.env.development?
|
||||
lib_ruby_files = Dir.glob(File.join("app/api/**", "*.rb"))
|
||||
lib_reloader ||= ActiveSupport::FileUpdateChecker.new(lib_ruby_files) do
|
||||
# lib_ruby_files.each do |lib_file|
|
||||
# puts "start require #{lib_file}"
|
||||
# require_dependency(lib_file)
|
||||
# end
|
||||
#
|
||||
|
||||
# binding.pry
|
||||
# if Object.const_defined?(:Mobile)
|
||||
# Object.send(:remove_const, :Mobile)
|
||||
# end
|
||||
#
|
||||
# $".delete_if {|s| s.include?('api/mobile') }
|
||||
# require File.join(Rails.root,"app/api/mobile/api.rb")
|
||||
Rails.application.reload_routes!
|
||||
end
|
||||
|
||||
ActionDispatch::Callbacks.to_prepare do
|
||||
lib_reloader.execute_if_updated
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue