From eac23bfb854157aaac0bbe9e7cdb225b4811b3dd Mon Sep 17 00:00:00 2001 From: "OHASHI, Norikazu" Date: Thu, 2 May 2019 07:09:17 +0900 Subject: [PATCH] =?utf8?q?=E8=94=B5=E6=9B=B8=E4=B8=80=E8=A6=A7=E3=82=92?= =?utf8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- create_table.sql | 9 +- sinatra/app/controllers/web_gui.rb | 124 +++++++++++++++++---- sinatra/app/models/books_db.rb | 103 ++++++++++------- sinatra/app/views/book_info.haml | 34 +++--- sinatra/app/views/book_list.haml | 106 ++++++++++++++++++ sinatra/app/views/book_regist.haml | 35 +++--- sinatra/app/views/coffee/check_form.coffee | 38 +++++++ sinatra/app/views/user_home.haml | 5 +- 8 files changed, 354 insertions(+), 100 deletions(-) create mode 100644 sinatra/app/views/book_list.haml diff --git a/create_table.sql b/create_table.sql index bfe59de..57d1864 100644 --- a/create_table.sql +++ b/create_table.sql @@ -15,13 +15,13 @@ CREATE TABLE books ( volume INTEGER, series VARCHAR(255), author VARCHAR(127), - orignal_author VARCHAR(127), + original_author VARCHAR(127), illustrator VARCHAR(127), translator VARCHAR(127), supervisor VARCHAR(127), publisher VARCHAR(127), pubdate DATE, - cover TEXT, + cover_uri TEXT, create_at DATETIME NOT NULL, update_at DATETIME NOT NULL); @@ -31,11 +31,10 @@ CREATE TABLE book_collections ( summary TEXT, book_rank INTEGER, create_at DATETIME NOT NULL, - update_at DATETIME NOT NULL, - PRIMARY KEY(isbn, user_id)); + update_at DATETIME NOT NULL); CREATE TABLE book_covers ( - key_hash VARCHAR(60) NOT NULL PRIMARY KEY, + key_hash VARCHAR(65) NOT NULL PRIMARY KEY, isbn VARCHAR(14) NOT NULL UNIQUE, mime_type VARCHAR(255) NOT NULL, cover LONGBLOB NOT NULL, diff --git a/sinatra/app/controllers/web_gui.rb b/sinatra/app/controllers/web_gui.rb index 12fdb9e..0adb31b 100644 --- a/sinatra/app/controllers/web_gui.rb +++ b/sinatra/app/controllers/web_gui.rb @@ -5,6 +5,7 @@ require 'sinatra/base' require 'sinatra' require 'haml' +require 'logger' # ユーザアクセス用モデル require_relative '../models/users_db' @@ -28,11 +29,23 @@ class WebGui < Sinatra::Base helpers do # ページIDを保存して、viewに渡す - # @params [Simbole] pageId 表示するページID + # @params [Symble] pageId 表示するページID def goPage(pageId) @pageId = pageId haml pageId end + + # + # @params [Hash] Upload用パラメータ + # @return [Hash] Uploadデータ情報 + def getUploadData(upload_param) + upload_data = Hash.new + file = upload_param[:tempfile] + upload_data[:mime_type] = upload_param[:type] + upload_data[:data] = file.read + + return upload_data + end end set :root, File.join(File.dirname(__FILE__), '..') @@ -44,7 +57,7 @@ class WebGui < Sinatra::Base use Rack::Session::Cookie, :expire_after => 1200, :secret => 'change' - + # スタイルシート get '/css/style.css' do scss :'scss/style' @@ -122,9 +135,7 @@ class WebGui < Sinatra::Base @id = id @username = user.full_name @is_addmin = user.isAddmin - @newest_list = [ {title: "書籍1", create_at: DateTime.now}, - {title: "書籍2", create_at: DateTime.now}, - {title: "書籍3", create_at: DateTime.now}] + @newest_list = BookManager.newestListOfBooks(id, 5) goPage :user_home end @@ -190,6 +201,14 @@ class WebGui < Sinatra::Base if (id == nil) raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。") end + begin + user = UserAccount.getUser(id) + @id = id + @username = user.full_name + @book_info = Hash.new + rescue UserAccount::NotFoundInstanceError + raise WebError.new(status: 404, message: "ユーザ情報が存在しません。") + end goPage :book_regist end @@ -222,42 +241,46 @@ class WebGui < Sinatra::Base # 書影の新規登録があるかを確認 image_f = false - if (book_info[:cover_base64] != null) + if (params[:cover_base64] != nil) image_f = true end params.each do |key, value| case key - when :sammary, :book_rank, :cover_base64, :cover_file + when 'summary', 'book_rank', 'cover_base64', 'cover_file', 'mime_type' then # 対象キーは書籍情報ではないので飛す next end - if ((key == :cover_uri) && (image_f)) + if ((key == "cover_uri") && (image_f)) # 登録するイメージがあるのでURIの登録は飛す next end - book_info[key] = value + book_info[key.to_sym] = value end begin isbn = book_info[:isbn] + # 書籍情報の更新 if (BookManager.isBook(isbn)) + book_info.delete(:isbn) BookManager.updateBook(isbn, book_info) else BookManager.createBook(book_info) end - + + cover_image = Hash.new # 書影情報の更新 if (image_f) - data = Base64.decode64(book_info[:cover_base64]) + 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)) - data = getUploadData(params[:cover_file]) + cover_image = getUploadData(params[:cover_file]) end - if (data != nil) - image_hash = BookManager.addBookCover(isbn, data) + 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 @@ -284,7 +307,12 @@ class WebGui < Sinatra::Base isbn = params[:isbn] begin - @book = searchISBN(isbn, id) + user = UserAccount.getUser(id) + @id = id + @username = user.full_name + @book_info = BookManager.searchISBN(isbn, id) + rescue UserAccount::NotFoundInstanceError + raise WebError.new(status: 404, message: "ユーザ情報が存在しません。") rescue BookManager::AlreadyInstanceError raise WebError.new(status: 409, message: "あなたの蔵書にすでに該当する書籍があります。", refs: "/book_regist") end @@ -299,13 +327,23 @@ class WebGui < Sinatra::Base if (id == nil) raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。") end - - @book = params - data = getUploadData(params[:cover_file]) - if (data != nil) - base64 = Base64.encode64(data) - @book[:cover_base64] = base64 - @book[:cover_uri] = "data:image/jpeg;base64," + base64 + + begin + user = UserAccount.getUser(id) + rescue UserAccount::NotFoundInstanceError + raise WebError.new(status: 404, message: "ユーザ情報が存在しません。") + end + + @id = id + @username = user.full_name + @book_info = params + cover_image = getUploadData(params[:cover_file]) + if (cover_image != nil) + base64 = Base64.encode64(cover_image[:data]) + mime_type = cover_image[:mime_type] + @book_info[:cover_base64] = base64 + @book_info[:mime_type] = mime_type + @book_info[:cover_uri] = "data:" + mime_type + ";base64," + base64 end goPage :book_regist end @@ -331,18 +369,58 @@ class WebGui < Sinatra::Base end begin + end + begin + user = UserAccount.getUser(id) + @id = id + @username = user.full_name @book_info = BookManager.getBookCollect(isbn, id) + rescue UserAccount::NotFoundInstanceError + raise WebError.new(status: 404, message: "ユーザ情報が存在しません。") rescue BookManager::NotFoundInstanceError - raise WebError.new(status: 404, message: "", refs: "/user_home") + raise WebError.new(status: 404, message: "蔵書情報が見付かりません。", refs: "/user_home") end goPage :book_info end + + get '/book_list' do + id = session[:userId] + list_status = session[:find_status] + if (id == nil) + raise WebError.new(status: 408, message: "セッション期限切れです。再ログインをしてください。") + end + begin + user = UserAccount.getUser(id) + @id = id + @username = user.full_name + rescue UserAccount::NotFoundInstanceError + raise WebError.new(status: 404, message: "ユーザ情報が存在しません。") + end + if (list_status == nil) + position = {start: 0, step: 10} + find_status = nil + else + position = list_status[:position] + find_status = list_status[:find_status] + end + books = BookManager.findBook(find_status) + @book_list = BookManager.narrowBookOfId(id, books, position[:start], position[:step]) + @start = position[:start] + @step = position[:step] + if (@book_list == nil) + raise WebError.new(status: 404, message: "対象の蔵書が見つかりません。", refs: "/user_home") + end + + goPage :book_list + end + # ログアウトページ get '/logout' do # 最終ログイン情報登録 goPage :logout end + # エラーページ error WebError do e = env['sinatra.error'] diff --git a/sinatra/app/models/books_db.rb b/sinatra/app/models/books_db.rb index 69a4e6d..8c6f5c6 100644 --- a/sinatra/app/models/books_db.rb +++ b/sinatra/app/models/books_db.rb @@ -105,7 +105,7 @@ class BookManager # ISBNから書籍情報検索してハッシュで返却 # @param [String] isbn # @return [Hash] 書籍情報 - def self.getHashOfBooks(book) + def self.getHashOfBooks(isbn) book = Book.find_by(isbn: isbn) if (book == nil) return nil @@ -117,13 +117,13 @@ class BookManager volume: book.volume, series: book.series, author: book.author, - orignal_author: book.orignal_author, + original_author: book.original_author, illustrator: book.illustrator, translator: book.translator, supervisor: book.supervisor, publisher: book.publisher, pubdate: book.pubdate, - cover_uri: book.cover + cover_uri: book.cover_uri } return book_hash end @@ -143,7 +143,7 @@ class BookManager when "翻訳", "訳" then key = :translator when "原著", "原" then - key = :orignal_author + key = :original_author when "監修", "監" then key = :supervisor else @@ -161,10 +161,10 @@ class BookManager # ISBNからopenBDを検索してハッシュで返却 # @param [String] isbn # @return [Hash] 書籍情報 - def self.getHashOfOpenBD(book) + def self.getHashOfOpenBD(isbn) client = HTTPClient.new res = client.get("http://api.openbd.jp/v1/get", isbn: isbn) - books = JSON.perse(res.body) + books = JSON.parse(res.body) if (books.size > 1) raise FailedGetInstance @@ -172,6 +172,9 @@ class BookManager if (books.size < 1) return nil end + if (books[0] == nil) + return nil + end book = books[0]["summary"] parsons = getParsons(book["author"]) book_hash = { @@ -181,7 +184,7 @@ class BookManager volume: book["volume"], series: book["series"], author: parsons[:author], - orignal_author: parsons[:orignal_author], + original_author: parsons[:original_author], translator: parsons[:translator], supervisor: parsons[:supervisor], illustrator: book[:illustrator], @@ -196,20 +199,20 @@ class BookManager # @param [Book] book 書籍情報 # @param [BookCollection] book_collect 蔵書情報 # @return [Hash] 書籍情報 - def createBookHash(book, book_collect) + def self.createBookHash(book, book_collect) { isbn: book[:isbn], title: book[:title], volume: book[:volume], series: book[:series], author: book[:author], - orignal_author: book[:orignal_author], + original_author: book[:original_author], translator: book[:translator], supervisor: book[:supervisor], illustrator: book[:illustrator], publisher: book[:publisher], pubdate: book[:pubdate], - cover_uri: book[:cover], + cover_uri: book[:cover_uri], summary: book_collect[:summary], rank: book_collect[:rank] } @@ -227,9 +230,11 @@ class BookManager cover = cover_image[:data] mime_type = cover_image[:mime_type] key_hash = Digest::SHA256.hexdigest(cover) - - if (BookCover.find_by(isbn: isbn) != nil) - raise AlreadyInstanceError + book_cover = BookCover.find_by(isbn: isbn) + if (book_cover != nil) + if (no book_cover.destroy) + raise DbAccessError + end end book_cover = BookCover.new book_cover.key_hash = key_hash @@ -250,7 +255,7 @@ class BookManager # @return [BookCover] 書影情報 def self.getBookCover(key_hash) book_cover = BookCover.find_by(key_hash: key_hash) - if (book_cover) + if (book_cover == nil) raise NotFoundInstanceError end return book_cover @@ -289,9 +294,12 @@ class BookManager raise AlreadyInstanceError end book_info = getHashOfBooks(isbn) - if (book_info != nil) + if (book_info == nil) book_info = getHashOfOpenBD(isbn) end + if (book_info == nil) + book_info = {isbn: isbn} + end return book_info end @@ -304,15 +312,9 @@ class BookManager end book = Book.new book_info.each do |key, value| - if ((key == :cover_image) || (key == :isbn)) - # 書影のイメージとISBNはここでは設定しない - next - end - if (key == :cover_uri) - # 書影にはURIのキーを登録 - book.cover = value + if (key == :pubdate) + book.pubdate = Date.parse(value) else - # その他のステータスはそのまま登録 method = key.to_s + "=" book.send(method, value) end @@ -343,7 +345,7 @@ class BookManager # 書籍情報の変更 # @param [String] isbn_str # @param [Hash] columns 変更するカラムと値のハッシュ - def self.updateBook(id, columns) + def self.updateBook(isbn_str, columns) isbn = toIsbn(isbn_str) book = Book.find_by(isbn: isbn) change_f = false @@ -356,13 +358,13 @@ class BookManager # 値がない、値が空文字の場合は対象としない。 next end - if (key == :cover_uri) - method = "cover=" - else - method = key.to_s + "=" - end - if (value != book.send(method.chop)) - book.send(method, value) + if (value != book.send(key)) + if (key == :pubdate) + book.pubdate = Date.parse(value) + else + method = key.to_s + "=" + book.send(method, value) + end change_f = true end end @@ -370,7 +372,7 @@ class BookManager # 更新内容の適用 if (change_f) book.update_at = DateTime.now - if (book.save) + if (not book.save) raise DbAccessError end end @@ -383,6 +385,11 @@ class BookManager def self.findBook(find_keys) books = Book.all + + if (find_keys == nil) + return books + end + find_keys.each do |key, value| if (books == nil) break @@ -427,13 +434,26 @@ class BookManager # @param [Integer] user_id ユーザID # @param [Arrray] books 絞り込み対象の書籍情報一覧 # @return [Array] 絞り込んだ書籍情報 - def narrowBookOfId(user_id, books) + def self.narrowBookOfId(user_id, books, start, step) + if (books.size < start) + return nil + end + if (books.size < start + step) + step = books.size - start + end narrow_books=Array.new + count = 0 books.each do |book| book_collect = BookCollection.find_by(user_id: user_id, isbn: book.isbn) if (book_collect != nil) - narrow_book = createBookHash(book, book_collect) - narrow_books.push(narrow_book) + if (count >= start + step) + break + end + if ((count >= start) && (count < start + step)) + narrow_book = createBookHash(book, book_collect) + narrow_books.push(narrow_book) + end + count += 1 end end return narrow_books @@ -444,7 +464,7 @@ class BookManager # @param [Integer] user_id 取得対象のユーザID # @param [Arrray] books 絞り込み対象の書籍情報一覧 # @return [Array] 絞り込んだ書籍情報 - def getBookCollect(isbn_str, user_id) + def self.getBookCollect(isbn_str, user_id) isbn = toIsbn(isbn_str) book = Book.find_by(isbn: isbn) if (book == nil) @@ -463,14 +483,17 @@ class BookManager # @param [String] summary ユーザ毎の紹介分 # @param [Integer] book_rank ユーザの評価 # @return [BookCollection] 蔵書情報 - def createBookCollect(isbn, user_id, summary, book_rank) + def self.createBookCollect(isbn, user_id, summary, book_rank) if (BookCollection.find_by(isbn: isbn, user_id: user_id) != nil) raise NotFoundInstanceError end + book_collect = BookCollection.new book_collect.isbn = isbn book_collect.user_id = user_id - book_collect.summary = summary + if ((summary != nil) && (summary != "")) + book_collect.summary = summary + end book_collect.book_rank = book_rank book_collect.create_at = DateTime.now book_collect.update_at = DateTime.now @@ -516,10 +539,10 @@ class BookManager # 蔵書の新規登録リスト取得 # @param [Integer] user_id 取得対象のユーザID # @return [Array] 取得した書籍情報 - def self.newerListOfBooks(user_id, num) + def self.newestListOfBooks(user_id, num) books = Array.new book_collects = BookCollection.where(user_id: user_id). - oder(create_at: :desc).first(num) + order(create_at: :desc).first(num) book_collects.each do |item| book = Book.find_by(isbn: item.isbn) books.push(book) diff --git a/sinatra/app/views/book_info.haml b/sinatra/app/views/book_info.haml index 720f68a..2b0dda2 100644 --- a/sinatra/app/views/book_info.haml +++ b/sinatra/app/views/book_info.haml @@ -4,11 +4,11 @@ - book_info = @book_info %h2 - #{book_info[:title]} + 「#{book_info[:title]}」 - %image{:border => '1', :src => book_info[:cover_uri], :width => '256', :height => '364'} - -.book_table +%image{:border => '1', :src => book_info[:cover_uri], :width => '160', :height => '230'} + +.book_info %table %tr %th @@ -20,64 +20,64 @@ 書名 %td #{book_info[:title]} - - if book_info [:value] != nil - : #{book_info[:value]} 巻 - - if book_info[:series] != nil + - if book_info [:volume] != nil + : #{book_info[:volume]} 巻 + - if book_info[:series] != nil and book_info[:series] != "" %tr %th 単行本シリーズ %td #{book_info[:series]} - - if book_info[:author] != nil + - if book_info[:author] != nil and book_info[:author] != "" %tr %th 著者 %td #{book_info[:author]} - - if book_info[:orignal_author] != nil + - if book_info[:original_author] != nil and book_info[:original_author] != "" %tr %th 原作者 %td - #{book_info[:orignal_author]} - - if book_info[:illustrator] + #{book_info[:original_author]} + - if book_info[:illustrator] != nil && book_info[:illustrator] != "" %tr %th 作画 %td #{book_info[:illustrator]} - - if book_info[:translator] != nil + - if book_info[:translator] != nil and book_info[:translator] != "" %tr %th 翻訳 %td #{book_info[:translator]} - - if book_info[:supervisor] != nil + - if book_info[:supervisor] != nil and book_info[:supervisor] != "" %tr %th 監修 %td #{book_info[:supervisor]} - - if book_info[:publisher] != nil + - if book_info[:publisher] != nil and book_info[:publisher] != "" %tr %th 出版社 %td #{book_info[:publisher]} - - if book_info[:pubdate] != nil + - if book_info[:pubdate] != nil and book_info[:pubdate] != "" %tr %th 出版日 %td #{book_info[:pubdate]} - - if book_info[:summary] != nil + - if book_info[:summary] != nil and book_info[:summary] != "" %tr %th 書籍概要 %td %pre #{book_info[:summary]} - - if book_info[:book_rank] != nil + - if book_info[:book_rank] != nil and book_info[:book_rank] != "" %tr %th 評価 diff --git a/sinatra/app/views/book_list.haml b/sinatra/app/views/book_list.haml new file mode 100644 index 0000000..29d8403 --- /dev/null +++ b/sinatra/app/views/book_list.haml @@ -0,0 +1,106 @@ +- # encoding: utf-8 +- user_name = @username; book_list = @book_list +- start = @start; step = @step; index = @start +- if start + step > book_list.size + - next_step = 0 +- else + - next_step = step +- if start - step < 0 + - before_step = start +- else + - before_step = step +%h3 + #{user_name} さんの蔵書一覧は以下となります。 + +.find_form + %form{:action => "/book_find", :method => "post"} + %table + %tr + %th + 検索対象 + %th + 検索内容 + %th + + %tr + %td + %select{:name => 'kind', :id => 'kind'} + %option{ :value => 'title' } + 書名 + %option{ :value => 'author' } + 著者 + %option{ :value => 'publisher' } + 出版社 + %option{ :value => 'original_author'} + 原作 + %option{ :value => 'illustrator' } + 作画 + %option{ :value => 'translator' } + 翻訳 + %option{ :value => 'supervisor' } + 監修 + %option{ :value => 'series' } + 単行本シリーズ + %td + %input{ :type => 'text', :name => 'find_value', :id => 'find_value', :size => 60} + %td + %input{ :type => 'submit', :class => 'side_button', :value => '検索'} + +.seek_form + %form{:method => "post"} + %input{ :type => 'submit', :class => 'side_button', :formaction => "/book_list/before/#{before_step}", :value => "《#{before_step}件戻る", :disabled => (before_step == 0) } + %label{ :for => 'step' } + 表示件数 + %input{ :name => 'step', :type => 'text', :id => 'step', :size => 5, :value => step, :pattern => '[0-5]{1,4}'} + %label{ :type => 'submit', :class => 'side_button', :formaction => '/book_list/chage_step', :value => '表示件数変更'} + %input{ :type => 'submit', :class => 'side_button', :formaction => "/book_list/next/#{next_step}", :value => "#{next_step}件進む》", :disabled => (next_step == 0) } + %input{ :type => 'hidden', :name => 'start', :value => start} + +.book_list + %table + %tr + %th.number + 項番 + %th.title + 書名 + %th.author + 著者 + %th.publisher + 出版社 + %th.pubdate + 出版日 + %th.controll + 操作 + - book_list.each do |book| + - book_id = index; index += 1 + - isbn = book[:isbn]; publisher = book[:publisher]; pubdate = book[:pubdate] + - if book[:volume] != nil + - title = "#{book[:title]} : #{book[:volume]} 巻" + - else + - title = book[:title] + - if book [:author] != nil and book[:author] !="" + - author = book[:author] + - else + - author = "" + - if book [:original_author] != nil and book[:orignal_author] != "" + - author += "原作:#{book[:oritnal_author]} " + - if book[:illustrator] != nil and book[:illustrator] != "" + - author += "作画:#{book[:illustrator]} " + %tr + %td.number + #{book_id} + %td.title + %a{ :href => "/book_info/#{isbn}" } + #{title} + %td.author + #{author} + %td.publisher + #{publisher} + %td.pubdate + #{pubdate} + %td.controll + %input{ :type => 'button', :class => 'side_button', :onclick =>"location.href='/book_edit/#{isbn}'", :value => '編集' } + %input{ :type => 'button', :class => 'side_button', :onclick =>"location.href='/book_delete/#{isbn}'", :value => '削除' } + + + diff --git a/sinatra/app/views/book_regist.haml b/sinatra/app/views/book_regist.haml index e802e79..3afdecf 100644 --- a/sinatra/app/views/book_regist.haml +++ b/sinatra/app/views/book_regist.haml @@ -2,6 +2,11 @@ - admin_f = @admin_f - id = @id - book_info = @book_info +- if book_info[:title] != nil + - isbn_regist_f = true +- else + - isbn_regist_f = false + %h3 新規に蔵書の情報を登録することができます。 @@ -14,24 +19,24 @@ %hr -%form{ :action => "/book_regist", :method => "post"} +%form{ :action => "/book_regist", :method => "post", :enctype => "multipart/form-data"} .formstyle .params .item %label{ :for => 'isbn' } %span ISBN: - %input{ :name => 'isbn', :type => 'text', :id => 'isbn', :value => book_info[:isbn], :readonly => (isbn != nil) } - %input{ :type => 'submit', :class => 'side_button', :value => 'ISBN探索', formaction => '/book_search_isbn' } + %input{ :name => 'isbn', :type => 'text', :id => 'isbn', :value => book_info[:isbn], :readonly => isbn_regist_f, :required => isbn_regist_f} + %input{ :type => 'submit', :class => 'side_button', :value => 'ISBN探索', :formaction => '/book_search_isbn' } .item %label{ :for => 'title' } - 書名: + %span + 書名: %input{ :name => 'title', :type => 'text', :id => 'title', :value => book_info[:title]} %label{ :for => 'volume' } - %span - 巻数: + 巻数: %input{ :name => 'volume', :type => 'text', :id => 'volume', :size => 3, :value => book_info[:volume]} .item @@ -49,7 +54,7 @@ .item %label{ :for => 'original_author'} %span - 原作者: + 原作: %input{ :name => 'original_author', :type => 'text', :id => 'original_author', :value => book_info[:orignal_author]} .item @@ -74,24 +79,26 @@ %label{ :for => 'publisher' } %span 出版社: - %input{ :name => 'sublisher', :type => 'text', :id => 'publisher', :value => book_info[:publisher]} + %input{ :name => 'publisher', :type => 'text', :id => 'publisher', :value => book_info[:publisher]} .item %label{ :for => 'pubdate' } %span 出版日: - %input{ :name => 'pubdate', :type => 'date', :id => 'pubdate', :value => book_info[:pubdate]} + %input{ :name => 'pubdate', :type => 'text', :id => 'pubdate', :value => book_info[:pubdate], :oninput => 'validDateForm(this)'} .item %label{ :for => 'cover_file' } %span 書影: - - if cover_uri != nil - %image{ :border => '1', :src => cover_uri, :class => 'cover_image', :width => '128', :height => '182'} - - if cover_base64 != nil + - if book_info[:cover_uri] != nil + %image{ :border => '1', :src => book_info[:cover_uri], :class => 'cover_image', :width => '128', :height => '182'} + %input{ :name => 'cover_uri', :type => 'hidden', :value => book_info[:cover_uri]} + - if book_info[:cover_base64] != nil %input{ :name => 'cover_base64', :type => 'hidden', :value => book_info[:cover_base64]} + %input{ :name => 'mime_type', :type => 'hidden', :value => book_info[:mime_type]} %input{ :name => 'cover_file', :type => 'file', :id => 'cover_file', :accept => 'image/png, image/jpeg'} - %input{ :type => 'submit', :class => 'side_button', :value => '書影Upload', formaction => '/book_upload_cover' } + %input{ :type => 'submit', :class => 'side_button', :value => '書影Upload', :formaction => '/book_upload_cover' } .item %label{ :for => 'summary' } @@ -108,7 +115,7 @@ .buttons %hr - %input{ :type => 'submit', :class=>'push_button', :value => '送信'} + %input{ :type => 'submit', :class=>'push_button', :value => '送信', :onclick => 'validDateId("pubdate")'} %input{ :type => 'button', :class=>'push_button', :onclick =>"location.href='/user_home'", :value => 'もどる' } diff --git a/sinatra/app/views/coffee/check_form.coffee b/sinatra/app/views/coffee/check_form.coffee index b87061f..fb0e119 100644 --- a/sinatra/app/views/coffee/check_form.coffee +++ b/sinatra/app/views/coffee/check_form.coffee @@ -17,4 +17,42 @@ window.checkSameForm = (my_form, other_id, message) -> my_form.setCustomValidity(message) else my_form.setCustomValidity('') + + +isValidDate = (value) -> + if (/^\d{1,4}(\/|-)\d{1,2}\1\d{1,2}$/.test(value)) + text = value + else if (/^\d{4}\d{2}\d{2}$/.test(value)) + text = (value.substr(0,4) + '-' + value.substr(4,2) + '-' + value.substr(6,2)) + else + return false + + [year, month, day] = + text.split(/\/|-/).map (v) -> parseInt(v, 10) + return (year >= 1) && (1 <= month && month <= 12) && + ( 1 <= day && day <= daysInMonth(year, month)) + +daysInMonth = (year, month) -> + if (month == 2 ) + if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) + return 29 + else + return 28 + return { + 1: 31, 2: 28, 3: 31, 4: 30, + 5: 31, 6: 30, 7: 31, 8: 31, + 9: 30, 10: 31, 11: 30, 12: 31 + }[month] + +window.validDateForm = (form) -> + result = isValidDate(form.value) + if (!result) + form.setCustomValidity('日付のフォーマットが正しくありません。') + else + form.setCustomValidity('') + + +window.validDateId = (id) -> + validDateForm(document.getElementById(id)) + diff --git a/sinatra/app/views/user_home.haml b/sinatra/app/views/user_home.haml index 183e806..087e1f1 100644 --- a/sinatra/app/views/user_home.haml +++ b/sinatra/app/views/user_home.haml @@ -10,6 +10,9 @@ %ul - newest_list.each do |item| - %li #{item[:title]} (登録日時:#{item[:create_at].strftime("%Y/%m/%d %T")}) + - if item[:volume] == nil + %li #{item[:title]} (登録日時:#{item[:create_at].strftime("%Y/%m/%d %T")}) + - else + %li #{item[:title]} : #{item[:volume]} 巻 (登録日時:#{item[:create_at].strftime("%Y/%m/%d %T")}) -- 2.19.2