package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBSuppliesOrderDetailsService;
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.TBSuppliesOrderDetailsDTO;
import vn.azteam.tpf.service.dto.TBSuppliesOrderDetailsCriteria;
import vn.azteam.tpf.service.TBSuppliesOrderDetailsQueryService;
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 TBSuppliesOrderDetails.
 */
@RestController
@RequestMapping("/api")
public class TBSuppliesOrderDetailsResource {

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

    private static final String ENTITY_NAME = "tBSuppliesOrderDetails";

    private final TBSuppliesOrderDetailsService tBSuppliesOrderDetailsService;

    private final TBSuppliesOrderDetailsQueryService tBSuppliesOrderDetailsQueryService;

    public TBSuppliesOrderDetailsResource(TBSuppliesOrderDetailsService tBSuppliesOrderDetailsService, TBSuppliesOrderDetailsQueryService tBSuppliesOrderDetailsQueryService) {
        this.tBSuppliesOrderDetailsService = tBSuppliesOrderDetailsService;
        this.tBSuppliesOrderDetailsQueryService = tBSuppliesOrderDetailsQueryService;
    }

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

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

    /**
     * GET  /tb-supplies-order-details : get all the tBSuppliesOrderDetails.
     *
     * @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 tBSuppliesOrderDetails in body
     */
    @GetMapping("/tb-supplies-order-details")
    @Timed
    public ResponseEntity<List<TBSuppliesOrderDetailsDTO>> getAllTBSuppliesOrderDetails(TBSuppliesOrderDetailsCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBSuppliesOrderDetails by criteria: {}", criteria);
        Page<TBSuppliesOrderDetailsDTO> page = tBSuppliesOrderDetailsQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-supplies-order-details");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-supplies-order-details/count : count all the tBSuppliesOrderDetails.
    *
    * @param criteria the criterias which the requested entities should match
    * @return the ResponseEntity with status 200 (OK) and the count in body
    */
    @GetMapping("/tb-supplies-order-details/count")
    @Timed
    public ResponseEntity<Long> countTBSuppliesOrderDetails(TBSuppliesOrderDetailsCriteria criteria) {
        log.debug("REST request to count TBSuppliesOrderDetails by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBSuppliesOrderDetailsQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-supplies-order-details/:id : get the "id" tBSuppliesOrderDetails.
     *
     * @param id the id of the tBSuppliesOrderDetailsDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBSuppliesOrderDetailsDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-supplies-order-details/{id}")
    @Timed
    public ResponseEntity<TBSuppliesOrderDetailsDTO> getTBSuppliesOrderDetails(@PathVariable Long id) {
        log.debug("REST request to get TBSuppliesOrderDetails : {}", id);
        Optional<TBSuppliesOrderDetailsDTO> tBSuppliesOrderDetailsDTO = tBSuppliesOrderDetailsService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBSuppliesOrderDetailsDTO);
    }

    /**
     * DELETE  /tb-supplies-order-details/:id : delete the "id" tBSuppliesOrderDetails.
     *
     * @param id the id of the tBSuppliesOrderDetailsDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-supplies-order-details/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBSuppliesOrderDetails(@PathVariable Long id) {
        log.debug("REST request to delete TBSuppliesOrderDetails : {}", id);
        tBSuppliesOrderDetailsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
