Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Add service query method for Vue front-end filtering calls #569

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generators/client/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const vueFiles = {
'core/ribbon/ribbon.vue',
'core/ribbon/ribbon.component.ts',
'shared/date/filters.ts',
'shared/query/params.ts',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following the other comment, needs to add params.spec.ts too

'shared/sort/jhi-sort-indicator.component.ts',
'shared/sort/jhi-sort-indicator.vue',
'shared/sort/sorts.ts',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function buildQueryParams(queryParams) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be cool to add test for this file

if (Array.isArray(queryParams) && queryParams.length > 0) {
let paramString = '';
for (const param of queryParams) {
if (paramString.length > 0) {
paramString += '&';
}
paramString += param;
}
return `${paramString}`;
}
return '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default function buildPaginationQueryOpts(paginationQuery) {
}
sorts += 'sort=' + paginationQuery.sort[idx];
}
return `${sorts}&page=${paginationQuery.page}&size=${paginationQuery.size}`;
const pagination = `page=${paginationQuery.page}&size=${paginationQuery.size}`;
return sorts.length > 0 ? `${sorts}&${pagination}` : `${pagination}`;
}
return '';
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import buildQueryParams from '@/shared/query/params';
<% if (pagination !== 'no') { %>
import buildPaginationQueryOpts from '@/shared/sort/sorts';
<% } %>
Expand Down Expand Up @@ -32,13 +33,26 @@ export default class <%= entityAngularName %>Service {
});
}

public query(queryParams?: any<% if (pagination !== 'no') { %>, paginationQuery?: any<% } %>) : Promise<I<%= entityAngularName %>[]> {
return new Promise<I<%= entityAngularName %>[]>((resolve, reject) => {
let queryString = `${buildQueryParams(queryParams)}`<% if (pagination !== 'no') { %> + `${buildPaginationQueryOpts(paginationQuery)}` <% } %>
if (queryString.length > 0) {
queryString = `?${queryString}`
}
axios.get(`${baseApiUrl}${queryString}`).then(function (res) {
resolve(res.data);
}).catch(err => { reject(err); });
});
}

public retrieve(<% if (pagination !== 'no') { %>paginationQuery?: any<% } %>) : Promise<any> {
return new Promise<any>((resolve, reject) => {
axios.get(baseApiUrl<% if (pagination !== 'no') { %> + `?${buildPaginationQueryOpts(paginationQuery)}` <% } %>).then(function (res) {
resolve(res);
}).catch(err => { reject(err); });
});
}

<%_
if (!readOnly) { _%>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ describe('Service Tests', () => {
});
});

it('should return a filtered list of <%= entityAngularName %>', async () => {
const returnedFromService = Object.assign({}, elemDefault);
mockedAxios.get.mockReturnValue(Promise.resolve({ data: [returnedFromService] }));

return service.query([]<% if (pagination !== 'no') { %>, { sort: {}, page: 0, size: 10 }<% } %>).then((res) => {
expect(res).toContainEqual(elemDefault);
});
});

<%_ if (!readOnly) { _%>
it('should delete a <%= entityAngularName %>', async () => {
mockedAxios.delete.mockReturnValue(Promise.resolve({ok: true}));
Expand Down