return upload_data
end
+
+ # ユーザID が nil ならばセッションエラー
+ # @params [int] id ユーザid
+ def checkSession(id)
+ if (id == nil)
+ raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
+ end
+ end
+
+ # 更新用の書籍情報を作成する。
+ # @param [Hash] params Postで取得した全パラメータ
+ # @return [Hash] 更新用書籍情報
+ def makeBookInfo(params)
+ book_info = Hash.new
+ params.each do |key, value|
+ case key
+ when 'summary', 'book_rank', 'cover_base64', 'cover_file', 'mime_type' then
+ # 対象キーは書籍情報ではないので飛す
+ next
+ end
+ if ((key == 'cover_uriD') && (image_f))
+ # 登録するイメージがあるのでURIの登録は飛す
+ next
+ end
+ book_info[key.to_sym] = value
+ end
+ return book_info
+ end
+
+ # 書影情報の作成
+ # @param [Boolean] image_f Base64のイメージデータ有
+ # @param [Hash] params Postで取得した全パラメータ
+ # @param [String] book_url 書影URL
+ # @return [Hash] 作成した書影データ
+ def setBookImage(image_f, params, book_url)
+ cover_image = Hash.new
+ # 書影情報の更新
+ if (image_f)
+ cover_image[:data] = Base64.decode64(params[:cover_base64])
+ cover_image[:mime_type] = params[:mime_type]
+ end
+ if ((not image_f) && (book_url == nil) &&
+ (params[:cover_file] != nil))
+ cover_image = getUploadData(params[:cover_file])
+ end
+ return cover_image
+ end
end
# スタイルシート
get '/user_home' do
cache_control :public, :must_revalidate, :max_age => 30
id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
user = UserAccount.getUser(id)
@id = id
@username = user.full_name
# @raise [WebError] ユーザID不正
get '/user_edit' do
id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
begin
user = UserAccount.getUser(id)
@is_admin = UserAccount.checkAdmin(id);
get '/user_edit/:editId' do
id = session[:userId]
edit_id = params[:editId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
if (not UserAccount.checkAdmin(id))
raise WebError.new(status: 405, message: "管理者アカウントでないと操作できません。")
end
email = params[:email]; try_pass = params[:try_pass]
new_pass = params[:new_pass]
- if (id == nil)
- raise WebError.new(staut: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
begin
if (new_pass != "")
check_id = UserAccount.checkPasswd(name, try_pass)
end
# ユーザ情報編集ページ(ユーザID指定)(POST)
+ # @path_param editId [Integer] 編集対象ユーザID
# @post_param name [String] ログインユーザ名,
# @post_param try_pass [String] 旧パスワード
# @post_param new_pass [String] 新パスワード
email = params[:email]
new_pass = params[:new_pass]
- if (id == nil)
- raise WebError.new(staut: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
if (not UserAccount.checkAdmin(id))
raise WebError.new(status: 405, message: "管理者アカウントでないと操作できません。")
end
id = session[:userId]
book_update_f = false
session[:book_update_f] = book_update_f
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
if (not UserAccount.checkAdmin(id))
raise WebError.new(status: 405, message: "管理者アカウントでないと操作できません。")
end
cache_control :public, :must_revalidate, :max_age => 30
id = session[:userId]
delete_id = params[:deleteId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
if (not UserAccount.checkAdmin(id))
raise WebError.new(status: 405, message: "管理者アカウントでないと操作できません。")
end
cache_control :public, :must_revalidate, :max_age => 30
id = session[:userId]
delete_id = params[:deleteId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
if (not UserAccount.checkAdmin(id))
raise WebError.new(status: 405, message: "管理者アカウントでないと操作できません。")
end
id = session[:userId]
book_update_f = false
session[:book_update_f] = book_update_f
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
begin
user = UserAccount.getUser(id)
@id = id
@username = user.full_name
@book_info = Hash.new
@update_f = book_update_f
- @is_admin = UserAccount.checkAdmin(id);
+ @is_admin = UserAccount.checkAdmin(id)
rescue UserAccount::NotFoundInstanceError
raise WebError.new(status: 404, message: "ユーザ情報が存在しません。")
end
book_update_f = true
session[:book_update_f] = book_update_f
isbn = params[:isbn]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ is_admin_books = UserAccount.checkAdmin(id) & session[:is_admin]
+ checkSession(id)
begin
user = UserAccount.getUser(id)
@id = id
@username = user.full_name
- @book_info = BookManager.getBookCollect(isbn, id)
+ @book_info = is_admin_books ? BookManager.getBook(isbn) :
+ BookManager.getBookCollect(isbn, id)
@update_f = book_update_f
- @is_admin = UserAccount.checkAdmin(id);
+ @is_admin = UserAccount.checkAdmin(id)
+ @is_admin_books = is_admin_books
rescue UserAccount::NotFoundInstanceError
raise WebError.new(status: 404, message: "ユーザ情報が存在しません。")
end
post '/book_regist' do
id = session[:userId]
book_update_f = session[:book_update_f]
- book_info = Hash.new
-
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
-
- # 書影の新規登録があるかを確認
- image_f = false
- if (params[:cover_base64] != nil)
- image_f = true
- end
+ checkSession(id)
+ is_admin_books = UserAccount.checkAdmin(id) & session[:is_admin]
- params.each do |key, value|
- case key
- when 'summary', 'book_rank', 'cover_base64', 'cover_file', 'mime_type' then
- # 対象キーは書籍情報ではないので飛す
- next
- end
- if ((key == "cover_uri") && (image_f))
- # 登録するイメージがあるのでURIの登録は飛す
- next
- end
- book_info[key.to_sym] = value
- end
+ # 書影の新規登録があるかを確認
+ image_f = (params[:cover_base64] != nil)
+ # 書籍の設定情報を選出
+ book_info = makeBookInfo(params)
+
begin
isbn = book_info[:isbn]
BookManager.createBook(book_info)
end
- cover_image = Hash.new
+ cover_image = setBookImage(image_f, params, book_info[:cover_uri])
+
# 書影情報の更新
- if (image_f)
- cover_image[:data] = Base64.decode64(params[:cover_base64])
- cover_image[:mime_type] = params[:mime_type]
- end
- if ((not image_f) && (book_info[:cover_uri] == nil) &&
- (params[:cover_file] != nil))
- cover_image = getUploadData(params[:cover_file])
- end
if (cover_image[:data] != nil)
image_hash = BookManager.addBookCover(isbn, cover_image)
cover_uri = '/book_image/' + image_hash
BookManager.updateBook(isbn, cover_uri: cover_uri)
end
- # 蔵書情報の更新
- summary = params[:summary]; book_rank = params[:book_rank]
- if (book_update_f)
- BookManager.updateBookCollect(isbn, id, summary, book_rank)
- else
- BookManager.createBookCollect(isbn, id, summary, book_rank)
- end
-
+ if (!is_admin_books)
+ # 蔵書情報の更新
+ summary = params[:summary]; book_rank = params[:book_rank]
+ if (book_update_f)
+ BookManager.updateBookCollect(isbn, id, summary, book_rank)
+ else
+ BookManager.createBookCollect(isbn, id, summary, book_rank)
+ end
+ end
rescue BookManager::NotFoundInstanceError,
BookManager::AlreadyInstanceError
raise WebError.new(status: 400, message: "蔵書情報の登録に失敗しました。", refs: "/book_regist")
# @raise [WebError] 登録済みの蔵書だった。
post '/book_search_isbn' do
id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
isbn = params[:isbn]
begin
@id = id
@username = user.full_name
@book_info = BookManager.searchISBN(isbn, id)
- @is_admin = UserAccount.checkAdmin(id);
+ @is_admin = UserAccount.checkAdmin(id)
rescue UserAccount::NotFoundInstanceError
raise WebError.new(status: 404, message: "ユーザ情報が存在しません。")
rescue BookManager::AlreadyInstanceError
raise WebError.new(status: 409, message: "あなたの蔵書にすでに該当する書籍があります。", refs: "/book_regist")
+ rescue BookManager::FailedGetInstance
+ raise WebError.new(status: 500, message: "ISBNによる書籍検索に失敗しました。", refs: "/book_regist")
end
goPage :book_regist
end
- # ISBN からの書籍情報を検索する
+ # 書影データのアップロード
# @post_param [Hash] cover_file イメージファイル情報
# @raise [WebError] セッションの期限切れ
post '/book_upload_cover' do
id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
begin
user = UserAccount.getUser(id)
@id = id
@username = user.full_name
@book_info = params
- @is_admin = UserAccount.checkAdmin(id);
+ @is_admin = UserAccount.checkAdmin(id)
cover_image = getUploadData(params[:cover_file])
if (cover_image != nil)
base64 = Base64.encode64(cover_image[:data])
# 書影の表示
+ # @path_param [String] hash 書影のハッシュキー
get '/book_image/:hash' do
cache_control :public, :must_revalidate, :max_age => 30
image_hash = params[:hash]
end
# 蔵書情報の取得ページ
+ # @path_param [String] isbn 取得対象のISBN
# @raise [WebError] セッションの期限切れ
# @raise [WebError] ユーザID不正
# @raise [WebError] 蔵書情報がない
cache_control :public, :must_revalidate, :max_age => 30
id = session[:userId]
isbn = params[:isbn]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
- begin
- end
begin
user = UserAccount.getUser(id)
+ is_admin_books = UserAccount.checkAdmin(id) & session[:is_admin]
@id = id
@username = user.full_name
- @book_info = BookManager.getBookCollect(isbn, id)
- @is_admin = UserAccount.checkAdmin(id);
+ @book_info = is_admin_books ? BookManager.getBook(isbn) :
+ BookManager.getBookCollect(isbn, id)
+ @is_admin = UserAccount.checkAdmin(id)
rescue UserAccount::NotFoundInstanceError
raise WebError.new(status: 404, message: "ユーザ情報が存在しません。")
rescue BookManager::NotFoundInstanceError
# 蔵書一覧の取得(トリガページ)
# @raise [WebError] セッションの期限切れ
get '/book_list' do
- id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(session[:userId])
session[:list_status] = {position: {start: 0, step: 10},
find_status: nil}
+ session[:is_admin] = false
+ redirect to('/book_list/main')
+ end
+
+ # 管理用の蔵書一覧の取得(トリガページ)
+ # @raise [WebError] セッションの期限切れ
+ get '/book_admin' do
+ checkSession(session[:userId])
+ session[:list_status] = {position: {start: 0, step: 10},
+ find_status: nil}
+ session[:is_admin] = true
redirect to('/book_list/main')
end
# 蔵書一覧の取得(表示件数変更ページ)
# @raise [WebError] セッションの期限切れ
post '/book_list/change_step' do
- id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(session[:userId])
step = params[:step].to_i
session[:list_status][:position] = {start: 0, step: step}
redirect to('/book_list/main')
# 蔵書一覧の取得(次ページ)
# @raise [WebError] セッションの期限切れ
post '/book_list/next' do
- id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(session[:userId])
step = session[:list_status][:position][:step]
session[:list_status][:position][:start] += step
redirect to('/book_list/main')
# 蔵書一覧の取得(前ページ)
# @raise [WebError] セッションの期限切れ
post '/book_list/before' do
- id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(session[:userId])
step = session[:list_status][:position][:step]
session[:list_status][:position][:start] -= step
redirect to('/book_list/main')
# 蔵書一覧の取得(検索後)
# @raise [WebError] セッションの期限切れ
post '/book_list/find' do
- id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(session[:userId])
find_status = session[:list_status][:find_status]
find_key = params[:kind].to_sym
find_value = params[:find_value]
# 蔵書一覧の取得 (蔵書情報取得から)
# @raise [WebError] セッションの期限切れ
get '/book_list/fromInfo' do
- id = session[:userId]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(session[:userId])
list_status = session[:list_status]
if (list_status == nil)
session[:list_status] = {position: {start:0, step: 10},
get '/book_list/main' do
id = session[:userId]
list_status = session[:list_status]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
begin
user = UserAccount.getUser(id)
+ is_admin_books = UserAccount.checkAdmin(id) & session[:is_admin]
@id = id
@username = user.full_name
- @is_admin = UserAccount.checkAdmin(id);
+ @is_admin = UserAccount.checkAdmin(id)
+ @is_admin_books = is_admin_books
rescue UserAccount::NotFoundInstanceError
raise WebError.new(status: 404, message: "ユーザ情報が存在しません。")
end
position = list_status[:position]
find_status = list_status[:find_status]
- @book_list, @full_size = BookManager.narrowBookOfId(id, position[:start], position[:step], find_status)
+ @book_list, @full_size = is_admin_books ?
+ BookManager.narrowBooks(position[:start],
+ position[:step], find_status) :
+ BookManager.narrowBookOfId(id, position[:start],
+ position[:step], find_status)
@start = position[:start]
@step = position[:step]
if (@book_list == nil)
cache_control :public, :must_revalidate, :max_age => 30
id = session[:userId]
isbn = params[:isbn]
- if (id == nil)
- raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。")
- end
+ checkSession(id)
begin
user = UserAccount.getUser(id)
+ is_admin_books = UserAccount.checkAdmin(id) & session[:is_admin]
@id = id
@username = user.full_name
- @book_info = BookManager.getBookCollect(isbn, id)
- @is_admin = UserAccount.checkAdmin(id);
+ @book_info = is_admin_books ? BookManager.getBook(isbn) :
+ BookManager.getBookCollect(isbn, id)
+ @is_admin = UserAccount.checkAdmin(id)
rescue UserAccount::NotFoundInstanceError
raise WebError.new(status: 404, message: "ユーザ情報が存在しません。")
rescue BookManager::NotFoundInstanceError
get '/book_delete/result/:isbn' do
cache_control :public, :must_revalidate, :max_age => 30
id = session[:userId]
+ is_admin_books = UserAccount.checkAdmin(id) & session[:is_admin]
isbn = params[:isbn]
begin
- BookManager.deleteBookCollect(isbn, id)
+ is_admin_books ? BookManager.deleteBook(isbn) :
+ BookManager.deleteBookCollect(isbn, id)
rescue UserAccount::NotFoundInstanceError
raise WebError.new(status: 404, message: "ユーザ情報が存在しません。")
rescue BookManager::NotFoundInstanceError
status e.params[:status]
@refs = e.params[:refs]
@error_message = e.params[:message]
- haml :error
+ goPage :error
end
end
# 正規表現によるレコード探索
# @param [Symbol] key カラム名
# @param [String] pattern 正規表現パターン
- # @return [Array<User>] 対象となるユーザ一覧
+ # @return [Array<Book>] 対象となる書籍一覧
def self.with_regexp(key, pattern)
column = columns_hash[key.to_s].name
where("`#{table_name}`.`#{column}` REGEXP ?", pattern)
# 正規表現によるレコード探索
# @param [Symbol] key カラム名
# @param [String] pattern 正規表現パターン
- # @return [Array<User>] 対象となるユーザ一覧
+ # @return [Array<BookCollection>] 対象となる蔵書一覧
def self.with_regexp(key, pattern)
column = columns_hash[key.to_s].name
where("`#{table_name}`.`#{column}` REGEXP ?", pattern)
# 正規表現によるレコード探索
# @param [Symbol] key カラム名
# @param [String] pattern 正規表現パターン
- # @return [Array<User>] 対象となるユーザ一覧
+ # @return [Array<BookCover>] 対象となる書影一覧
def self.with_regexp(key, pattern)
column = columns_hash[key.to_s].name
where("`#{table_name}`.`#{column}` REGEXP ?", pattern)
# 正規表現によるレコード探索
# @param [Symbol] key カラム名
# @param [String] pattern 正規表現パターン
- # @return [Array<User>] 対象となるユーザ一覧
+ # @return [Array<BookFull>] 対象となるユーザ書籍一覧
def self.with_regexp(key, pattern)
column = columns_hash[key.to_s].name
where("`#{table_name}`.`#{column}` REGEXP ?", pattern)
def self.getHashOfOpenBD(isbn)
client = HTTPClient.new
res = client.get("http://api.openbd.jp/v1/get", isbn: isbn)
+ if (res.status != HTTP::Status::OK)
+ raise FailedGetInstance
+ end
books = JSON.parse(res.body)
if (books.size > 1)
# @return [Hash] 書籍情報
def self.getHashOfNDL(isbn)
client = HTTPClient.new
- res = client.get("http://iss.ndl.go.jp/api/sru",
+ res = client.get("https://iss.ndl.go.jp/api/sru",
{'operation' => 'searchRetrieve',
'query' => "isbn=#{isbn}",
'recordSchema' => 'dcndl'})
+ if (res.status != HTTP::Status::OK)
+ raise FailedGetInstance
+ end
library_param = REXML::Document.new(res.body)
book_num = library_param.elements['//searchRetrieveResponse/numberOfRecords'].text.to_i
if (book_num < 1)
end
parsons = getParsonsFromNDL(creators)
book_hash = {
- search_type: SEARCH_ON_OPENBD,
+ search_type: SEARCH_ON_OPENNDL,
isbn: isbn,
title: book.elements['dcterms:title'] != nil ? book.elements['dcterms:title'].text : nil,
volume: book.elements['dcndl:volume/rdf:Description/rdf:value'] != nil ? book.elements['dcndl:volume/rdf:Description/rdf:value'].text.gsub(/[^\d]/, "").to_i : nil,
def self.getHashOfGBS(isbn)
client = HTTPClient.new
res = client.get("https://www.googleapis.com/books/v1/volumes", q: "isbn:#{isbn}")
+ if (res.status != HTTP::Status::OK)
+ raise FailedGetInstance
+ end
books = JSON.parse(res.body)
if (books["totalItems"] > 1)
# 書籍探索(ISBN)
# @param [String] isbn_str 探索対象のISBN
+ # @param [Integer] user_id 探索対象のユーザID
# @return [Hash] 書籍情報
def self.searchISBN(isbn_str, user_id)
isbn = toIsbn(isbn_str)
# @return [Book]
def self.getBook(isbn_str)
isbn = toIsbn(isbn_str)
-
- book = Book.find_by(isbn)
+ book = Book.find_by(isbn: isbn)
if (book == nil)
# 対象の書籍情報がなかった。
raise NotFoundInstanceError
end
end
- # ユーザでの絞り込み
+ #全書籍取得
+ # @param [Integer] start 表示開始位置
+ # @param [Integer] step 表示数
+ # @param [Hash] find_keys 検索対象
+ # @return [Array<Hash>] 絞り込んだ書籍情報
+ def self.narrowBooks(start, step, find_keys)
+ return narrowBookList(Book.all,
+ start, step, find_keys);
+ end
+
+ #ユーザでの絞り込み
# @param [Integer] user_id ユーザID
# @param [Integer] start 表示開始位置
# @param [Integer] step 表示数
# @return [Array<Hash>] 絞り込んだ書籍情報
def self.narrowBookOfId(user_id, start, step, find_keys)
# ユーザに関連する書籍の一覧を取得する。
- find_books = BookFull.where(user_id: user_id)
+ return narrowBookList(BookFull.where(user_id: user_id),
+ start, step, find_keys);
+ end
+ #指定範囲の書籍一覧を取得
+ # @param [Array<Books>] find_books 探索の一覧
+ # @param [Integer] start 表示開始位置
+ # @param [Integer] step 表示数
+ # @param [Hash] find_keys 検索対象
+ # @return [Array<Hash>] 絞り込んだ書籍情報
+ def self.narrowBookList(find_books, start, step, find_keys)
# 条件に合う書籍を検索
if (find_keys != nil)
find_keys.each do |key, value|
reg_value = $~[1]
find_books = find_books.with_regexp(key, reg_value)
else
- find_books = books.where(key => value)
+ find_books = find_books.where(key => value)
end
end
end
narrow_books = find_books.limit(step).offset(start)
return narrow_books, find_books.size
end
-
#蔵書情報の取得
# @param [String] isbn_str 取得対象のISBN
# @param [Integer] user_id 取得対象のユーザID