Skip to content

Commit

Permalink
feat(db): get all children of specified taxonomy
Browse files Browse the repository at this point in the history
So if you have taxonomy countries and other taxonomy cities, you can link them both by specifying
parent. So if you ask - get all cities from all countries, this is right method for you. Other
feature like you want to get population of city (UK -> London -> popu
  • Loading branch information
Domantas committed Feb 25, 2021
1 parent 346f279 commit d659ec8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/components/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,46 @@ export async function getTerms({
result: response.result,
};
}

export async function getChildrenOfTerms({
taxonomyName,
language,
pageNumber,
pageSize,
sort,
filter,
projection,
excludeCulture,
cluster,
}) {
const response = await server.loadJson(
`${Config.apiUrl}${Endpoints.PROJECT.DATABASE.TAXONOMY.TERM.GET_CHILDREN(
taxonomyName
)}`,
{
method: 'POST',
headers: {
'X-CM-ProjectId': Config.projectId,
'X-CM-Cluster': cluster,
Authorization: `Bearer ${Config.secretKey}`,
Accept: 'application/json',
'Content-Type': 'application/json',
'Accept-Language': language || 'en',
},
body: JSON.stringify({
pageSize: pageSize || Config.tablePageSize,
pageNumber: pageNumber || 0,
projection: objectOrStringToString(projection),
filter: objectOrStringToString(filter),
sort: objectOrStringToString(sort),
excludeCulture,
}),
}
);

if (!response) return null;
return {
totalCount: response.totalCount,
result: response.result,
};
}
2 changes: 2 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export const CONFIG = {
TERM: {
GET: (id) => `/v2/db/terms/${id}`,
GET_ALL: (taxonomyName) => `/v2/db/taxonomies/${taxonomyName}/terms`,
GET_CHILDREN: (taxonomyName) =>
`/db/taxonomies/${taxonomyName}/terms/children`,
},
SYSTEM: {
GET_TERMS: (taxonomyName) => `/v2/taxonomies/${taxonomyName}/terms`,
Expand Down
8 changes: 7 additions & 1 deletion tests/db/collections.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getRecords} from 'Components/db';
import {getRecords, getChildrenOfTerms} from 'Components/db';
import config from 'Config';

require('dotenv').config();
Expand All @@ -19,4 +19,10 @@ describe('db tests', () => {
const response = await getRecords(request);
expect(response.totalCount).toBe(2);
});

test('get all children of specified taxonomy', async () => {
const request = {taxonomyName: 'countries'};
const response = await getChildrenOfTerms(request);
expect(response.totalCount).toBe(3);
});
});

0 comments on commit d659ec8

Please sign in to comment.