From 0a4dc11eaf043400aa5358a744a47dd21c2bdc4f Mon Sep 17 00:00:00 2001 From: JHipster Bot Date: Fri, 28 Feb 2020 20:20:03 -0700 Subject: [PATCH 1/2] Add service query method for Vue front-end filtering calls --- generators/client/files.js | 1 + .../webapp/app/shared/query/params.ts.ejs | 13 +++++++ .../webapp/app/entities/entity.service.ts.ejs | 10 ++++++ .../entity-management.service.spec.ts.ejs | 36 +++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 generators/client/templates/vue/src/main/webapp/app/shared/query/params.ts.ejs diff --git a/generators/client/files.js b/generators/client/files.js index 67277495..f9703b25 100644 --- a/generators/client/files.js +++ b/generators/client/files.js @@ -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', diff --git a/generators/client/templates/vue/src/main/webapp/app/shared/query/params.ts.ejs b/generators/client/templates/vue/src/main/webapp/app/shared/query/params.ts.ejs new file mode 100644 index 00000000..877e58a6 --- /dev/null +++ b/generators/client/templates/vue/src/main/webapp/app/shared/query/params.ts.ejs @@ -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 ''; +} diff --git a/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs b/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs index 46243599..08a22c05 100755 --- a/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs +++ b/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs @@ -1,4 +1,5 @@ import axios from 'axios'; +import buildQueryParams from '@/shared/query/params'; <% if (pagination !== 'no') { %> import buildPaginationQueryOpts from '@/shared/sort/sorts'; <% } %> @@ -39,6 +40,15 @@ export default class <%= entityAngularName %>Service { }).catch(err => { reject(err); }); }); } + + public query(queryParams?: any<% if (pagination !== 'no') { %>, paginationQuery?: any<% } %>) : Promise { + return new Promise((resolve, reject) => { + axios.get(`${baseApiUrl}?${buildQueryParams(queryParams)}`<% if (pagination !== 'no') { %> + `&${buildPaginationQueryOpts(paginationQuery)}` <% } %>).then(function (res) { + resolve(res); + }).catch(err => { reject(err); }); + }); + } + <%_ if (!readOnly) { _%> diff --git a/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs b/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs index 00373bfd..1bb61b74 100644 --- a/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs +++ b/generators/entity-client/templates/vue/src/test/javascript/spec/app/entities/entity-management.service.spec.ts.ejs @@ -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})); From 9eb502777200f2553c93150cd082f7bd6248867b Mon Sep 17 00:00:00 2001 From: JHipster Bot Date: Sun, 1 Mar 2020 09:40:19 -0700 Subject: [PATCH 2/2] Use data-type instead of any --- .../main/webapp/app/entities/entity.service.ts.ejs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs b/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs index 08a22c05..3d57cc48 100755 --- a/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs +++ b/generators/entity-client/templates/vue/src/main/webapp/app/entities/entity.service.ts.ejs @@ -33,17 +33,17 @@ export default class <%= entityAngularName %>Service { }); } - public retrieve(<% if (pagination !== 'no') { %>paginationQuery?: any<% } %>) : Promise { - return new Promise((resolve, reject) => { - axios.get(baseApiUrl<% if (pagination !== 'no') { %> + `?${buildPaginationQueryOpts(paginationQuery)}` <% } %>).then(function (res) { - resolve(res); + public query(queryParams?: any<% if (pagination !== 'no') { %>, paginationQuery?: any<% } %>) : Promise> { + return new Promise>((resolve, reject) => { + axios.get(`${baseApiUrl}?${buildQueryParams(queryParams)}`<% if (pagination !== 'no') { %> + `&${buildPaginationQueryOpts(paginationQuery)}` <% } %>).then(function (res) { + resolve(res.data); }).catch(err => { reject(err); }); }); } - public query(queryParams?: any<% if (pagination !== 'no') { %>, paginationQuery?: any<% } %>) : Promise { + public retrieve(<% if (pagination !== 'no') { %>paginationQuery?: any<% } %>) : Promise { return new Promise((resolve, reject) => { - axios.get(`${baseApiUrl}?${buildQueryParams(queryParams)}`<% if (pagination !== 'no') { %> + `&${buildPaginationQueryOpts(paginationQuery)}` <% } %>).then(function (res) { + axios.get(baseApiUrl<% if (pagination !== 'no') { %> + `?${buildPaginationQueryOpts(paginationQuery)}` <% } %>).then(function (res) { resolve(res); }).catch(err => { reject(err); }); });