I have 2 models: employee and jobs Employee has many jobs and jobs belongs to employee
I have the CRUD endpoint for employee and jobs in my API
And finally I have the models employee to accept nested attributes from jobs so when a employee is created jobs can also be created
In my employees entity I have
module MyApi::V1::Entities
class EmployeeResponseEntity < Grape::Entity
expose :id
expose :name
expose :active
expose :one_time
expose :cycle
expose :jobs, using: MyApi::V1::Entities::JobResponseEntity
end
end
and here is the jobs
module MyApi::V1::Entities
class JobResponseEntity < Grape::Entity
expose :id, documentation: { type: 'integer', desc: 'ID' }
expose :date, documentation: { type: 'date', desc: 'Date' }
expose :start_time, documentation: { type: 'time', desc: 'Start Time' }
expose :end_time, documentation: { type: 'time', desc: 'End Time' }
expose :notes, documentation: { type: 'text', desc: 'Notes' }
expose :status, documentation: { type: 'string', desc: 'Status' }
expose :cancelled, documentation: { type: 'boolean', desc: 'Cancelled' }
expose :paid, documentation: { type: 'boolean', desc: 'Paid' }
end
end
I don't want to retrieve jobs that are cancelled and the start_date < Time.now()
Is this a model or entity logic?
Thanks in advance
This kind of restriction is always made by your model logic. There're lots of similar examples such as a School manager application. Suppose that your client asks your API to return the Class #1 and all students which grade is greater or equal than 6. How can you model it?
In my opinion, you should model it through the resource /class/1/students?min_grade=6. I don't think you should model it through the URI /class/1 and returning only the students with a specific grade. And in this scenario, this filtering must be made by your model rather than your entity.
Applying this same idea to your problem, you should model an URI like /employee/1/jobs?start_date=12/12/2014&status=active and this filtering must be made by your model and not your entity.