Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CurrencyQuery
- def self.all(search_prefix = nil)
- %(
- PREFIX dc: <#{PrefixesService::PREFIXES[:dc]}>
- PREFIX skos: <#{PrefixesService::PREFIXES[:skos]}>
- PREFIX sali: <#{PrefixesService::PREFIXES[:sali]}>
- SELECT ?currency ?prefix ?label (REPLACE(?definition, "Locations used: ", "") AS ?cleanedDefinition) ?totalCount
- WHERE {
- {
- SELECT (COUNT(?currency) AS ?totalCount)
- WHERE {
- ?currency rdfs:subClassOf sali:R767niCLQVC5zIcO5WDQMSl .
- ?currency dc:identifier ?prefix .
- ?currency rdfs:label ?label .
- ?currency skos:definition ?definition .
- }
- }
- ?currency rdfs:subClassOf sali:R767niCLQVC5zIcO5WDQMSl .
- ?currency dc:identifier ?prefix .
- ?currency rdfs:label ?label .
- ?currency skos:definition ?definition .
- #{'FILTER(CONTAINS(?prefix, "' + search_prefix.upcase + '"))' if search_prefix.present?}
- }
- ORDER BY ?prefix
- )
- end
- end
- def self.all_currencies(search_prefix = nil, page:, per_page:)
- sparql_query = CurrencyQuery.all(search_prefix)
- sparql_query += "LIMIT #{per_page} OFFSET #{page}"
- result = GraphDBConnector.instance.query(sparql_query)
- return nil unless result[:success]
- return [] unless result[:data].any?
- [result[:data].first[:totalCount], result[:data]]
- end
- class PaginationService
- DEFAULT_PAGE = 1
- PER_PAGE = 20
- MAX_PER_PAGE = 100
- def initialize(params)
- @page = params[:page].to_i.abs.positive? ? params[:page].to_i.abs : DEFAULT_PAGE
- @per_page = validate_per_page(params[:per_page])
- end
- def call
- meta = {
- page: @page,
- per_page: @per_page,
- total_elements: nil
- }
- [@per_page, @page, meta]
- end
- private
- def validate_per_page(per_page)
- per_page = per_page.to_i.abs
- per_page = per_page.positive? ? per_page : PER_PAGE
- per_page = MAX_PER_PAGE if per_page > MAX_PER_PAGE
- per_page
- end
- end
- def index
- per_page, page, meta = PaginationService.new(page: index_params[:page], per_page: index_params[:per_page]).call
- total_elements, records = Amount.all_currencies(index_params[:prefix_search], page: page, per_page: per_page)
- meta[:total_elements] = total_elements.value.to_i
- render json: CurrencySerializer.new(records: records, root_key: :data, meta: meta).serialized_json, status: :ok
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement