# frozen_string_literal: true

class Api::V1::ShopController < Api::BaseController
  before_action :require_user!

  respond_to :json

  skip_before_action :set_cache_headers

  def index
    type = params[:type]
    if type == 'featured_products'
      body = Redis.current.get("gabstore:featuredproducts")
        
      if body.nil?
        Request.new(:get, "https://shop.dissenter.com/product/group/json").perform do |res|
          if res.code == 200
            body = res.body_with_limit
            Redis.current.set("gabstore:featuredproducts", body) 
            Redis.current.expire("gabstore:featuredproducts", 15.minutes.seconds)
          else
            body = nil
          end
        end
      end

      render json: body
    else
      raise GabSocial::NotPermittedError
    end
  end

end