I've read this question/answer How to call Loopback4 controller's method from another controller HOWEVER, my use case is slightly different.
Unlike that answer above, I DO need my API functions to be accessible via http (and I also couldn't get even get the answer in the link above to work).
My Use Case I need for my Main Controller to go and get all Locations via the iciService (which is all good and working) BUT then I need to insert all of that Locations data into another datasource (mongoDB), I believe by importing my Locations Controller, right? But how exactly is what I'm not sure on.
I've research this but I'm unclear. I'm not a strong programmer.
Here's my Main Controller code:
import { inject } from '@loopback/context';
import { iciService } from '../services/ici.service';
import { get, param } from '@loopback/rest';
export class iciController {
constructor(
@inject('services.ici')
private iciService: iciService
) {}
@get('/locations')
getall() {
return this.iciService.getLocations();
}
}
And here's the Locations controller that I need to link out to.
import {
Count,
CountSchema,
Filter,
FilterExcludingWhere,
repository,
Where,
} from '@loopback/repository';
import {
post,
param,
get,
getModelSchemaRef,
patch,
put,
del,
requestBody,
} from '@loopback/rest';
import {Location} from '../models';
import {LocationRepository} from '../repositories';
export class LocationController {
constructor(
@repository(LocationRepository)
public locationRepository : LocationRepository,
) {}
@post('/locations', {
responses: {
'200': {
description: 'Location model instance',
content: {'application/json': {schema: getModelSchemaRef(Location)}},
},
},
})
async create(
@requestBody({
content: {
'application/json': {
schema: getModelSchemaRef(Location, {
title: 'NewLocation',
}),
},
},
})
location: Location,
): Promise<Location> {
return this.locationRepository.create(location);
}
}