关注接口完成

This commit is contained in:
guange 2014-12-10 16:23:56 +08:00
parent b9156b271e
commit 6a3ebb5e94
7 changed files with 66 additions and 14 deletions

View File

@ -27,6 +27,7 @@ module Mobile
mount Apis::Auth
mount Apis::Users
mount Apis::Courses
mount Apis::Watches
add_swagger_documentation ({api_version: 'v1', base_path: '/api'})
end

View File

@ -30,6 +30,7 @@ module Mobile
desc "用户登出"
params do
requires :token, type: String
end
delete do
authenticate!

View File

@ -2,11 +2,11 @@ module Mobile
module Apis
class Courses < Grape::API
resource :courses do
desc "get all courses"
desc "获取所有课程"
params do
optional :school_id, type: Integer, desc: 'school number'
requires :per_page_count, type: Integer
requires :page, type: Integer, desc: 'current page no'
optional :school_id, type: Integer, desc: '传入学校id,返回该学校课程列表'
requires :per_page_count, type: Integer, desc: '每页总数'
requires :page, type: Integer, desc: '当前页码'
end
get do
cs = CoursesService.new
@ -15,7 +15,7 @@ module Mobile
present :status, 0
end
desc "Return a course"
desc "返回单个课程"
params do
requires :id, type: Integer
end

View File

@ -2,12 +2,8 @@ module Mobile
module Apis
class Users < Grape::API
resource :users do
desc "get all users"
get do
['hello']
end
desc "add a user"
desc "注册用户"
params do
requires :login, type: String, desc: 'username'
requires :mail, type: String, desc: 'mail'
@ -27,8 +23,9 @@ module Mobile
end
desc "modify user"
desc "修改用户"
params do
requires :token, type: String
#optional :file, type: File, desc: 'avatar'
optional :occupation, type: String
optional :brief_introduction, type: String

View File

@ -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

View File

@ -17,7 +17,7 @@
class WatchersController < ApplicationController
before_filter :require_login#, :find_watchables, :only => [:watch, :unwatch]
def watch
s = Service.new
s = WatchesService.new
watchables = s.watch params.merge(:current_user_id => User.current.id)
respond_to do |format|
format.html { redirect_to_referer_or {render :text => (true ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
@ -33,7 +33,7 @@ class WatchersController < ApplicationController
end
def unwatch
s = Service.new
s = WatchesService.new
watchables = s.unwatch params.merge(:current_user_id => User.current.id)
respond_to do |format|
format.html { redirect_to_referer_or {render :text => (false ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}

View File

@ -1,4 +1,4 @@
class Service
class WatchesService
def watch params
@current_user = User.find(params[:current_user_id])
@watchables = find_watchables params