Skip to content
Open
Show file tree
Hide file tree
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
34 changes: 33 additions & 1 deletion app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ProductsController < ApplicationController
before_action :set_product, only: %i[show edit update destroy]
before_action :create_session, only: %i[create update destroy]
before_action :create_session, only: %i[create update destroy search]

def index
@products = Product.all
Expand Down Expand Up @@ -45,6 +45,38 @@ def destroy
redirect_to products_path, notice: 'The product was successfully destroyed.'
end

def search
url = URI.parse('https://rubylab2.myshopify.com/admin/api/2023-04/graphql.json')
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url.path)
request['Content-Type'] = 'application/json'
request['X-Shopify-Access-Token'] = cookies[:shopify_app_session]
request.body = {
query: <<~GRAPHQL,
query($searchTerm: String) {
products(first: 10, query: $searchTerm) {
edges {
node {
id
title
}
}
}
}
GRAPHQL
variables: { searchTerm: params[:query] }
}.to_json

response = http.request(request)
result = JSON.parse(response.body)
@products = result.dig("data", "products", "edges").to_a
@our_products = @products.map do |product|
Product.find_by(shopify_id: product.dig("node", "id").split("/").last)
end
end

private

def set_product
Expand Down
8 changes: 7 additions & 1 deletion app/views/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h1>All Products</h1>
<h1>Search Products</h1>

<%= form_tag search_path, method: :get do %>
<%= text_field_tag :query, params[:query], placeholder: "Enter product name" %>
<%= submit_tag "Search" %>
<% end %>

<h1>All Products</h1>
<table>
<tr>
<th>Title</th>
Expand Down
19 changes: 19 additions & 0 deletions app/views/products/search.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1>Found Products</h1>
<% if @our_products.empty? %>
<p>Oops, looks like nothing is here.</p>
<% else %>
<table>
<tr>
<th>Title</th>
<th>Edit product</th>
<th>Delete product</th>
</tr>
<% @our_products.each do |product| %>
<tr>
<td><%= link_to product.title, product_path(product) %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= button_to 'Destroy', product_path(product), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
get '/auth/callback', to: 'shopify_auth#callback'
get '/import_shopify_data', to: 'shopify_import#import'
post '/webhooks/shopify/product_updated', to: 'webhooks#product_updated'
get '/search', to: 'products#search'
resources :orders do
member do
post :cancel
Expand Down