package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBGuidelineDetailsObjectUpdateService;
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.TBGuidelineDetailsObjectUpdateDTO;
import vn.azteam.tpf.service.dto.TBGuidelineDetailsObjectUpdateCriteria;
import vn.azteam.tpf.service.TBGuidelineDetailsObjectUpdateQueryService;
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 TBGuidelineDetailsObjectUpdate.
 */
@RestController
@RequestMapping("/api")
public class TBGuidelineDetailsObjectUpdateResource {

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

    private static final String ENTITY_NAME = "tBGuidelineDetailsObjectUpdate";

    private final TBGuidelineDetailsObjectUpdateService tBGuidelineDetailsObjectUpdateService;

    private final TBGuidelineDetailsObjectUpdateQueryService tBGuidelineDetailsObjectUpdateQueryService;

    public TBGuidelineDetailsObjectUpdateResource(TBGuidelineDetailsObjectUpdateService tBGuidelineDetailsObjectUpdateService, TBGuidelineDetailsObjectUpdateQueryService tBGuidelineDetailsObjectUpdateQueryService) {
        this.tBGuidelineDetailsObjectUpdateService = tBGuidelineDetailsObjectUpdateService;
        this.tBGuidelineDetailsObjectUpdateQueryService = tBGuidelineDetailsObjectUpdateQueryService;
    }

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

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

    /**
     * GET  /tb-guideline-details-object-updates : get all the tBGuidelineDetailsObjectUpdates.
     *
     * @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 tBGuidelineDetailsObjectUpdates in body
     */
    @GetMapping("/tb-guideline-details-object-updates")
    @Timed
    public ResponseEntity<List<TBGuidelineDetailsObjectUpdateDTO>> getAllTBGuidelineDetailsObjectUpdates(TBGuidelineDetailsObjectUpdateCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBGuidelineDetailsObjectUpdates by criteria: {}", criteria);
        Page<TBGuidelineDetailsObjectUpdateDTO> page = tBGuidelineDetailsObjectUpdateQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-guideline-details-object-updates");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-guideline-details-object-updates/count : count all the tBGuidelineDetailsObjectUpdates.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-guideline-details-object-updates/count")
    @Timed
    public ResponseEntity<Long> countTBGuidelineDetailsObjectUpdates(TBGuidelineDetailsObjectUpdateCriteria criteria) {
        log.debug("REST request to count TBGuidelineDetailsObjectUpdates by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBGuidelineDetailsObjectUpdateQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-guideline-details-object-updates/:id : get the "id" tBGuidelineDetailsObjectUpdate.
     *
     * @param id the id of the tBGuidelineDetailsObjectUpdateDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBGuidelineDetailsObjectUpdateDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-guideline-details-object-updates/{id}")
    @Timed
    public ResponseEntity<TBGuidelineDetailsObjectUpdateDTO> getTBGuidelineDetailsObjectUpdate(@PathVariable Long id) {
        log.debug("REST request to get TBGuidelineDetailsObjectUpdate : {}", id);
        Optional<TBGuidelineDetailsObjectUpdateDTO> tBGuidelineDetailsObjectUpdateDTO = tBGuidelineDetailsObjectUpdateService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBGuidelineDetailsObjectUpdateDTO);
    }

    /**
     * DELETE  /tb-guideline-details-object-updates/:id : delete the "id" tBGuidelineDetailsObjectUpdate.
     *
     * @param id the id of the tBGuidelineDetailsObjectUpdateDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-guideline-details-object-updates/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBGuidelineDetailsObjectUpdate(@PathVariable Long id) {
        log.debug("REST request to delete TBGuidelineDetailsObjectUpdate : {}", id);
        tBGuidelineDetailsObjectUpdateService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
