autoforce / autoforce/APIcasso

Problems with nested resources and custom queries

Open
#89 1 comment 0 reactions 1 assignee Claimed by @ErvalhouS View on GitHub
question
Dominant language
Ruby
Stars
31
Forks
1
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.