autoforce / autoforce/APIcasso

Problems with nested resources and custom queries

Abierto
#89 1 comentario 0 reacciones 1 asignado Reclamado por @ErvalhouS Ver en GitHub
question
Lenguaje dominante
Ruby
Estrellas
31
Forks
1
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.