<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_
  const instanceType = restClass;
  const instanceName = restInstance;
  const listOrFlux = reactive ? 'Flux' : 'List';
  const mapper = entityInstance + 'Mapper';
  const entityToDtoReference = mapper + '::' + 'toDto';
  const entityListToDtoListReference = mapper + '.' + 'toDto';
  // add by begcode start
  const pageName = ormTool !== 'mybatis' ? 'Page': 'org.springframework.data.domain.Page';
  const pageType = ormTool !== 'mybatis' ? 'Pageable': 'Pageable';
  const pageUtil = ormTool !== 'mybatis' ? 'PaginationUtil': 'IPageUtil';
  const pageGetContent = ormTool !== 'mybatis' ? 'getContent': 'getRecords';
  // add by begcode end
if (paginationNo) { %>
    public <% if (reactive) { %>Mono<<% } %>List<<%= instanceType %>><% if (reactive) { %>><% } %> search<%= entityClassPlural %>(@RequestParam("query") String query) {
        LOG.debug("REST request to search <%= entityClassPlural %> for query {}", query);<%- include('search_stream_template', {viaService: viaService, fromResource: true}); -%>
<% } else { %>
    public <% if (reactive) { %>Mono<<% } %>ResponseEntity<<%= listOrFlux %><<%= instanceType %>>><% if (reactive) { %>><% } %> search<%= entityClassPlural %>(@RequestParam("query") String query, @org.springdoc.core.annotations.ParameterObject Pageable pageable<% if (reactive) { %>, ServerHttpRequest request<% } %>) {
        LOG.debug("REST request to search for a page of <%= entityClassPlural %> for query {}", query);
  <%_ if (reactive) { _%>
        return <%= entityInstance %><%= viaService ?
                'Service.' + (searchEngineElasticsearch ? 'searchCount' : 'countAll') :
                (searchEngineElasticsearch ? 'Search' : '') + 'Repository.count' %>()
            .map(total -> new PageImpl<>(new ArrayList<>(), pageable, total))
            .map(page -> PaginationUtil.generatePaginationHttpHeaders(ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders()), page))
            .map(headers -> ResponseEntity.ok().headers(headers).body(<%= entityInstance %><%= viaService ? 'Service' : (searchEngineElasticsearch ? 'Search' : '') + 'Repository' %>.search(query, pageable)<% if (!viaService && dtoMapstruct) { %>.map(<%= entityToDtoReference %>)<% } %>));
  <%_ } else { _%>
        try {
    <%_ if (viaService) { _%>
            <%= pageName%><<%= instanceType %>> page = <%= entityInstance %>Service.search(query, pageable);<%# modify by begcode %>
    <%_ } else { _%>
            <%= ormTool === 'mybatis' ? 'org.springframework.data.domain.' : ''%>Page<<%= persistClass %>> page = <%= entityInstance %><% if (searchEngineElasticsearch) { %>Search<% } %>Repository.search(query, pageable);
    <%_ } _%>
            HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(<% if (reactive) { %>ForwardedHeaderUtils.adaptFromForwardedHeaders(request.getURI(), request.getHeaders())<% } else { %>ServletUriComponentsBuilder.fromCurrentRequest()<% } %>, page);
            return ResponseEntity.ok().headers(headers).body(<% if (!viaService && dtoMapstruct) { %><%= entityListToDtoListReference %>(<% } %>page.getContent()<% if (!viaService && dtoMapstruct) { %>)<% } %>);
        } catch (RuntimeException e) {
    <% if (searchEngineElasticsearch) { %>
            throw ElasticsearchExceptionMapper.mapException(e);
    <%_ } else { _%>
            throw new RuntimeException(e);
    <% } %>
        }
  <%_ } _%>
<% } -%>
    }
