From 5476cf38d2e27aea0265e2b6c47eefc2e10542ce Mon Sep 17 00:00:00 2001 From: JHipster Bot Date: Fri, 28 Feb 2020 20:20:03 -0700 Subject: [PATCH 1/4] 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 9ec91f4fc5b6e35825f7dd147b142beb99efd812 Mon Sep 17 00:00:00 2001 From: JHipster Bot Date: Sun, 1 Mar 2020 09:40:19 -0700 Subject: [PATCH 2/4] 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); }); }); From db3ed0c6740c72de7eb505c10c855cfc00907751 Mon Sep 17 00:00:00 2001 From: Kevin Whittington Date: Tue, 10 Mar 2020 12:38:06 -0600 Subject: [PATCH 3/4] Return array from query function --- .../vue/src/main/webapp/app/entities/entity.service.ts.ejs | 4 ++-- 1 file changed, 2 insertions(+), 2 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 3d57cc48..07b8bd52 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,8 +33,8 @@ export default class <%= entityAngularName %>Service { }); } - public query(queryParams?: any<% if (pagination !== 'no') { %>, paginationQuery?: any<% } %>) : Promise> { - return new Promise>((resolve, reject) => { + 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); }); From 0ebf2739bb6fc99c9ac7f367f587dcac97b2f217 Mon Sep 17 00:00:00 2001 From: JHipster Bot Date: Tue, 10 Mar 2020 22:14:12 -0600 Subject: [PATCH 4/4] Fix test and query string mixing --- .../main/webapp/app/shared/sort/sorts.ts.ejs | 3 +- .../webapp/app/entities/entity.service.ts.ejs | 6 ++- .../entity-management.service.spec.ts.ejs | 37 +++---------------- 3 files changed, 12 insertions(+), 34 deletions(-) diff --git a/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs b/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs index eac26a0d..d15529ae 100644 --- a/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs +++ b/generators/client/templates/vue/src/main/webapp/app/shared/sort/sorts.ts.ejs @@ -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 ''; } 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 07b8bd52..84ad998c 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 @@ -35,7 +35,11 @@ export default class <%= entityAngularName %>Service { 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) { + 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); }); }); 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 1bb61b74..3d0e5458 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 @@ -263,38 +263,11 @@ 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); + 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); }); });