From 1fbfd56d572442e83da0655f603ade41fcc89815 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 29 Oct 2016 22:07:27 +0800 Subject: [PATCH] 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 --- app/controllers/api/v2/maps_controller.rb | 5 ++++ app/controllers/api/v2/restful_controller.rb | 6 ++++ doc/api/apis/maps.raml | 8 +++++- doc/api/examples/map.json | 1 + doc/api/examples/map_starred.json | 29 ++++++++++++++++++++ doc/api/examples/maps.json | 1 + doc/api/schemas/_map.json | 3 ++ spec/api/v2/maps_api_spec.rb | 27 +++++++++++++----- 8 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 doc/api/examples/map_starred.json diff --git a/app/controllers/api/v2/maps_controller.rb b/app/controllers/api/v2/maps_controller.rb index 0bcd9bee..e8aa3f05 100644 --- a/app/controllers/api/v2/maps_controller.rb +++ b/app/controllers/api/v2/maps_controller.rb @@ -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 diff --git a/app/controllers/api/v2/restful_controller.rb b/app/controllers/api/v2/restful_controller.rb index 00325ba4..08177ad7 100644 --- a/app/controllers/api/v2/restful_controller.rb +++ b/app/controllers/api/v2/restful_controller.rb @@ -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(',') diff --git a/doc/api/apis/maps.raml b/doc/api/apis/maps.raml index 03c4db2b..d3361a70 100644 --- a/doc/api/apis/maps.raml +++ b/doc/api/apis/maps.raml @@ -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: diff --git a/doc/api/examples/map.json b/doc/api/examples/map.json index 20e63204..78711649 100644 --- a/doc/api/examples/map.json +++ b/doc/api/examples/map.json @@ -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 diff --git a/doc/api/examples/map_starred.json b/doc/api/examples/map_starred.json new file mode 100644 index 00000000..4ac6c698 --- /dev/null +++ b/doc/api/examples/map_starred.json @@ -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 + ] + } +} diff --git a/doc/api/examples/maps.json b/doc/api/examples/maps.json index 501c0325..1e2a7baf 100644 --- a/doc/api/examples/maps.json +++ b/doc/api/examples/maps.json @@ -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 diff --git a/doc/api/schemas/_map.json b/doc/api/schemas/_map.json index 1234122d..76586914 100644 --- a/doc/api/schemas/_map.json +++ b/doc/api/schemas/_map.json @@ -27,6 +27,9 @@ "updated_at": { "$ref": "_datetimestamp.json" }, + "user_id": { + "$ref": "_id.json" + }, "topic_ids": { "type": "array", "items": { diff --git a/spec/api/v2/maps_api_spec.rb b/spec/api/v2/maps_api_spec.rb index ef72dec7..5f5e9b83 100644 --- a/spec/api/v2/maps_api_spec.rb +++ b/spec/api/v2/maps_api_spec.rb @@ -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