diff --git a/memoapp/.gitignore b/memoapp/.gitignore index 359f1bc..3e50d64 100644 --- a/memoapp/.gitignore +++ b/memoapp/.gitignore @@ -1 +1 @@ -public/memos.json +memos.json diff --git a/memoapp/app.rb b/memoapp/app.rb index 4f59076..150b337 100644 --- a/memoapp/app.rb +++ b/memoapp/app.rb @@ -3,8 +3,9 @@ require 'sinatra' require 'sinatra/reloader' require 'json' +require 'securerandom' -FILE_PATH = 'public/memos.json' +FILE_PATH = 'memos.json' # publicフォルダから移動 def load_memos File.exist?(FILE_PATH) ? JSON.parse(File.read(FILE_PATH)) : {} @@ -12,7 +13,7 @@ end def save_memos(memos) File.open(FILE_PATH, 'w') do |file| - file.write(JSON.pretty_generate(memos)) + file.write(JSON.generate(memos)) end end @@ -31,8 +32,8 @@ end post '/memos' do memos = load_memos - id = (memos.keys.map(&:to_i).max || 0) + 1 - memos[id.to_s] = { 'title' => params[:title], 'content' => params[:content] } + id = SecureRandom.uuid + memos[id] = { 'title' => params[:title], 'content' => params[:content] } save_memos(memos) redirect '/memos' end diff --git a/memoapp/public/memos.json b/memoapp/public/memos.json deleted file mode 100644 index d12994c..0000000 --- a/memoapp/public/memos.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "1": { - "title": "", - "content": "" - }, - "2": { - "title": "a", - "content": "aaa" - } -} \ No newline at end of file