update client api

This commit is contained in:
2013-02-27 23:56:50 +08:00
parent b5ce4d4abe
commit 02a9b70e8c
4 changed files with 29 additions and 11 deletions
+3 -3
View File
@@ -33,19 +33,19 @@ Build Status
* 2013.2.19 用户可以删除自己的key
* 2013.2.21 添加图书搜索API
* 2013.2.26 添加归还已经借阅图书的API
* 2013.2.27 修改借书逻辑,由之前的系统自动查找合适人选,变为借书者自行选择合适的拥有书者。
* 2013.2.27 修改借书逻辑,由之前的系统自动查找合适人选,变为借书者自行选择合适的拥有书者。修改bookinfo API增加返回所有当前可借的实例信息
#### JSON API使用指南
* GET /api/userinfo/:user_id 用户用户的相关信息
* GET /api/bookinfo/:isbn 需要传入书籍的13位ISBN号,获取书籍的相关信息,拥有书籍的用户,和当前可借的用户信息
* GET /api/bookinfo/:isbn 需要传入书籍的13位ISBN号,获取书籍的相关信息,拥有书籍的用户,和当前所有可借的用户信息
* GET /api/books 以列表形式获取图书信息,每页10条信息,如要获取第二页信息地址则为 /api/books/?page=2,依次类推
* GET /api/book/search/:keyword 图书搜索,最多返回20条相关记录
* POST /api/auth 客户端使用auth key,进行验证登录,验证成功后,可以使用该key,进行所有需要认证的操作。
必填参数key,auth在用户中心,点击小锁图片即可进入KEY管理页面。(KEY不区分大小写)
* POST '/api/books/add', 添加图书,必须填入图书的ISBN(isbn),和Auth key(key)。括号为传递参数的实际名称
* POST '/api/book/return' 归还已经借阅的图书,必需参数Auth key(key),Book binstance id(instance_id )
* POST '/api/books/borrow' 借阅图书,必需参数Auth key(key),Book binstance id(instance_id )
#### Road map:
* 移动客户端的支持,用户可以在手机上查找图书,通过扫描二维码添加图书。
+20 -3
View File
@@ -9,7 +9,7 @@ class ApiController < ApplicationController
end
render json: book.to_json(include: [{:users =>
{except: [:api_key, :email, :created_at]}},
:available_instance])
:total_available_instances])
end
def user_info
@@ -72,6 +72,22 @@ class ApiController < ApplicationController
end
def borrow_book
key = AuthKey.find_by_value params[:key]
unless key
render json: {status: 'error', message: 'key is invalid,please change an other key.'}
return
end
instance = BookInstance.find_by_id params[:instance_id]
if instance!=nil && instance.borrowed? != true
user = User.find_by_id key.user_id
user.borrow instance
render json: {message: 'Borrow book success.', status: 'success'}
return
end
render json: {message: 'Sorry, you can not borrow this book right now.', status: 'error'}
end
def return_book
key = AuthKey.find_by_value params[:key]
unless key
@@ -79,8 +95,9 @@ class ApiController < ApplicationController
return
end
instance = BookInstance.find_by_id params[:instance_id]
if @current_user.borrowed_and_not_returned_books.include? instance
@current_user.return_book instance
user = User.find_by_id key.user_id
if user.borrowed_and_not_returned_books.include? instance
user.return_book instance
render json: {message: 'Return book success.', status: 'success'}
return
end
+5 -4
View File
@@ -16,7 +16,7 @@ Libr::Application.routes.draw do
get '/douban_callback' => 'home#douban_callback'
get '/qq_callback' => 'home#qq_callback'
get '/users/:id' => 'user#view'
get '/users/:id/edit' =>'user#edit'
get '/users/:id/edit' => 'user#edit'
put '/users/:id' => 'user#update'
get '/search' => 'search#do_search'
delete '/users/:user_id/books/:instance_id' => 'user#delete_book'
@@ -28,10 +28,11 @@ Libr::Application.routes.draw do
get 'api/bookinfo/:isbn' => 'api#book_info'
get 'api/userinfo/:user_id' => 'api#user_info'
get 'api/books' => 'api#books'
post 'api/auth' =>'api#auth'
get 'api/books/search/:keyword'=>'api#search'
post 'api/auth' => 'api#auth'
get 'api/books/search/:keyword' => 'api#search'
post 'api/books/add' => 'api#add_book'
post 'api/books/return' =>'api#return_book'
post 'api/books/return' => 'api#return_book'
post 'api/books/borrow' =>'api#borrow_book'
# The priority is based upon order of creation:
# first created -> highest priority.
+1 -1
View File
@@ -1,6 +1,6 @@
module Utils
def random_key(length=8)
charset = %w{0 1 2 3 4 6 7 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! @ # $ % & *}
charset = %w{0 1 2 3 4 6 7 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ! @ $ *}
(0...length).map{ charset.to_a[rand(charset.size)] }.join
end