Merge branch 'api' of http://repository.trustie.net/xianbo/trustie2 into api
This commit is contained in:
commit
7d70a6b2cb
|
@ -17,3 +17,4 @@
|
||||||
/config/configuration.yml
|
/config/configuration.yml
|
||||||
.rbenv-gemsets
|
.rbenv-gemsets
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
public/api_doc/
|
||||||
|
|
1
Gemfile
1
Gemfile
|
@ -20,6 +20,7 @@ gem "builder", "3.0.0"
|
||||||
gem 'acts-as-taggable-on', '2.4.1'
|
gem 'acts-as-taggable-on', '2.4.1'
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
|
gem 'grape-swagger'
|
||||||
gem 'puma'
|
gem 'puma'
|
||||||
gem 'better_errors', path: 'lib/better_errors'
|
gem 'better_errors', path: 'lib/better_errors'
|
||||||
gem 'rack-mini-profiler', path: 'lib/rack-mini-profiler'
|
gem 'rack-mini-profiler', path: 'lib/rack-mini-profiler'
|
||||||
|
|
|
@ -110,6 +110,9 @@ GEM
|
||||||
grape-entity (0.4.4)
|
grape-entity (0.4.4)
|
||||||
activesupport
|
activesupport
|
||||||
multi_json (>= 1.3.2)
|
multi_json (>= 1.3.2)
|
||||||
|
grape-swagger (0.8.0)
|
||||||
|
grape
|
||||||
|
grape-entity
|
||||||
guard (2.6.1)
|
guard (2.6.1)
|
||||||
formatador (>= 0.2.4)
|
formatador (>= 0.2.4)
|
||||||
listen (~> 2.7)
|
listen (~> 2.7)
|
||||||
|
@ -307,6 +310,7 @@ DEPENDENCIES
|
||||||
fastercsv (~> 1.5.0)
|
fastercsv (~> 1.5.0)
|
||||||
grape (~> 0.9.0)
|
grape (~> 0.9.0)
|
||||||
grape-entity
|
grape-entity
|
||||||
|
grape-swagger
|
||||||
guard-rails (~> 0.5.3)
|
guard-rails (~> 0.5.3)
|
||||||
guard-spork (~> 1.5.1)
|
guard-spork (~> 1.5.1)
|
||||||
guard-test (~> 1.0.0)
|
guard-test (~> 1.0.0)
|
||||||
|
|
|
@ -27,7 +27,9 @@ module Mobile
|
||||||
mount Apis::Auth
|
mount Apis::Auth
|
||||||
mount Apis::Users
|
mount Apis::Users
|
||||||
mount Apis::Courses
|
mount Apis::Courses
|
||||||
|
mount Apis::Watches
|
||||||
|
|
||||||
|
add_swagger_documentation ({api_version: 'v1', base_path: '/api'})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,12 @@ module Mobile
|
||||||
module Apis
|
module Apis
|
||||||
class Auth < Grape::API
|
class Auth < Grape::API
|
||||||
resource :auth do
|
resource :auth do
|
||||||
desc "Creates and returns access_token if valid login"
|
desc "用户登录"
|
||||||
params do
|
params do
|
||||||
requires :login, type: String, desc: 'Username or email'
|
requires :login, type: String, desc: 'Username or email'
|
||||||
requires :password, type: String, desc: 'Password'
|
requires :password, type: String, desc: 'Password'
|
||||||
end
|
end
|
||||||
post :login do
|
post do
|
||||||
user,last_logon = ::User.try_to_login(params[:login], params[:password])
|
user,last_logon = ::User.try_to_login(params[:login], params[:password])
|
||||||
if user
|
if user
|
||||||
::ApiKey.delete_all(user_id: user.id)
|
::ApiKey.delete_all(user_id: user.id)
|
||||||
|
@ -27,6 +27,17 @@ module Mobile
|
||||||
{status: 1, message: 'Unauthorized.'}
|
{status: 1, message: 'Unauthorized.'}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc "用户登出"
|
||||||
|
params do
|
||||||
|
requires :token, type: String
|
||||||
|
end
|
||||||
|
delete do
|
||||||
|
authenticate!
|
||||||
|
::ApiKey.delete_all(user_id: current_user.id)
|
||||||
|
{status: 0}
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,11 +2,11 @@ module Mobile
|
||||||
module Apis
|
module Apis
|
||||||
class Courses < Grape::API
|
class Courses < Grape::API
|
||||||
resource :courses do
|
resource :courses do
|
||||||
desc "get all courses"
|
desc "获取所有课程"
|
||||||
params do
|
params do
|
||||||
optional :school_id, type: Integer, desc: 'school number'
|
optional :school_id, type: Integer, desc: '传入学校id,返回该学校课程列表'
|
||||||
requires :per_page_count, type: Integer
|
requires :per_page_count, type: Integer, desc: '每页总数'
|
||||||
requires :page, type: Integer, desc: 'current page no'
|
requires :page, type: Integer, desc: '当前页码'
|
||||||
end
|
end
|
||||||
get do
|
get do
|
||||||
cs = CoursesService.new
|
cs = CoursesService.new
|
||||||
|
@ -15,7 +15,7 @@ module Mobile
|
||||||
present :status, 0
|
present :status, 0
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "Return a course"
|
desc "返回单个课程"
|
||||||
params do
|
params do
|
||||||
requires :id, type: Integer
|
requires :id, type: Integer
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,12 +2,8 @@ module Mobile
|
||||||
module Apis
|
module Apis
|
||||||
class Users < Grape::API
|
class Users < Grape::API
|
||||||
resource :users do
|
resource :users do
|
||||||
desc "get all users"
|
|
||||||
get do
|
|
||||||
['hello']
|
|
||||||
end
|
|
||||||
|
|
||||||
desc "add a user"
|
desc "注册用户"
|
||||||
params do
|
params do
|
||||||
requires :login, type: String, desc: 'username'
|
requires :login, type: String, desc: 'username'
|
||||||
requires :mail, type: String, desc: 'mail'
|
requires :mail, type: String, desc: 'mail'
|
||||||
|
@ -27,8 +23,9 @@ module Mobile
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
desc "modify user"
|
desc "修改用户"
|
||||||
params do
|
params do
|
||||||
|
requires :token, type: String
|
||||||
#optional :file, type: File, desc: 'avatar'
|
#optional :file, type: File, desc: 'avatar'
|
||||||
optional :occupation, type: String
|
optional :occupation, type: String
|
||||||
optional :brief_introduction, type: String
|
optional :brief_introduction, type: String
|
||||||
|
@ -40,7 +37,7 @@ module Mobile
|
||||||
authenticate!
|
authenticate!
|
||||||
us = UsersService.new
|
us = UsersService.new
|
||||||
begin
|
begin
|
||||||
ue = us.edit_user params
|
ue = us.edit_user params.merge(id: current_user.id)
|
||||||
{status: 0, data: ue}
|
{status: 0, data: ue}
|
||||||
rescue => e
|
rescue => e
|
||||||
{status: 1, message: e.message}
|
{status: 1, message: e.message}
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
module Mobile
|
||||||
|
module Apis
|
||||||
|
class Watches < Grape::API
|
||||||
|
resource :watches do
|
||||||
|
|
||||||
|
desc "获取所有关注"
|
||||||
|
params do
|
||||||
|
requires :token, type: String
|
||||||
|
end
|
||||||
|
get do
|
||||||
|
authenticate!
|
||||||
|
ws = UsersService.new
|
||||||
|
ws.user_watcher(id: current_user.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
desc "关注某人"
|
||||||
|
params do
|
||||||
|
requires :token, type: String
|
||||||
|
requires :object_id, type: Integer, desc: '关注的用户的id'
|
||||||
|
end
|
||||||
|
post do
|
||||||
|
authenticate!
|
||||||
|
ws = WatchesService.new
|
||||||
|
begin
|
||||||
|
o = ws.watch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
||||||
|
{status:0 , data: o}
|
||||||
|
rescue =>e
|
||||||
|
{status:1, message: e.message}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
desc "取消关注"
|
||||||
|
params do
|
||||||
|
requires :token, type: String
|
||||||
|
requires :object_id, type: Integer, desc: '取消关注的用户的id'
|
||||||
|
end
|
||||||
|
delete do
|
||||||
|
authenticate!
|
||||||
|
ws = WatchesService.new
|
||||||
|
begin
|
||||||
|
ws.unwatch(params.merge({current_user_id:current_user.id, object_type:'user' }) )
|
||||||
|
{status:0}
|
||||||
|
rescue =>e
|
||||||
|
{status:1, message: e.message}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -17,7 +17,7 @@
|
||||||
class WatchersController < ApplicationController
|
class WatchersController < ApplicationController
|
||||||
before_filter :require_login#, :find_watchables, :only => [:watch, :unwatch]
|
before_filter :require_login#, :find_watchables, :only => [:watch, :unwatch]
|
||||||
def watch
|
def watch
|
||||||
s = Service.new
|
s = WatchesService.new
|
||||||
watchables = s.watch params.merge(:current_user_id => User.current.id)
|
watchables = s.watch params.merge(:current_user_id => User.current.id)
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_to_referer_or {render :text => (true ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
format.html { redirect_to_referer_or {render :text => (true ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
||||||
|
@ -33,7 +33,7 @@ class WatchersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def unwatch
|
def unwatch
|
||||||
s = Service.new
|
s = WatchesService.new
|
||||||
watchables = s.unwatch params.merge(:current_user_id => User.current.id)
|
watchables = s.unwatch params.merge(:current_user_id => User.current.id)
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_to_referer_or {render :text => (false ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
format.html { redirect_to_referer_or {render :text => (false ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class Service
|
class WatchesService
|
||||||
def watch params
|
def watch params
|
||||||
@current_user = User.find(params[:current_user_id])
|
@current_user = User.find(params[:current_user_id])
|
||||||
@watchables = find_watchables params
|
@watchables = find_watchables params
|
Loading…
Reference in New Issue