<%#
 Copyright 2013-2019 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

      http://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.
-%>
package <%=packageName%>.web.rest;

<%_ const viaService = service !== 'no';
    let isUsingMapsId = false;
    let mapsIdAssoc;
    for (idx in relationships) {
        isUsingMapsId = relationships[idx].useJPADerivedIdentifier;
        if ( isUsingMapsId === true) {
            mapsIdAssoc = relationships[idx];
            break;
        }
        isUsingMapsId = false;
    } _%>
<%_ if (dto !== 'mapstruct' || service === 'no') { _%>
import <%=packageName%>.domain.<%= asEntity(entityClass) %>;
<%_ } _%>
<%_ if (service !== 'no') { _%>
import <%=packageName%>.service.<%= entityClass %>Service;
<%_ } else { _%>
import <%=packageName%>.repository.<%= entityClass %>Repository;
    <%_ if (isUsingMapsId === true) { _%>
import <%=packageName%>.repository.<%= mapsIdAssoc.otherEntityNameCapitalized %>Repository;
    <%_ } _%>
    <%_ if (reactiveRepositories) { _%>
import <%=packageName%>.repository.reactive.<%= entityClass %>ReactiveRepository;
    <%_ } _%>
    <%_ if (searchEngine === 'elasticsearch') { _%>
import <%=packageName%>.repository.search.<%= entityClass %>SearchRepository;
    <%_ } _%>
<%_ } _%>
<%_ if (saveUserSnapshot) { _%>
import <%=packageName%>.repository.UserRepository;
<%_ } _%>
import <%=packageName%>.web.rest.errors.BadRequestAlertException;
<%_ if (dto === 'mapstruct') { _%>
import <%=packageName%>.service.dto.<%= asDto(entityClass) %>;
<%_ if (service === 'no') { _%>
import <%=packageName%>.service.mapper.<%= entityClass %>Mapper;
<%_ } } _%>
<%_ if (jpaMetamodelFiltering) {  _%>
import <%=packageName%>.service.dto.<%= entityClass %>Criteria;
import <%=packageName%>.service.<%= entityClass %>QueryService;
<%_ } _%>

import io.github.jhipster.web.util.HeaderUtil;
<%_ if (pagination !== 'no') { _%>
import io.github.jhipster.web.util.PaginationUtil;
<%_ } _%>
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
<%_ if (pagination !== 'no') { _%>
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;
<%_ } _%>
<%_ if (reactiveRepositories) { _%>
import org.springframework.http.MediaType;
<%_ } _%>
import org.springframework.http.ResponseEntity; <%_ if (isUsingMapsId === true && !viaService) { %>
import org.springframework.transaction.annotation.Transactional; <% } %>
import org.springframework.web.bind.annotation.*;
<%_ if (reactiveRepositories) { _%>
import reactor.core.publisher.Flux;
<%_ } _%>

<%_ if (validation) { _%>
import javax.validation.Valid;
<%_ } _%>
import java.net.URI;
import java.net.URISyntaxException;
<%_ if (pagination === 'no' && dto === 'mapstruct' && !viaService && fieldsContainNoOwnerOneToOne === true) { _%>
import java.util.LinkedList;<% } %>
import java.util.List;<% if (isUsingMapsId === true) { %>
import java.util.Objects;<% } %>
import java.util.Optional;<% if (databaseType === 'cassandra') { %>
import java.util.UUID;<% } %><% if (!viaService && (searchEngine === 'elasticsearch' || fieldsContainNoOwnerOneToOne === true)) { %>
import java.util.stream.Collectors;<% } %><% if (searchEngine === 'elasticsearch' || fieldsContainNoOwnerOneToOne === true) { %>
import java.util.stream.StreamSupport;<% } %><% if (searchEngine === 'elasticsearch') { %>

import static org.elasticsearch.index.query.QueryBuilders.*;<% } %>

/**
 * REST controller for managing {@link <%= packageName %>.domain.<%= asEntity(entityClass) %>}.
 */
@RestController
@RequestMapping("/api")<% if (databaseType === 'sql' && isUsingMapsId === true && !viaService) { %>
@Transactional <%_ } %>
public class <%= entityClass %>Resource {

    private final Logger log = LoggerFactory.getLogger(<%= entityClass %>Resource.class);

    <%_ let entityName = entityInstance;
    if (clientRootFolder && !skipUiGrouping) {
        entityName = _.camelCase(`${clientRootFolder}${entityClass}`);
    } _%>
    private static final String ENTITY_NAME = "<%= entityName %>";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;
    <%_
    const instanceType = (dto === 'mapstruct') ? asDto(entityClass) : asEntity(entityClass);
    const instanceName = (dto === 'mapstruct') ? asDto(entityInstance) : asEntity(entityInstance);
    _%><%- include('../../common/inject_template', {viaService: viaService, constructorName: entityClass + 'Resource', queryService: jpaMetamodelFiltering, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: true}); -%>

    /**
     * {@code POST  /<%= entityApiUrl %>} : Create a new <%= entityInstance %>.
     *
     * @param <%= instanceName %> the <%= instanceName %> to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new <%= instanceName %>, or with status {@code 400 (Bad Request)} if the <%= entityInstance %> has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/<%= entityApiUrl %>")
    public ResponseEntity<<%= instanceType %>> create<%= entityClass %>(<% if (validation) { %>@Valid <% } %>@RequestBody <%= instanceType %> <%= instanceName %>) throws URISyntaxException {
        log.debug("REST request to save <%= entityClass %> : {}", <%= instanceName %>);
        if (<%= instanceName %>.getId() != null) {
            throw new BadRequestAlertException("A new <%= entityInstance %> cannot already have an ID", ENTITY_NAME, "idexists");
        }
        <%_ if (saveUserSnapshot) { %>
        if (<%= instanceName %>.getUser() != null) {
            // Save user in case it's new and only exists in gateway
            userRepository.save(<%= instanceName %>.getUser());
        }
        <%_ } _%>
        <%_ if (databaseType === 'cassandra') { _%>
        <%= instanceName %>.setId(UUID.randomUUID());
        <%_ } _%>
<%- include('../../common/save_template', {asEntity, asDto, viaService: viaService, returnDirectly: false, isUsingMapsId: isUsingMapsId, mapsIdAssoc: mapsIdAssoc, isController: true}); -%>
        return ResponseEntity.created(new URI("/api/<%= entityApiUrl %>/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /<%= entityApiUrl %>} : Updates an existing <%= entityInstance %>.
     *
     * @param <%= instanceName %> the <%= instanceName %> to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated <%= instanceName %>,
     * or with status {@code 400 (Bad Request)} if the <%= instanceName %> is not valid,
     * or with status {@code 500 (Internal Server Error)} if the <%= instanceName %> couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/<%= entityApiUrl %>")
    public ResponseEntity<<%= instanceType %>> update<%= entityClass %>(<% if (validation) { %>@Valid <% } %>@RequestBody <%= instanceType %> <%= instanceName %>) throws URISyntaxException {
        log.debug("REST request to update <%= entityClass %> : {}", <%= instanceName %>);
        if (<%= instanceName %>.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        <%_ if (saveUserSnapshot) { %>
        if (<%= instanceName %>.getUser() != null) {
            // Save user in case it's new and only exists in gateway
            userRepository.save(<%= instanceName %>.getUser());
        }
        <%_ } _%>
<%- include('../../common/save_template', {asEntity, asDto, viaService: viaService, returnDirectly: false, isUsingMapsId: false, mapsIdAssoc: mapsIdAssoc}); -%>
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, <%= instanceName %>.getId().toString()))
            .body(result);
    }

    /**
     * {@code GET  /<%= entityApiUrl %>} : get all the <%= entityInstancePlural %>.
     *<% if (pagination !== 'no') { %>
     * @param pageable the pagination information.<% } if (!jpaMetamodelFiltering && fieldsContainOwnerManyToMany) { %>
     * @param eagerload flag to eager load entities from relationships (This is applicable for many-to-many).<% } if (jpaMetamodelFiltering) { %>
     * @param criteria the criteria which the requested entities should match.<% } else if (fieldsContainNoOwnerOneToOne) { %>
     * @param filter the filter of the request.<% } %>
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of <%= entityInstancePlural %> in body.
     */
    @GetMapping("/<%= entityApiUrl %>")<%_ if (databaseType === 'sql' && isUsingMapsId === true && !viaService) { _%>

    @Transactional(readOnly = true) <%_ } _%>
<%- include('../../common/get_all_template', {asEntity, asDto, viaService}); -%>

    <%_ if (reactiveRepositories) { _%>
    /**
     * {@code GET  /<%= entityApiUrl %>} : get all the <%= entityInstancePlural %> as a stream.
     * @return the {@link Flux} of <%= entityInstancePlural %>.
     */
    @GetMapping(value = "/<%= entityApiUrl %>", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)<%_ if (databaseType === 'sql' && isUsingMapsId === true && !viaService) { _%>

    @Transactional(readOnly = true) <%_ } _%>
    public Flux<<%= instanceType %>> getAll<%= entityClassPlural %>AsStream() {
        log.debug("REST request to get all <%= entityClassPlural %> as a stream");
        <%_ if (viaService) { _%>
        return <%= entityInstance %>Service.findAllAsFlux();
        <%_ } else { _%>
        return <%= entityInstance %>ReactiveRepository.findAll();
        <%_ } _%>
    }

    <%_ } _%>
    /**
     * {@code GET  /<%= entityApiUrl %>/:id} : get the "id" <%= entityInstance %>.
     *
     * @param id the id of the <%= instanceName %> to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the <%= instanceName %>, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/<%= entityApiUrl %>/{id}")<%_ if (databaseType === 'sql' && isUsingMapsId === true && !viaService) { _%>

    @Transactional(readOnly = true) <%_ } %>
    public ResponseEntity<<%= instanceType %>> get<%= entityClass %>(@PathVariable <%= pkType %> id) {
        log.debug("REST request to get <%= entityClass %> : {}", id);<%- include('../../common/get_template', {asEntity, asDto, viaService, returnDirectly:false}); -%>
        return ResponseUtil.wrapOrNotFound(<%= instanceName %>);
    }

    /**
     * {@code DELETE  /<%= entityApiUrl %>/:id} : delete the "id" <%= entityInstance %>.
     *
     * @param id the id of the <%= instanceName %> to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/<%= entityApiUrl %>/{id}")
    public ResponseEntity<Void> delete<%= entityClass %>(@PathVariable <%= pkType %> id) {
        log.debug("REST request to delete <%= entityClass %> : {}", id);
<%- include('../../common/delete_template', {viaService: viaService}); -%>
        return ResponseEntity.noContent().headers(HeaderUtil.createEntityDeletionAlert(applicationName, <%= enableTranslation %>, ENTITY_NAME, id<% if (pkType !== 'String') { %>.toString()<% } %>)).build();
    }<% if (searchEngine === 'elasticsearch') { %>

    /**
     * {@code SEARCH  /_search/<%= entityApiUrl %>?query=:query} : search for the <%= entityInstance %> corresponding
     * to the query.
     *
     * @param query the query of the <%= entityInstance %> search.<% if (pagination !== 'no') { %>
     * @param pageable the pagination information.<% } %>
     * @return the result of the search.
     */
    @GetMapping("/_search/<%= entityApiUrl %>")<%- include('../../common/search_template', {asEntity, asDto, viaService}); -%><% } %>
}
