Skip to content

Commit

Permalink
Add Top Ten endpoint and new releases
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhbhatia committed Dec 14, 2024
1 parent 1f0dd0e commit 00fc96f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/controllers/api/v1/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# frozen_string_literal: true

class Api::V1::ProductsController < ApplicationController
before_action :onboarded_products

def new_releases
onboarded_products = Product.published.where(store: Store.where(user: User.where.not(stripe_user_id: nil)))
@new_releases = onboarded_products.order(created_at: :desc).take(12)
end

def top_ten
@top_ten = onboarded_products.order("RANDOM()").take(10)
end

def onboarded_products
Product.published.where(store: Store.where(user: User.where.not(stripe_user_id: nil)))
end
end
10 changes: 10 additions & 0 deletions app/views/api/v1/products/top_ten.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

json.array! @top_ten do |product|
json.name product.name
json.store_name product.store.name
json.price product.price
json.slug product.slug
json.created_at product.created_at
json.issue_cover url_for(product.issue_cover)
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
namespace :api do
namespace :v1 do
get "new_releases", to: "products#new_releases", as: "new_releases"
get "top_ten", to: "products#top_ten", as: "top_ten"
end
end

Expand Down

0 comments on commit 00fc96f

Please sign in to comment.