Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add placeholder on SQL and refactor due to advice from RuboCop #3

Merged
merged 1 commit into from Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions Gemfile
@@ -1,11 +1,9 @@
# frozen_string_literal: true

source "https://rubygems.org"
source 'https://rubygems.org'

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }

gem 'pg'
gem 'sinatra'
gem 'sinatra-reloader'
gem 'pg'


2 changes: 1 addition & 1 deletion app.rb
Expand Up @@ -24,7 +24,7 @@
post '/' do
@title = params['title']
@detail = params['detail']
memo = Memo.new(title: @title, detail: @detail)
memo = Memo.new(title: @title, detail: @detail)
@action = 'new'
@action_jp = '保存'
if memo.save
Expand Down
44 changes: 19 additions & 25 deletions memo.rb
@@ -1,10 +1,13 @@
# frozen_string_literal: true

require 'pg'

# Memo is a class like ActiveRecord in order to make it easy to manage Postgres DB
class Memo
attr_accessor :title, :detail

def self.connect
PG::connect(host: 'localhost', user: 'uni', dbname: 'sinatrasimplememo', port: 5432)
PG.connect(host: 'localhost', user: 'uni', dbname: 'sinatrasimplememo', port: 5432)
end

def self.titles
Expand All @@ -13,49 +16,40 @@ def self.titles
result = connection.exec(query)
result.map { |data| data['title'] }
end

def self.find_by_title(title)
title = Memo.simple_escape(title)
query = "SELECT * FROM memos WHERE title = '#{title}'"
Memo.connect.exec(query)[0]
query = 'SELECT * FROM memos WHERE title = $1'
Memo.connect.exec(query, [title])[0]
end

def self.update_by_title(old_title, title, detail)
old_title = Memo.simple_escape(old_title)
title = Memo.simple_escape(title)
detail = Memo.simple_escape(detail)
query = "UPDATE memos SET title = '#{title}', detail = '#{detail}' WHERE title = '#{old_title}'"
Memo.connect.exec(query)
query = 'UPDATE memos SET title = $1, detail = $2 WHERE title = $3'
Memo.connect.exec(query, [title, detail, old_title])
end

def self.destroy_by_title(title)
title = Memo.simple_escape(title)
query = "DELETE FROM memos WHERE title = '#{title}'"
Memo.connect.exec(query)
query = 'DELETE FROM memos WHERE title = $1'
Memo.connect.exec(query, [title])
end

def self.create(title: nil, detail: nil)
memo = Memo.new(title: title, detail: detail)
memo.save
end

def self.simple_escape(input)
input.gsub("'", "''")
# .gsub("/", "'/")
end

def initialize(title: nil, detail: nil)
@title = Memo.simple_escape(title)
@detail = Memo.simple_escape(detail)
@title = title
@detail = detail
end

def save
connection = Memo.connect
raise 'EmptyTitleError' if title.strip.chomp.empty?
raise 'InvalidTitleError' if title == 'new'
query = "INSERT INTO memos (title, detail) VALUES ('#{title}', '#{detail}')"
result = connection.exec(query)
rescue => e

query = 'INSERT INTO memos (title, detail) VALUES ($1, $2)'
connection.exec(query, [title, detail])
rescue EmptyTitleError, InvalidTitleError => e
puts e
nil
end
Expand Down