filter maps by user_id in api (#872)

* filter maps by user_id in api

* test user_id map filter

* update starred maps example to make starred true lol

* add user id to map schema/examples
This commit is contained in:
Devin Howard 2016-10-29 22:07:27 +08:00 committed by GitHub
parent a32f98bde2
commit 1fbfd56d57
8 changed files with 72 additions and 8 deletions

View file

@ -5,6 +5,11 @@ module Api
def searchable_columns
[:name, :desc]
end
def apply_filters(collection)
collection = collection.where(user_id: params[:user_id]) if params[:user_id]
collection
end
end
end
end

View file

@ -141,6 +141,7 @@ module Api
collection = accessible_records
collection = yield collection if block_given?
collection = search_by_q(collection) if params[:q]
collection = apply_filters(collection)
collection = order_by_sort(collection) if params[:sort]
collection = collection.page(params[:page]).per(params[:per])
self.collection = collection
@ -167,6 +168,11 @@ module Api
collection.where(condition)
end
def apply_filters(collection)
# override this function for specific filters
collection
end
def order_by_sort(collection)
builder = collection
sorts = params[:sort].split(',')

View file

@ -2,6 +2,12 @@
get:
is: [ searchable: { searchFields: "name, desc" }, embeddable: { embedFields: "user,topics,synapses,mappings,contributors,collaborators" }, orderable, pageable ]
securedBy: [ null, token, oauth_2_0, cookie ]
queryParameters:
user_id:
description: |
Pass a user_id to only return maps created by that user. For example, `/api/v2/maps?user_id=1` would return maps created by the Metamaps user with id 1.
required: false
type: number
responses:
200:
body:
@ -91,7 +97,7 @@ post:
description: Created
body:
application/json:
example: !include ../examples/map.json
example: !include ../examples/map_starred.json
delete:
responses:
204:

View file

@ -8,6 +8,7 @@
"starred": false,
"created_at": "2016-03-26T08:02:05.379Z",
"updated_at": "2016-03-27T07:20:18.047Z",
"user_id": 1234,
"topic_ids": [
58,
59

View file

@ -0,0 +1,29 @@
{
"data": {
"id": 2,
"name": "Emergent Network Phenomena",
"desc": "Example map for the API",
"permission": "commons",
"screenshot": "https://s3.amazonaws.com/metamaps-assets/site/missing-map.png",
"starred": true,
"created_at": "2016-03-26T08:02:05.379Z",
"updated_at": "2016-03-27T07:20:18.047Z",
"user_id": 1234,
"topic_ids": [
58,
59
],
"synapse_ids": [
2
],
"mapping_ids": [
94,
95,
96
],
"collaborator_ids": [],
"contributor_ids": [
2
]
}
}

View file

@ -9,6 +9,7 @@
"starred": false,
"created_at": "2016-03-26T08:02:05.379Z",
"updated_at": "2016-03-27T07:20:18.047Z",
"user_id": 1234,
"topic_ids": [
58,
59

View file

@ -27,6 +27,9 @@
"updated_at": {
"$ref": "_datetimestamp.json"
},
"user_id": {
"$ref": "_id.json"
},
"topic_ids": {
"type": "array",
"items": {

View file

@ -1,4 +1,4 @@
# frozen_string_literal: true
#t frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'maps API', type: :request do
@ -6,13 +6,26 @@ RSpec.describe 'maps API', type: :request do
let(:token) { create(:token, user: user).token }
let(:map) { create(:map, user: user) }
it 'GET /api/v2/maps' do
create_list(:map, 5)
get '/api/v2/maps'
describe 'GET /api/v2/maps' do
it 'returns all maps' do
create_list(:map, 5)
get '/api/v2/maps'
expect(response).to have_http_status(:success)
expect(response).to match_json_schema(:maps)
expect(JSON.parse(response.body)['data'].count).to eq 5
expect(response).to have_http_status(:success)
expect(response).to match_json_schema(:maps)
expect(JSON.parse(response.body)['data'].count).to eq 5
end
it 'filters by user id' do
create(:map, user_id: 1)
create(:map, user_id: 2)
create(:map, user_id: 2, permission: :private)
get '/api/v2/maps', params: { user_id: 2 }
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)['data'].count).to eq 1
expect(JSON.parse(response.body)['data'][0]['user_id']).to eq 2
end
end
it 'GET /api/v2/maps/:id' do