package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBNurseryDetailsService;
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException;
import vn.azteam.tpf.web.rest.util.HeaderUtil;
import vn.azteam.tpf.web.rest.util.PaginationUtil;
import vn.azteam.tpf.service.dto.TBNurseryDetailsDTO;
import vn.azteam.tpf.service.dto.TBNurseryDetailsCriteria;
import vn.azteam.tpf.service.TBNurseryDetailsQueryService;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing TBNurseryDetails.
 */
@RestController
@RequestMapping("/api")
public class TBNurseryDetailsResource {

    private final Logger log = LoggerFactory.getLogger(TBNurseryDetailsResource.class);

    private static final String ENTITY_NAME = "tBNurseryDetails";

    private final TBNurseryDetailsService tBNurseryDetailsService;

    private final TBNurseryDetailsQueryService tBNurseryDetailsQueryService;

    public TBNurseryDetailsResource(TBNurseryDetailsService tBNurseryDetailsService, TBNurseryDetailsQueryService tBNurseryDetailsQueryService) {
        this.tBNurseryDetailsService = tBNurseryDetailsService;
        this.tBNurseryDetailsQueryService = tBNurseryDetailsQueryService;
    }

    /**
     * POST  /tb-nursery-details : Create a new tBNurseryDetails.
     *
     * @param tBNurseryDetailsDTO the tBNurseryDetailsDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new tBNurseryDetailsDTO, or with status 400 (Bad Request) if the tBNurseryDetails has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/tb-nursery-details")
    @Timed
    public ResponseEntity<TBNurseryDetailsDTO> createTBNurseryDetails(@RequestBody TBNurseryDetailsDTO tBNurseryDetailsDTO) throws URISyntaxException {
        log.debug("REST request to save TBNurseryDetails : {}", tBNurseryDetailsDTO);
        if (tBNurseryDetailsDTO.getId() != null) {
            throw new BadRequestAlertException("A new tBNurseryDetails cannot already have an ID", ENTITY_NAME, "idexists");
        }
        TBNurseryDetailsDTO result = tBNurseryDetailsService.save(tBNurseryDetailsDTO);
        return ResponseEntity.created(new URI("/api/tb-nursery-details/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /tb-nursery-details : Updates an existing tBNurseryDetails.
     *
     * @param tBNurseryDetailsDTO the tBNurseryDetailsDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated tBNurseryDetailsDTO,
     * or with status 400 (Bad Request) if the tBNurseryDetailsDTO is not valid,
     * or with status 500 (Internal Server Error) if the tBNurseryDetailsDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/tb-nursery-details")
    @Timed
    public ResponseEntity<TBNurseryDetailsDTO> updateTBNurseryDetails(@RequestBody TBNurseryDetailsDTO tBNurseryDetailsDTO) throws URISyntaxException {
        log.debug("REST request to update TBNurseryDetails : {}", tBNurseryDetailsDTO);
        if (tBNurseryDetailsDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        TBNurseryDetailsDTO result = tBNurseryDetailsService.save(tBNurseryDetailsDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, tBNurseryDetailsDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /tb-nursery-details : get all the tBNurseryDetails.
     *
     * @param pageable the pagination information
     * @param criteria the criterias which the requested entities should match
     * @return the ResponseEntity with status 200 (OK) and the list of tBNurseryDetails in body
     */
    @GetMapping("/tb-nursery-details")
    @Timed
    public ResponseEntity<List<TBNurseryDetailsDTO>> getAllTBNurseryDetails(TBNurseryDetailsCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBNurseryDetails by criteria: {}", criteria);
        Page<TBNurseryDetailsDTO> page = tBNurseryDetailsQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-nursery-details");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-nursery-details/count : count all the tBNurseryDetails.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-nursery-details/count")
    @Timed
    public ResponseEntity<Long> countTBNurseryDetails(TBNurseryDetailsCriteria criteria) {
        log.debug("REST request to count TBNurseryDetails by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBNurseryDetailsQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-nursery-details/:id : get the "id" tBNurseryDetails.
     *
     * @param id the id of the tBNurseryDetailsDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBNurseryDetailsDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-nursery-details/{id}")
    @Timed
    public ResponseEntity<TBNurseryDetailsDTO> getTBNurseryDetails(@PathVariable Long id) {
        log.debug("REST request to get TBNurseryDetails : {}", id);
        Optional<TBNurseryDetailsDTO> tBNurseryDetailsDTO = tBNurseryDetailsService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBNurseryDetailsDTO);
    }

    /**
     * DELETE  /tb-nursery-details/:id : delete the "id" tBNurseryDetails.
     *
     * @param id the id of the tBNurseryDetailsDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-nursery-details/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBNurseryDetails(@PathVariable Long id) {
        log.debug("REST request to delete TBNurseryDetails : {}", id);
        tBNurseryDetailsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/tb-nursery-details?query=:query : search for the tBNurseryDetails corresponding
     * to the query.
     *
     * @param query the query of the tBNurseryDetails search
     * @param pageable the pagination information
     * @return the result of the search
     */
    @GetMapping("/_search/tb-nursery-details")
    @Timed
    public ResponseEntity<List<TBNurseryDetailsDTO>> searchTBNurseryDetails(@RequestParam String query, Pageable pageable) {
        log.debug("REST request to search for a page of TBNurseryDetails for query {}", query);
        Page<TBNurseryDetailsDTO> page = tBNurseryDetailsService.search(query, pageable);
        HttpHeaders headers = PaginationUtil.generateSearchPaginationHttpHeaders(query, page, "/api/_search/tb-nursery-details");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

}
