Skip to content

Commit 8c6d1e9

Browse files
authored
Merge pull request #202 from gtt-project/fix/projects-api-geometry-and-layers
Fix projects api "include=geometry,layers" case issue
2 parents 414b5a9 + 2d8da55 commit 8c6d1e9

File tree

4 files changed

+63
-58
lines changed

4 files changed

+63
-58
lines changed

app/views/projects/show.api.rsb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ api.project do
99
api.is_public @project.is_public?
1010

1111
if @project.geom
12-
api.geojson @project.geojson
12+
api.geojson @project.geojson.to_json
1313
else
1414
api.geojson ""
1515
end

doc/api.md

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -78,57 +78,3 @@ http://localhost:3000/gtt/settings.json
7878
]
7979
}
8080
```
81-
82-
## Project Level API Endpoint
83-
84-
### Project settings API Endpoint
85-
86-
```text
87-
GET /projects/{project_identifier}.json
88-
```
89-
90-
Get project and selected GTT layer data of a specific project.
91-
92-
***Request***
93-
94-
```text
95-
http://localhost:3000/projects/1.json
96-
```
97-
98-
***Response***
99-
100-
```json
101-
{
102-
"project": {
103-
"id": 1,
104-
"name": "My Project ",
105-
"identifier": "my-project",
106-
"description": "",
107-
"homepage": "",
108-
"status": 1,
109-
"is_public": false,
110-
"geojson": "",
111-
"gttLayer": [
112-
{
113-
"id": 1,
114-
"name": "OSM",
115-
"type": "ol.source.OSM",
116-
"options": {
117-
"url": "https://tile.openstreetmap.jp/{z}/{x}/{y}.png",
118-
"custom": "17/34.74701/135.35740",
119-
"crossOrigin": null,
120-
"attributions": "<a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a>"
121-
},
122-
"created_at": "2022-06-29T09:07:12.574Z",
123-
"updated_at": "2022-07-05T07:33:09.237Z",
124-
"global": true,
125-
"default": true,
126-
"position": 0,
127-
"baselayer": true
128-
}
129-
],
130-
"created_on": "2022-06-29T09:08:38Z",
131-
"updated_on": "2022-06-29T09:08:38Z"
132-
}
133-
}
134-
```

lib/redmine_gtt/patches/projects_controller_patch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def index
1212
format.api {
1313
retrieve_project_query
1414
scope = project_scope
15-
@include_geometry = params[:include] == 'geometry'
15+
@include_geometry = include_in_api_response?('geometry')
1616
if @include_geometry || params[:contains].present?
1717
query = RedmineGtt::SpatialProjectsQuery.new(
1818
contains: params[:contains],

test/integration/projects_api_test.rb

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ class ProjectsApiTest < Redmine::IntegrationTest
167167
test 'GET /projects.xml with include=layers should return layers' do
168168
ts = RedmineGtt::Actions::CreateTileSource.(type: 'ol.source.OSM', name: 'default', default: true).tile_source
169169
@project.gtt_tile_sources = [ts]
170+
@subproject1.gtt_tile_sources = [ts]
170171
get '/projects.xml?include=layers'
171172
assert_response :success
172173
xml = xml_data
173174
assert projects = xml.xpath('/projects/project')
174175
assert layer = projects.xpath('layers/layer').first
176+
assert layer.present?
175177
assert_equal 'ol.source.OSM', layer['type']
176178
end
177179

@@ -183,12 +185,69 @@ class ProjectsApiTest < Redmine::IntegrationTest
183185
xml = xml_data
184186
assert project = xml.xpath('/project')
185187
assert layer = project.xpath('layers/layer').first
188+
assert layer.present?
189+
assert_equal 'ol.source.OSM', layer['type']
190+
end
191+
192+
test 'GET /projects.xml with include=geometry,layers should return both geojson and layers' do
193+
ts = RedmineGtt::Actions::CreateTileSource.(type: 'ol.source.OSM', name: 'default', default: true).tile_source
194+
@project.gtt_tile_sources = [ts]
195+
@subproject1.gtt_tile_sources = [ts]
196+
geo = {
197+
'type' => 'Feature',
198+
'geometry' => {
199+
'type' => 'Polygon',
200+
'coordinates' => [
201+
[[123.269691,9.305099], [123.279691,9.305099],[123.279691,9.405099],[123.269691,9.405099], [123.269691,9.305099]]
202+
]
203+
}
204+
}
205+
geojson = geo.to_json
206+
207+
@project.update_attribute :geojson, geojson
208+
@subproject1.update_attribute :geojson, geojson
209+
210+
get '/projects.xml?include=geometry,layers'
211+
assert_response :success
212+
xml = xml_data
213+
assert projects = xml.xpath('/projects/project')
214+
assert json = projects.xpath('geojson').first.text
215+
assert json.present?
216+
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
217+
assert layer = projects.xpath('layers/layer').first
218+
assert layer.present?
219+
assert_equal 'ol.source.OSM', layer['type']
220+
end
221+
222+
test 'GET /projects/1.xml with include=geometry,layers should return both geojson and layers' do
223+
ts = RedmineGtt::Actions::CreateTileSource.(type: 'ol.source.OSM', name: 'default', default: true).tile_source
224+
@project.gtt_tile_sources = [ts]
225+
geo = {
226+
'type' => 'Feature',
227+
'geometry' => {
228+
'type' => 'Polygon',
229+
'coordinates' => [
230+
[[123.269691,9.305099], [123.279691,9.305099],[123.279691,9.405099],[123.269691,9.405099], [123.269691,9.305099]]
231+
]
232+
}
233+
}
234+
geojson = geo.to_json
235+
236+
@project.update_attribute :geojson, geojson
237+
238+
get '/projects/1.xml?include=geometry,layers'
239+
assert_response :success
240+
xml = xml_data
241+
assert project = xml.xpath('/project')
242+
assert json = project.xpath('geojson').first.text
243+
assert json.present?
244+
assert_equal geo['geometry'], JSON.parse(json)['geometry'], json
245+
assert layer = project.xpath('layers/layer').first
246+
assert layer.present?
186247
assert_equal 'ol.source.OSM', layer['type']
187248
end
188249

189250
def xml_data
190251
Nokogiri::XML(@response.body)
191252
end
192253
end
193-
194-

0 commit comments

Comments
 (0)