Skip to content
Snippets Groups Projects
Commit 7c3f6d05 authored by ALGLAVE Ivan's avatar ALGLAVE Ivan
Browse files

retrieve multiple people from an id array with a single call

parent bbc91228
No related branches found
No related tags found
No related merge requests found
...@@ -54,6 +54,15 @@ export class PeopleDao { ...@@ -54,6 +54,15 @@ export class PeopleDao {
}); });
}); });
findManyById = (idList: string[]): Promise<People[]> =>
new Promise((resolve, reject) => {
this._peopleModel.find({ _id: { $in: idList } }, {}, {}, (err, value) => {
if (err) reject(err.message);
if (!value) resolve([]);
resolve(value);
});
});
findById = (id: string): Promise<People | void> => findById = (id: string): Promise<People | void> =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this._peopleModel.findById(id, (err, value) => { this._peopleModel.findById(id, (err, value) => {
......
...@@ -24,9 +24,24 @@ export class PeopleController { ...@@ -24,9 +24,24 @@ export class PeopleController {
//@UseGuards(AuthGuard('jwt')) //@UseGuards(AuthGuard('jwt'))
@Get() @Get()
findAll(): Promise<PeopleEntity[] | void> { findAll(): Promise<PeopleEntity[] | void> {
console.log(
encodeURIComponent(
JSON.stringify([
'640703a32ece22f629e3435f',
'640705340ca60d0b09ba2b88',
'640706a80ca60d0b09ba2cd2',
]),
),
);
return this._peopleService.findAll(); return this._peopleService.findAll();
} }
@Get('/many/:list')
findMany(@Param() params: { list: string }): Promise<PeopleEntity[] | void> {
const idList = JSON.parse(params.list);
return this._peopleService.findMany(idList);
}
@UseGuards(AuthGuard('jwt')) @UseGuards(AuthGuard('jwt'))
@Get(':id') @Get(':id')
findOne(@Param() params: { id: string }): Promise<PeopleEntity | void> { findOne(@Param() params: { id: string }): Promise<PeopleEntity | void> {
......
...@@ -13,6 +13,9 @@ export class PeopleService { ...@@ -13,6 +13,9 @@ export class PeopleService {
findAll = (): Promise<PeopleEntity[] | void> => this._peopleDao.find(); findAll = (): Promise<PeopleEntity[] | void> => this._peopleDao.find();
findMany = (idList: string[]): Promise<PeopleEntity[] | void> =>
this._peopleDao.findManyById(idList);
findOne = (id: string): Promise<PeopleEntity | void> => findOne = (id: string): Promise<PeopleEntity | void> =>
this._peopleDao.findById(id); this._peopleDao.findById(id);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment