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

prevent join at /users/:user_id using summary table and de-normalization #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions webapp/ruby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ def update_last_login(user_id)
end

def buy_product(product_id, user_id)
db.xquery('INSERT INTO histories (product_id, user_id, created_at) VALUES (?, ?, UTC_TIMESTAMP())', \
product_id, user_id)
product = db.xquery('SELECT * FROM products WHERE id = ?', params[:product_id]).first

db.xquery('INSERT INTO histories (product_id, user_id, created_at, name, description, image_path, price) VALUES (?, ?, UTC_TIMESTAMP(), ?, ?, ?, ?)', \
product_id, user_id,
product[:name], product[:description], product[:image_path], product[:price])
end

def already_bought?(product_id)
Expand Down Expand Up @@ -124,12 +127,10 @@ def create_comment(product_id, user_id, content)

get '/users/:user_id' do
products_query = <<SQL
SELECT p.id, p.name, p.description, p.image_path, p.price, h.created_at
FROM histories as h
LEFT OUTER JOIN products as p
ON h.product_id = p.id
WHERE h.user_id = ?
ORDER BY h.id DESC
SELECT product_id as id, name, description, image_path, price, created_at
FROM histories
WHERE user_id = ?
ORDER BY id DESC
SQL
products = db.xquery(products_query, params[:user_id])

Expand Down