Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.56 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. module V1
  4.   module Shops
  5.     module MobileApi
  6.       class GetDatasController < V1::ShopApiController
  7.         respond_to :json
  8.         before_action :set_company, :set_shop
  9.         around_action :company_shard
  10.         before_action :read_products,  only: :index
  11.  
  12.         def index
  13.           data_type = params[:data_type]
  14.           updated_at = params[:updated_at].blank? ? nil : params[:updated_at].to_datetime
  15.           case data_type
  16.           when "categories"
  17.             @datas = @current_shop.categories.order("created_at desc")
  18.           when "tags"
  19.             @datas = @current_shop.tags
  20.           when "taggings"
  21.             @datas = @current_shop.taggings
  22.           when "option_types"
  23.             @datas = @current_shop.option_types
  24.           when "option_type_values"
  25.             @datas = @current_shop.option_type_values
  26.           when "option_type_values"
  27.             @datas = @current_shop.option_type_values
  28.           when "prices"
  29.             @datas = current_user.prices.where(shop_id: @current_shop.id)
  30.           when "products"
  31.             @datas = @current_shop.products
  32.           when "product_barcodes"
  33.             @datas = @current_shop.product_barcodes
  34.           when "product_option_types"
  35.             @datas = @current_shop.product_option_types
  36.           when "product_option_type_values"
  37.             @datas = @current_shop.product_option_type_values
  38.           when "product_packs"
  39.             @datas = @current_shop.product_packs
  40.           when "product_vendor_codes"
  41.             @datas = @current_shop.product_vendor_codes
  42.           when "variants"
  43.             @datas = @current_shop.variants
  44.           when "variant_option_type_values"
  45.             @datas = @current_shop.variant_option_type_values
  46.           else
  47.             @datas = []
  48.           end
  49.  
  50.           @datas = @datas.where(["updated_at >= ?", updated_at]) unless updated_at.blank?
  51.           @datas = @datas.order("updated_at asc") unless updated_at.blank?
  52.  
  53.           @datas = @datas.page(params[:page] || 1)
  54.                          .per(params[:per_page] || 100)
  55.  
  56.           render json: @datas,
  57.                  each_serializer: MobileApiSerializer,
  58.                  root: "data",
  59.                  meta: {
  60.                   current_page: @datas.current_page,
  61.                   total_pages:  @datas.total_pages,
  62.                   limit_value:  @datas.limit_value,
  63.                   total_items:  @datas.total_count,
  64.                   last_update_time: Time.zone.now
  65.                  }
  66.         end
  67.       end
  68.     end
  69.   end
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement