diff --git a/memoapp/README.md b/memoapp/README.md index e267617..aeb1ef6 100644 --- a/memoapp/README.md +++ b/memoapp/README.md @@ -1,9 +1,9 @@ ## Usage -`git clone https://github.com/riq0h/memoapp.git` - -`cd memoapp` - -`bundle install` - -`ruby app.rb` +``` +git clone https://github.com/riq0h/memoapp.git +cd memoapp +bundle install +psql -U postgres -f memos.sql +ruby app.rb +``` diff --git a/memoapp/app.rb b/memoapp/app.rb index 34977c7..f713f3e 100644 --- a/memoapp/app.rb +++ b/memoapp/app.rb @@ -2,29 +2,46 @@ require 'sinatra' require 'sinatra/reloader' -require 'json' -require 'securerandom' +require 'pg' require 'cgi' -FILE_PATH = 'memos.json' +configure do + set :conn, PG.connect(dbname: 'memo_app') +end helpers do def h(text) CGI.escapeHTML(text.to_s) end -end -def load_memos - if !File.zero?(FILE_PATH) - JSON.parse(File.read(FILE_PATH)) - else - {} + def db + settings.conn end -end -def save_memos(memos) - File.open(FILE_PATH, 'w') do |file| - file.write(JSON.generate(memos)) + def find_memo(id) + db.exec_params('SELECT * FROM memos WHERE id = $1', [id]).first + end + + def create_memo(title, content) + db.exec_params( + 'INSERT INTO memos (title, content) VALUES ($1, $2) RETURNING id', + [title, content] + ).first['id'] + end + + def update_memo(id, title, content) + db.exec_params( + 'UPDATE memos SET title = $1, content = $2 WHERE id = $3', + [title, content, id] + ) + end + + def delete_memo(id) + db.exec_params('DELETE FROM memos WHERE id = $1', [id]) + end + + def all_memos + db.exec('SELECT * FROM memos ORDER BY created_at DESC').to_a end end @@ -33,7 +50,7 @@ get '/' do end get '/memos' do - @memos = load_memos + @memos = all_memos erb :index end @@ -42,37 +59,29 @@ get '/memos/new' do end post '/memos' do - memos = load_memos - id = SecureRandom.uuid - memos[id] = { 'title' => params[:title], 'content' => params[:content] } - save_memos(memos) - redirect '/memos' + id = create_memo(params[:title], params[:content]) + redirect "/memos/#{id}" end get '/memos/:id' do - @memo = load_memos[params[:id]] + @memo = find_memo(params[:id]) halt 404, erb(:not_found) unless @memo erb :show end get '/memos/:id/edit' do - @memo = load_memos[params[:id]] + @memo = find_memo(params[:id]) halt 404, erb(:not_found) unless @memo erb :edit end patch '/memos/:id' do - memos = load_memos - halt 404, erb(:not_found) unless memos[params[:id]] - memos[params[:id]] = { 'title' => params[:title], 'content' => params[:content] } - save_memos(memos) + update_memo(params[:id], params[:title], params[:content]) redirect "/memos/#{params[:id]}" end delete '/memos/:id' do - memos = load_memos - halt 404, erb(:not_found) unless memos.delete(params[:id]) - save_memos(memos) + delete_memo(params[:id]) redirect '/memos' end diff --git a/memoapp/memos.sql b/memoapp/memos.sql new file mode 100644 index 0000000..f712772 --- /dev/null +++ b/memoapp/memos.sql @@ -0,0 +1,9 @@ +CREATE DATABASE memo_app; +DROP TABLE IF EXISTS memos; +\c memo_app +CREATE TABLE memos ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + title VARCHAR(255) NOT NULL, + content TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); diff --git a/memoapp/public/styles.css b/memoapp/public/styles.css index 659d156..dcd1c7c 100644 --- a/memoapp/public/styles.css +++ b/memoapp/public/styles.css @@ -1,3 +1,10 @@ +:root { + --save-color: #007bff; + --edit-color: #28a745; + --delete-color: #dc3545; + --back-color: #6c757d; +} + body { font-family: Arial, sans-serif; background-color: #f0f0f0; @@ -35,7 +42,7 @@ nav { nav a { margin: 0 10px; text-decoration: none; - color: #007bff; + color: var(--save-color); } nav a:hover { @@ -59,44 +66,42 @@ form textarea { border-radius: 4px; } -form button { +.button { padding: 10px; color: #fff; border: none; border-radius: 4px; cursor: pointer; + text-decoration: none; + text-align: center; + display: inline-block; + width: 100%; } -form button.save { - background-color: #007bff; +.button:hover { + opacity: 0.9; } -form button.save:hover { - background-color: #0056b3; +.button.save { + background-color: var(--save-color); } -form button.delete { - background-color: #dc3545; +.button.delete { + background-color: var(--delete-color); + padding: 12px; + font-size: 90%; } -form button.delete:hover { - background-color: #c82333; +.button.edit { + background-color: var(--edit-color); } -form button.edit { - background-color: #28a745; -} - -form button.edit:hover { - background-color: #218838; -} - -form button.back { - background-color: #6c757d; -} - -form button.back:hover { - background-color: #5a6268; +.button.back { + background-color: var(--back-color); + margin-top: 10px; + display: block; + margin: 10px auto 0; + box-sizing: border-box; } .memo-list { @@ -110,7 +115,7 @@ form button.back:hover { .memo-list a { text-decoration: none; - color: #007bff; + color: var(--save-color); } .memo-list a:hover { @@ -123,27 +128,18 @@ form button.back:hover { margin-bottom: 10px; } -.memo-actions form { - display: inline; +.memo-actions form, +.memo-actions a { + flex: 1; + margin: 0 5px; } -.memo-actions button { - padding: 10px; - color: #fff; - border: none; - border-radius: 4px; - cursor: pointer; +.memo-actions form:first-child, +.memo-actions a:first-child { + margin-left: 0; } -.memo-actions .edit { - background-color: #28a745; +.memo-actions form:last-child, +.memo-actions a:last-child { + margin-right: 0; } - -.memo-actions .edit:hover { - background-color: #218838; -} - -.memo-actions .back { - background-color: #6c757d; -} - diff --git a/memoapp/views/edit.erb b/memoapp/views/edit.erb index 5f940d7..e335ad3 100644 --- a/memoapp/views/edit.erb +++ b/memoapp/views/edit.erb @@ -1,8 +1,8 @@ -
+ - +
diff --git a/memoapp/views/index.erb b/memoapp/views/index.erb index ee2eee6..c24bd92 100644 --- a/memoapp/views/index.erb +++ b/memoapp/views/index.erb @@ -1,5 +1,5 @@ diff --git a/memoapp/views/new.erb b/memoapp/views/new.erb index 125b083..749a689 100644 --- a/memoapp/views/new.erb +++ b/memoapp/views/new.erb @@ -3,5 +3,5 @@ - + diff --git a/memoapp/views/not_found.erb b/memoapp/views/not_found.erb index 5bb9ea4..4a1a28d 100644 --- a/memoapp/views/not_found.erb +++ b/memoapp/views/not_found.erb @@ -1,3 +1,3 @@ -

404 Not Found

+

404 Not Found

お探しのページは見つかりませんでした。

トップページに戻る diff --git a/memoapp/views/show.erb b/memoapp/views/show.erb index e3bf232..be297f4 100644 --- a/memoapp/views/show.erb +++ b/memoapp/views/show.erb @@ -1,14 +1,10 @@

<%= h(@memo['title']) %>

<%= h(@memo['content']) %>

-
- -
-
+ 編集 + - +
-
- -
+戻る