autoforce / autoforce/APIcasso

Problems with nested resources and custom queries

Đang mở
#89 1 bình luận 0 reaction 1 người được giao Được @ErvalhouS nhận Xem trên GitHub
question
Ngôn ngữ chính
Ruby
Star
31
Fork
1
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

I've been performing queries from the endpoint without `nested_resources` and it worked fine, but when I've tried to perform them with `nested_resources` I couldn't make it work.
I got this error:

```
NoMethodError in Apicasso::CrudController#nested_index

undefined method `incidence_trackings' for # Did you mean? incidence_tracking incidence_tracking= incidence_tracking_ids incidence_tracking_ids=
```

This is the endpoint that I tested:

`http://localhost:3000/api/v1/incidences/1/incidence_trackings`

I need to perform a custom query so the API returns the expected results; I've been reading your documentation and there is a section called "_Extending base API actions_", which I've tried and couldn't make it work. In this case that didn't work I got the **BadRequest error (Bad hacker, stop bullying or I'll tell your mom!).**

It seems that I need an id and a `nested_resource`.

I've tried using any id from my resource, even if I don't need it, and it doesn't work either.
This is the endpoint that I tested:

`http://localhost:3000/api/v1/incidences/1/custom`

```
Processing by Apicasso::CrudController#nested_index as */*
Parameters: {"resource"=>"incidences", "id"=>"2", "nested"=>"custom"}
Completed 400 Bad Request in 1ms (ActiveRecord: 0.0ms)
```

Can I make an endpoint which doesn't require an id with a custom query.

This is my code:

**routes.rb**

```
Rails.application.routes.draw do

match '/:resource/:id/custom' => 'apicustom#custom', via: :get
mount Apicasso::Engine, at: "/api/v1"

end
```

**api_controller.rb**

```
class ApiController < ApplicationController
Apicasso::Key.create(scope:
{ read:
{
incidence: true,
phone_identifier: true,
interest_point: true,
incidence_type: true,
incidence_tracking: true
},
create:
{
incidence: true,
phone_identifier: true
}
})
end
```
**apicustom_controller.rb**

```
class ApicustomController < Apicasso::CrudController

def custom
render json: Incidence.find_by_sql("SELECT inc.id, inc.image_url, inc.description, it.status
FROM incidences inc, (SELECT id, status, date, incidence_id
FROM incidence_trackings it1
WHERE date = (SELECT MAX(date)
FROM incidence_trackings it2
WHERE it1.incidence_id = it2.incidence_id)) it
WHERE inc.id = it.incidence_id;")

end

end
```

Thanks for your time.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.