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

Commit

Permalink
Add service query method for Vue front-end filtering calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jhipster-bot authored and Kevin Whittington committed Mar 10, 2020
1 parent 6dd274c commit 5476cf3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
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',
'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) {
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
@@ -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 @@ -39,6 +40,15 @@ export default class <%= entityAngularName %>Service {
}).catch(err => { reject(err); });
});
}

public query(queryParams?: any<% if (pagination !== 'no') { %>, paginationQuery?: any<% } %>) : Promise<any> {
return new Promise<any>((resolve, reject) => {
axios.get(`${baseApiUrl}?${buildQueryParams(queryParams)}`<% 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,42 @@ describe('Service Tests', () => {
});
});

it('should return a filtered list of <%= entityAngularName %>', async () => {
const returnedFromService = Object.assign({
<%_ fields.forEach((field) => {
const fieldType = field.fieldType; _%>
<%_ if (fieldType === 'Boolean') { _%>
<%= field.fieldName%>: true,
<%_ } else if (['Integer', 'Long', 'Float', 'Double', 'BigDecimal', 'Duration'].includes(fieldType)) { _%>
<%= field.fieldName%>: 1,
<%_ } else if (fieldType === 'String' || fieldType === 'UUID') { _%>
<%= field.fieldName%>: 'BBBBBB',
<%_ } else if (['LocalDate'].includes(fieldType)) { _%>
<%= field.fieldName%>: format(currentDate, DATE_FORMAT),
<%_ } else if (['Instant', 'ZonedDateTime'].includes(fieldType)) { _%>
<%= field.fieldName%>: format(currentDate, DATE_TIME_FORMAT),
<%_ } else { // (fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent === 'any' || (fieldType === 'byte[]' || fieldType === 'ByteBuffer') && fieldTypeBlobContent === 'image' || fieldType === 'LocalDate' _%>
<%= field.fieldName%>: 'BBBBBB',
<%_ } _%>
<%_ }) _%>
}, elemDefault);
const queryParams = [];
const expected = Object.assign({
<%_ fields.forEach((field) => {
const fieldType = field.fieldType; _%>
<%_ if (['LocalDate'].includes(fieldType)) { _%>
<%= field.fieldName%>: currentDate,
<%_ } else if (['Instant', 'ZonedDateTime'].includes(fieldType)) { _%>
<%= field.fieldName%>: currentDate,
<%_ } _%>
<%_ }) _%>
}, returnedFromService);
mockedAxios.get.mockReturnValue(Promise.resolve([returnedFromService]));
return service.query(queryParams<% if (pagination !== 'no') { %>, { sort: {}, page: 0, size: 10 }<% } %>).then((res) => {
expect(res).toContainEqual(expected);
});
});

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

0 comments on commit 5476cf3

Please sign in to comment.