0

I have created a get api in which i want to apply search through name in query param. that is if we don't enter query param in same api it displays full list and if we enter any query param then it will show that result only. How can i do it??? Here is my loopback4 code

         @get('/zones', {
responses: {
  '200': {
    description: 'Array of Zone model instances',
    content: {
      'application/json': {
        schema: {
          type: 'array',
          items: getModelSchemaRef(Zone, { includeRelations: true }),
        },
      },
    },
  },
},
})
async find(
 //@param.path.string('name') name: String,
@param.query.object('filter', getFilterSchemaFor(Zone)) filter?: Filter<Zone>,
): Promise<Zone[]> {
return this.zoneRepository.find({ include: [{ relation: 'cameras' }] });
 }
neetz
  • 21
  • 1
  • 6

1 Answers1

0

If I understood correctly, you want to use add custom logic that checks the ?name= URL parameter.

To do so, we can write the following:

@get('/zones', {
  responses: {
    '200': {
      description: 'Array of Zone model instances',
      content: {
        'application/json': {
          schema: {
            type: 'array',
            items: getModelSchemaRef(Zone, { includeRelations: true }),
          },
        },
    },
  },
},
})
async find(
 @param.path.string('name') name: string,
): Promise<Zone[]> {
  return this.zoneRepository.find({
    where: {name: name},
    include: [{ relation: 'cameras' }]
  });
}

This would inject the name parameter into the function and then put it as part of the ORM query.

Rifa Achrinza
  • 1,555
  • 9
  • 19