package vn.azteam.tpf.web.rest;

import com.codahale.metrics.annotation.Timed;
import vn.azteam.tpf.service.TBSuppliesOrderService;
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.TBSuppliesOrderDTO;
import vn.azteam.tpf.service.dto.TBSuppliesOrderCriteria;
import vn.azteam.tpf.service.TBSuppliesOrderQueryService;
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 TBSuppliesOrder.
 */
@RestController
@RequestMapping("/api")
public class TBSuppliesOrderResource {

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

    private static final String ENTITY_NAME = "tBSuppliesOrder";

    private final TBSuppliesOrderService tBSuppliesOrderService;

    private final TBSuppliesOrderQueryService tBSuppliesOrderQueryService;

    public TBSuppliesOrderResource(TBSuppliesOrderService tBSuppliesOrderService, TBSuppliesOrderQueryService tBSuppliesOrderQueryService) {
        this.tBSuppliesOrderService = tBSuppliesOrderService;
        this.tBSuppliesOrderQueryService = tBSuppliesOrderQueryService;
    }

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

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

    /**
     * GET  /tb-supplies-orders : get all the tBSuppliesOrders.
     *
     * @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 tBSuppliesOrders in body
     */
    @GetMapping("/tb-supplies-orders")
    @Timed
    public ResponseEntity<List<TBSuppliesOrderDTO>> getAllTBSuppliesOrders(TBSuppliesOrderCriteria criteria, Pageable pageable) {
        log.debug("REST request to get TBSuppliesOrders by criteria: {}", criteria);
        Page<TBSuppliesOrderDTO> page = tBSuppliesOrderQueryService.findByCriteria(criteria, pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tb-supplies-orders");
        return ResponseEntity.ok().headers(headers).body(page.getContent());
    }

    /**
    * GET  /tb-supplies-orders/count : count all the tBSuppliesOrders.
    *
    * @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-orders/count")
    @Timed
    public ResponseEntity<Long> countTBSuppliesOrders(TBSuppliesOrderCriteria criteria) {
        log.debug("REST request to count TBSuppliesOrders by criteria: {}", criteria);
        return ResponseEntity.ok().body(tBSuppliesOrderQueryService.countByCriteria(criteria));
    }

    /**
     * GET  /tb-supplies-orders/:id : get the "id" tBSuppliesOrder.
     *
     * @param id the id of the tBSuppliesOrderDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the tBSuppliesOrderDTO, or with status 404 (Not Found)
     */
    @GetMapping("/tb-supplies-orders/{id}")
    @Timed
    public ResponseEntity<TBSuppliesOrderDTO> getTBSuppliesOrder(@PathVariable Long id) {
        log.debug("REST request to get TBSuppliesOrder : {}", id);
        Optional<TBSuppliesOrderDTO> tBSuppliesOrderDTO = tBSuppliesOrderService.findOne(id);
        return ResponseUtil.wrapOrNotFound(tBSuppliesOrderDTO);
    }

    /**
     * DELETE  /tb-supplies-orders/:id : delete the "id" tBSuppliesOrder.
     *
     * @param id the id of the tBSuppliesOrderDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/tb-supplies-orders/{id}")
    @Timed
    public ResponseEntity<Void> deleteTBSuppliesOrder(@PathVariable Long id) {
        log.debug("REST request to delete TBSuppliesOrder : {}", id);
        tBSuppliesOrderService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

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

}
